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)

1049 questions
11
votes
1 answer

MSDTC is only working in one direction

I'm trying to use TransactionScope for unit tests and I keep getting errors on our build server. After following several helpful SO answers and blogs, I installed DTCPing and ran it on both server1 & server2. When I run it in the reverse order it…
kelloti
  • 8,705
  • 5
  • 46
  • 82
11
votes
3 answers

Why am I getting this error suddenly?

So I have a WCF service, inside which there's a Process() method. This method reads a byte array (a file) from one table, and basically puts that data from that file into multiple tables. It just iterates through each row. It was working fine since…
karan k
  • 947
  • 2
  • 21
  • 45
11
votes
4 answers

Why doesn't TransactionScope assume success?

The TransactionScope expects a call to its Complete method as follows. Otherwise the transaction will not be committed. using(TransactionScope scope = new TransactionScope()) { /* Perform transactional work here */ …
Scott Munro
  • 13,369
  • 3
  • 74
  • 80
11
votes
2 answers

Nested Transactions with TransactionScope

If you have somehting like this: IBinaryAssetStructureRepository rep = new BinaryAssetStructureRepository(); var userDto = new UserDto { id = 3345 }; var dto = new BinaryAssetBranchNodeDto("name", userDto, userDto); using (var scope1 = new…
Lieven Cardoen
  • 25,140
  • 52
  • 153
  • 244
11
votes
1 answer

Multiple SubmitChanges and transaction rollback using Linq To SQL

I'm using TransactionScope to submit data in Linq to SQL. My question is, if I use multiple SubmitChanges in the same scope, will all scope roll back in case of an error or just the changes made after the last SubmitChanges? For example: using…
dstr
  • 8,362
  • 12
  • 66
  • 106
10
votes
2 answers

Does TransactionScope Need DTC Service on?

From what I'm reading, in order to use TransactionScope in .NET, you need the Distributed Transaction Coordinator service in Windows to be running. I have that service turned off, and my app seems to be running the same and rolls back transactions…
SaltProgrammer
  • 1,045
  • 2
  • 13
  • 29
10
votes
1 answer

Recommended practice for stopping transactions escalating to distributed when using transactionscope

Using the TransactionScope object to set up an implicit transaction that doesn't need to be passed across function calls is great! However, if a connection is opened whilst another is already open, the transaction coordinator silently escalates the…
Kram
  • 4,099
  • 4
  • 39
  • 60
10
votes
1 answer

Why -- HOW -- are transactions processed after disposal?

I'm trying to work with some ambient transaction scopes (thanks, entity-framework), which I haven't really done before, and I'm seeing some ... odd behavior that I'm trying to understand. I'm trying to enlist in the current transaction scope and do…
user1228
10
votes
2 answers

TransactionScope and Connection Pooling

I'm trying to get a handle on whether we have a problem in our application with database connections using incorrect IsolationLevels. Our application is a .Net 3.5 database app using SQL Server 2005. I've discovered that the IsolationLevel of…
10
votes
2 answers

SqlConnection and avoiding promotion to MSDTC

When we need to do database access in our application, we use the following patterns: For querying, we have a static factory class with a method CreateOpenConnection which does nothing more than new SqlConnection(myConnectionString) and calls…
JulianR
  • 16,213
  • 5
  • 55
  • 85
10
votes
2 answers

Check if an existing transactionscope is active

I am using: public class TransactionUtils { public static TransactionScope CreateTransactionScope() { var TransactionOptions = new TransactionOptions(); TransactionOptions.IsolationLevel =…
webnoob
  • 15,747
  • 13
  • 83
  • 165
9
votes
2 answers

Where should I perform action when implementing IEnlistmentNotification?

I am trying to create a custom "resource manager" by implementing IEnlistmentNotification interface. This interface has following methods: Prepare() Commit() Rollback() InDoubt() While it's clear that rollback code should go in Rollback() method I…
matori82
  • 3,669
  • 9
  • 42
  • 64
9
votes
2 answers

Can't I call a stored procedure from Entity Framework inside a transaction scope?

I have a method that uses Entity Framework to do some changes/inserts in different entities, all this inside a single transaction scope. These changes works very well. My problem has began when I needed to use a stored procedure in the middle of…
Victor Rodrigues
  • 11,353
  • 23
  • 75
  • 107
9
votes
1 answer

Locking a table with a select in Entity Framework

I need to do something like this select * from myTable with (xlock,holdlock) using Entity Framework. Is this possible? I've opened a TransactionScope with the Serializable isolation level but my selects are not locking the tables. I'd like them…
8
votes
2 answers

Understanding Transactions in Entity Framework

Hi I am trying to use transactions along with Entity Framework. With so much information available online on the different ways to implement transactions I must say I am a bit confused on the right way. I have an example database with two tables…
nighthawk457
  • 1,102
  • 3
  • 12
  • 27