If anyone knows....
I have a db form where user can edit a db entity (lets say members) and i want to generate some sort of serial code (lets say member_code) in MySQL/InnoDb without using the autoinc field, in a multiuser enviroment.
I have created a table for storing the last value for each code in a seperated table/record that lets say
counters(counter_id,counter_value)
and each time i want to get a serial number, i first open a query with the following sql statement
select counter_value from counters where counter_id=:counter_id FOR UPDATE;
and read the counter_value in my application. Then i calculate the next value, and execute the following sql statement
update counters set counter_value=:counter_value where counter_id=:counter_id
and finally i call TSQLTransaction.Commit;
All this are packed in a function, lets say GenerateCode(counter_id);
I though that it should work, but in my tests (lazarus 1.2.0) it didn't (I create a small procedure that calls the GenerateCode() 500 or more and tries to insert the codes in another table as primary key, so that in case it gets a code that is already given to someone else the db will fire a primary key violation exception. The result was db fired the exception ~ 20 times)
After reading again the MySQL docs i find out that SELECT FOR UPDATE has no result if you not turn off the session variable autocommit.
So, i modified my CreateCode() function and put an TSQLConnection.ExecuteDirect('SET autocommit=0'); before issuing the rest statements.
short tested again, and seems to work now.
Now, my problem is that:
I call my GenerateCode() routine as the last step of a TSQLQuery.BeforePost event handler in order to get a serial code. So GenerateCode() will set the autocommit session variable to 0.
When should i reset the autocommit back to 1????
In my db-forms i always call in AfterPost event the DataSet.ApplyUpdates; and TSQLTransaction.CommitRetaining; methods.
On thought is after the immidiatly after the CommitRetaining or RollbackRetaing (only for the db forms that they call the GenerateCode function );
Any suggestions ? I am asking because i don't know what side effects this may have with the TSQLTransaction component i use.