If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > Problems when adding two rows at the same time with C++ using ODBC

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-03, 03:20
Bottleneck Bottleneck is offline
Registered User
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 2
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 03:47.
Reply With Quote
  #2 (permalink)  
Old 10-20-03, 08:21
Bottleneck Bottleneck is offline
Registered User
 
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?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On