I have the following situation:
If have a MySQL db with a InnoDB table which I use to store unique numbers. I start a transaction, read the value (eg. 1000471), store this value in another table and update the incremented value (100472). Now I want to avoid that somebody else even reads the value while my transaction is running.
If I would use plain MySQL I would do something like this:
Exceute("LOCK tbl1 READ");
Execute("SELECT ... from tbl1");
Execute("INSERT into tbl2");
Execute("UNLOCK TABLES");
but since I am using SubSonic as a DAL and the code should be independent from mysql, I have to use the TransactionScope.
My code:
TransactionOptions TransOpt = new TransactionOptions();
TransOpt.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
TransOpt.Timeout = new TimeSpan(0, 2, 0);
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew, TransOpt))
{
// Select Row from tbl1
// Do something
ts.Complete();
}
According to the help of TransactionOptions
system.transactions.isolationlevel
The effect I want to reach could be implemented with IsolationLevel.ReadCommitted, but I can still read the row from outside the transaction (If I try to change it, I get a lock, so the transaction is working)
Does anybody has a suggestion? Is a read lock even possible with TransactionScope