C# TransactionScope class configuration
We are using Transaction Scope for performing transactions in .Net 3.0. How do I configure it to use "Read Committed SNAPSHOT Isolation” Transaction as开发者_Go百科 default?
Note: We are looking for a one place fix. i.e, a configuration entry for SNAPSHOT.
We need enterprise level configuration. We cannot go for each every place where Transaction Scope is used. Is there a application config file where we can change it?
Try this:
var scope = new TransactionScope(IsolationLevel.ReadCommitted)
As per your update, try this instead:
var scope = new TransactionScope(IsolationLevel.Snapshot)
Check out this MSDN page on the IsolationLevel
enumeration for more information.
Update:
AFAIK, you can't configure this at a settings-file level, simply because the IsolationLevel
is dictated by construction of each instance of the type and external configuration for such things is extremely rare (off the top of my head I can only really think of one instance where this is possible with ASP.NET and controls, but not a single type).
Furthermore, TransactionScope
is a sealed
class, which means you can't inherit and extend its functionality to always construct a type with IsolationLevel.Snapshot
.
You could create your own type, SnapshotIsolationLevelTransactionScope
(for instance), and expose an UnderlyingTransactionScope
property so that the functioning class you want to work with is available to callers - this would allow you to always construct the scope with the desired isolation level, but still requires you alter existing code and continue to use this type in the future where needed.
精彩评论