Questions tagged [transactionscope]

TransactionScope is a .NET class used to mark a block of code as transactional. It uses an implicit programming model so transactions are managed by the infrastructure, rather than the developer. The class was introduced in .NET 2.0.

Description

The TransactionScope class is part of the System.Transactions namespace. Unlike the System.Transactions.Transaction class, which uses an explicit programming model, the TransactionScope class uses an implicit programming model, which simplifies how transactions are used in client code.

How to use it

To use TransactionScope, the developer simply creates a scope, does the transactional work and then indicates when the transaction is complete. There is no need to explicitly rollback the transaction in the case of an error.

 using(var scope = new TransactionScope())
 {
      // do database or other transactional work here
      scope.Complete();
 }

Inside the scope, resources are automatically enlisted in the ambient transaction so there is no need to manage the transaction directly. The transaction is automatically rolled back unless it is marked as complete before TransactionScope is disposed.

References

TransactionScope Class (MSDN)

1015 questions
286
votes
7 answers

TransactionScope automatically escalating to MSDTC on some machines?

In our project we're using TransactionScope's to ensure our data access layer performs it's actions in a transaction. We're aiming to not require the MSDTC service to be enabled on our end-user's machines. Trouble is, on half of our developers…
Yoopergeek
  • 5,494
  • 3
  • 21
  • 29
203
votes
3 answers

Under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction?

What does it mean for an SqlConnection to be "enlisted" in a transaction? Does it simply mean that commands I execute on the connection will participate in the transaction? If so, under what circumstances is an SqlConnection automatically enlisted…
Triynko
  • 17,370
  • 20
  • 92
  • 154
121
votes
3 answers

Get TransactionScope to work with async / await

I'm trying to integrate async/await into our service bus. I implemented a SingleThreadSynchronizationContext based on this example http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx. And it works fine, except for one thing:…
Yann
  • 1,298
  • 2
  • 10
  • 8
102
votes
2 answers

How does TransactionScope roll back transactions?

I'm writing an integration test where I will be inserting a number of objects into a database and then checking to make sure whether my method retrieves those objects. My connection to the database is through NHibernate...and my usual method of…
mezoid
  • 26,370
  • 35
  • 104
  • 147
92
votes
2 answers

Database.BeginTransaction vs Transactions.TransactionScope

What is the difference between System.Transactions.TransactionScope and EF6's Database.BeginTransaction? Could someone give a small example or just explain which one to use when with a clear difference? P.S: In my project, I'm using EF6. I've…
91
votes
10 answers

The transaction manager has disabled its support for remote/network transactions

I'm using SQL Server and ASP.NET. I have the following function: Using js = daoFactory.CreateJoinScope() Using tran = New Transactions.TransactionScope() '... tran.Complete() End Using End Using However, the exception 'The…
Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148
76
votes
3 answers

Why is System.Transactions TransactionScope default Isolationlevel Serializable

I am just wondering what a good reason to use Serializable as the default Isolationlevel may be when creating a System.Transactions TransactionScope, because I cannot think of any (and it seems that you cannot change the default via web/app.config…
Bernhard Kircher
  • 3,892
  • 3
  • 29
  • 38
67
votes
6 answers

"The operation is not valid for the state of the transaction" error and transaction scope

I am getting the following error when I try to call a stored procedure that contains a SELECT Statement: The operation is not valid for the state of the transaction Here is the structure of my calls: public void MyAddUpdateMethod() { using…
Michael Kniskern
  • 23,162
  • 65
  • 156
  • 224
65
votes
7 answers

TransactionScope Prematurely Completed

I have a block of code that runs within a TransactionScope and within this block of code I make several calls to the DB. Selects, Updates, Creates, and Deletes, the whole gamut. When I execute my delete I execute it using an extension method of the…
Hungry Beast
  • 3,509
  • 6
  • 42
  • 72
63
votes
4 answers

SQL Server: Isolation level leaks across pooled connections

As demonstrated by previous Stack Overflow questions (TransactionScope and Connection Pooling and How does SqlConnection manage IsolationLevel?), the transaction isolation level leaks across pooled connections with SQL Server and ADO.NET (also…
usr
  • 162,013
  • 33
  • 219
  • 345
62
votes
2 answers

Asynchronously commit or rollback a transaction scope

As many knows, TransactionScope were forgotten when the async await pattern was introduced in .Net. They were broken if we were trying to use some await call inside a transaction scope. Now this is fixed thanks to a scope constructor option. But it…
Frédéric
  • 8,372
  • 2
  • 51
  • 102
52
votes
3 answers

Will an inner transaction scope roll back if the outer transaction scope doesn't complete?

I have two transaction scopes, one within another. I would love to know if the inner transaction scope will be rolled back after it has been committed and the outer one does not complete.
Tebo
  • 13,615
  • 11
  • 49
  • 64
48
votes
1 answer

Dapper & TransactionScope?

I just started playing around with Dapper. So far i love it. Does dapper not work with TransactionScope? I noticed that even if i never call TransactionScope.Complete then my changes are still committed to the database. If TransactionScope isn't…
coding4fun
  • 7,611
  • 9
  • 53
  • 76
44
votes
2 answers

TransactionScope and multi-threading

I was wondering how you would use the TransactionScope class in the correct way when you are dealing with multithreading? We create a new scope in our main thread and then we spawn off a couple of worker threads and we want these to participate in…
TheCodeJunkie
  • 8,920
  • 7
  • 40
  • 54
38
votes
3 answers

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

Very simple question: is it possible to use System.Transactions.TransactionScope together with SqlBulkCopy? The documentation Transaction and Bulk Copy Operations doesn't mention anything (at least as of .NET 4.0) and my testing indicates it does…
jason
  • 220,745
  • 31
  • 400
  • 507
1
2 3
67 68