//Look up the Utility record and get the next transaction number
//Increment the transaction number in the Utility record and save the Utility record
//Assign the current transaction the transaction number
//Save the Transaction record
If (app.myDbase.Error= False) Then
app.myDbase.Commit
app.myDbase.SQLExecute ("END TRANSACTION")
Else
Dim sError As String = app.myDbase.ErrorMessage
app.myDbase.Rollback
MsgBox("DB Error: " + sError)
End If
[/code]
On occasion, I am getting duplicate Transaction numbers. Shouldn’t this not be possible since the entire process is enclosed in a Transaction?
Hard to tell as there seems to be code thats missing
If this IS all the code then no this won’t stop it from getting duplicates
You should do this in the following order (assuming your table is called NextID and the column is nextAvailable)
Begin Transaction
update NextID set nextAvailable = nextAvailable + 1
select nextAvailable from NextID
Commit
return next available
Why ?
The immediate update WILL block if there are multiple threads / process trying to do the update at once
And once they unblock they can safely read THEIR most recent update and get the right value
Doing it as read / update does not stop the read from occurring - but as update read does
I had a feeling that the database was allowing reading of the file during a transaction. I couldn’t imagine any other way this could happen. I thought there was some record locking going on during a transaction, but obviously not the case.
Remember that transactions make sure that all or none of your requests go through, and may not necessarily do any locking. If you don’t want anything to be able to read a table while you’re doing something else you’ll need to lock things up somehow.
I noticed that Norman suggested using the command “Update” within a single SQL statement. Can I assume that this command is is better than something like:
There is an autoincrement column as the primary key, however, these numbers represent multiple users from multiple clients (and can increment into unwieldy digits for the enduser - at least 8 digits).
Since the Utility record only has to deal with users from one client (say 10 or 20 users max), I believe crashing of users within a SQL update command would be near impossible (since the command executes within a few msec).
Actually this is how you HAD to do it before every db supported sequences etc.
WebObjects did this for a VERY long time & still can be configured to do this for you.
Works fine as long as you do it right.
I was under the assumption that a SQL transaction locked all the records in the database including preventing reading the records, but I guess it locks writes but not reads.
Reads can proceed - but if there are updates the reads will be of the values BEFORE the transaction was started - but there are cases where this can be altered (google for dirty reads)
On some db servers transactions & what values are read can be highly configurable