Ignore TransactionScope for specific query

If you wrap your log call inside of another transaction scope with the suppress option enabled, transaction scope will not be used. public override int SaveChanges() { try { return base.SaveChanges(); } catch (Exception ex) { using (var scope = new TransactionScope(TransactionScopeOption.Suppress)) { LogRepo.Log(message); // stuff to log from the context } throw; } }

How does TransactionScope work?

Hope this helps: http://msdn.microsoft.com/en-us/magazine/cc300805.aspx For those unfamiliar with TransactionScope, it is part of the System.Transactions namespace new to the Microsoft® .NET Framework 2.0. System.Transactions provides a transactions framework fully integrated into the .NET Framework, including but not limited to ADO.NET. The Transaction and TransactionScope classes are two of the most important classes in this namespace. … Read more

Refactoring ADO.NET – SqlTransaction vs. TransactionScope

You won’t immediately gain anything by switching your existing code to use TransactionScope. You should use it for future development because of the flexibility it provides. It will make it easier in the future to include things other than ADO.NET calls into a transaction. BTW, in your posted example, the SqlCommand instances should be in … Read more

How to use TransactionScope properly?

The code within the methods you call need to be transaction aware and enlist in the active transaction. This means creating or using classes which are resource managers (see Implement Your Own Resource Manager. You do this by implementing IEnlistmentNotification and enlisting in the transaction. When the transaction is completed, the transaction manager will call … Read more

NHibernate with TransactionScope

I have been using nHibernate 2.1 for awhile, and after a few production issues and trying quite a few variations, we have settled on the following method, as per Avoiding Leaking Connections With NHibernate And TransactionScope: using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (var session = sessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { // … Read more

How to know if the code is inside TransactionScope?

Transaction.Current should be reliable; I’ve just checked, at this works fine with suppressed transactions, too: Console.WriteLine(Transaction.Current != null); // false using (TransactionScope tran = new TransactionScope()) { Console.WriteLine(Transaction.Current != null); // true using (TransactionScope tran2 = new TransactionScope( TransactionScopeOption.Suppress)) { Console.WriteLine(Transaction.Current != null); // false } Console.WriteLine(Transaction.Current != null); // true } Console.WriteLine(Transaction.Current != null); … Read more

Multiple SaveChanges calls in entity framework

I know it’s kind of late answer but i found it useful to share. Now in EF6 it’s easier to achieve this by using dbContext.Database.BeginTransaction() like this : using (var context = new BloggingContext()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { // do your changes context.SaveChanges(); // do another changes context.SaveChanges(); dbContextTransaction.Commit(); } … Read more

Is it possible to use System.Transactions.TransactionScope with SqlBulkCopy?

SqlBulkCopy never enlists into a transaction. SqlCommand also does not do that. Common misconception. The enlistment is performed at the time SqlConnection.Open is called. After that, anything that runs on that connection is part of the transaction implicitly. In fact it is no longer allowed to pass an explicit transaction. If you want SqlBulkCopy to … Read more

Transaction scope timeout on 10 minutes

To further clarify: Transaction Scope uses the Machine config setting as the maximum timeout. The default machine timeout is 10 minutes. Setting the machine config to 2 hours: <system.transactions> <machineSettings maxTimeout=”02:00:00″/> </system.transactions> The app.config or web.config can be used reduced to the timeout but can not be used to exceed the machine config timeout. Setting … Read more

tech