TransactionScope, selects with Subsonic
I have an Invoice table (and SubSonic 'Invoice' ActiveRecord()
with a column InvoiceNumebr
that must have unique numbers. I am trying to use GetTheNextAvailableNumber()
inside a block TransactionScope
.
I'm not sure what happens if 5 or 50 different users try to create an invoice for approx. at the same time, the method will return the same number for all 5 or 50 users, unless they save the account object until it appears.
The method GetTheNextAvailableNumber()
that is called inside the block TransactionScope
uses a Subsonic Select query MAX()
to get the maximum number, and then adds 1. The column itself has a UNIQUE index!
Will there be a transaction isolation level default (Serializable) make sure each one gets a unique number? Or is there a smarter mechanism to achieve this? The column cannot have an IDENTITY because the PK InvoiceID column already has one.
source to share
What if you create a second table in your db and store the value NextAvailableNumber
in that table. You will then use a stored procedure to retrieve this value and increment it in the same call. You just need to lock this stored procedure to prevent concurrent calls. You can also lock it in a stored procedure.
source to share