Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2003
    Location
    Munich, Bavaria
    Posts
    2

    Unanswered: Problems when adding two rows at the same time with C++ using ODBC (SQL-Server)

    Hi there,

    I'm new to database developing and have the following problem:
    I wrote an application in C++ to evaluate and update a database on an SQL-Server using the ODBC-interface. When testing the App using two computers running a test application to fill the Database, i saw that the newly generated data was written with wrong IDs. I found the following problem in my code:

    int AddRecordset()
    {
    CRecordset rs(m_pDB);
    CString cstrFilter = "Select something from ...";

    rs.m_strFilter = cstrFilter;

    rs.Open();
    rs.AddNew();

    // Filling in the values...

    rs.Update();

    .
    .
    .

    return TRUE;
    }

    (only an example function to show the prob)

    So, now if both computers execute the the rs.AddNew() function at the same time, the newly generated records are generated with the same ID (an auto value), which means, that only one record is written to the database. The table is locked only when the rs.Update() function is called.
    I think I'll have to lock the table as soon as possible after opening to prevent this loss of data, but I don't know how.
    The calling function includes the db.BeginTrans() and the db.CommitTrans() functions.

    It'd be grand if anyone had a suggestion to solve this problem.

    Regards,
    Peter
    Last edited by Bottleneck; 10-20-03 at 04:47.

  2. #2
    Join Date
    Oct 2003
    Location
    Munich, Bavaria
    Posts
    2

    Re: Problems when adding two rows at the same time with C++ using ODBC (SQL-Server)

    OK, I think i found a solution:

    before i call
    db.BeginTrans() ;
    I added
    db.ExecuteSQL("set transaction isolation level serializable");

    Now the loss of data is stopped at cost of speed. But is this really the only solution?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •