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
15
votes
2 answers

Linq: Delete and Insert same Primary Key values within TransactionScope

I want to replace existing records in the DB with new records in one transaction. Using TransactionScope, I have using ( var scope = new TransactionScope()) { db.Tasks.DeleteAllOnSubmit(oldTasks); db.Tasks.SubmitChanges(); …
Candy Chiu
  • 6,579
  • 9
  • 48
  • 69
15
votes
2 answers

TransactionScope and Isolation Level

we have a problem to use TransactionScope. TransactionScope get to us very good flexibility to use transactions across our Data Access Layer. On this way we can use transactions implicit or explicit. There are some performance boost again ADO.NET…
Anton Kalcik
  • 2,107
  • 1
  • 25
  • 43
15
votes
2 answers

TransactionScopeOption - Required or RequiresNew

I'm currently having a confusion concerning the constructor of the TransactionScope object. Say that users of my website can order products. On submitting their request, I carry out a verification of the current quantity left and if it is still…
15
votes
6 answers

What is the reason of "Transaction context in use by another session"

I'm looking for a description of the root of this error: "Transaction context in use by another session". I get it sometimes in one of my unittests so I can't provider repro code. But I wonder what is "by design" reason for the error. UPDATE: the…
Shrike
  • 9,218
  • 7
  • 68
  • 105
15
votes
1 answer

Entity Framework 6 async operations and TranscationScope

I search on stackoverflow but could not find a similar question, please point me if there is already one. I was trying to implement a generic reusable repository with both sync and async operations but with my little knowledge with Entity Framework…
Sandeep Kumar
  • 1,505
  • 1
  • 15
  • 24
15
votes
4 answers

TransactionScope - The underlying provider failed on EnlistTransaction. MSDTC being aborted

Our team have got a problem that manifests as: The underlying provider failed on EnlistTransaction; Cannot access a disposed object.Object name: 'Transaction'. which seemed to appear as soon as we began using TransactionScope to handle our…
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
14
votes
1 answer

TransactionScope vs. IDbTransaction

What are the advantages/disadvantages of using TransactionScope in comparison to IDbTransaction? I will suggest some - please correct/complete the list. Advantages of TransactionScope: TransactionScope supports distributed transactions - you can…
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
14
votes
1 answer

Entity Framework and Transactionscope doesn't revert the isolation level after dispose of Transactionscope

I am struggeling a bit with transaction scopes and entity framework. Initially we want all our connections in the application to use snapshot isolation level when reading data, but in some circumstances we want to read data with either read…
Rune G
  • 1,447
  • 2
  • 12
  • 26
14
votes
2 answers

Why is TransactionScope operation is not valid?

I have a routine which uses a recursive loop to insert items into a SQL Server 2005 database The first call which initiates the loop is enclosed within a transaction using TransactionScope. When I first call ProcessItem the myItem data gets inserted…
Cragly
  • 3,554
  • 9
  • 45
  • 59
14
votes
2 answers

C# - System.Transactions.TransactionScope

I was curious about the TransactionScope class. For the most part, I assume it was intended for database connections (which is what I've used it for). My question, is can you put any code in the using-block of a TransactionScope to make it…
jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
14
votes
5 answers

SQLTransaction has completed error

I got following error once in my application. This SQLTransaction has completed; it is no longer usable Stack Trace is attached below – It says about Zombie Check and Rollback. What is the mistake in the code? Note: This error came only…
LCJ
  • 22,196
  • 67
  • 260
  • 418
13
votes
6 answers

TransactionScope not rolling back transaction

Here is the current architecture of my transaction scope source code. The third insert throws an .NET exception (Not a SQL Exception) and it is not rolling back the two previous insert statements. What I am doing wrong? EDIT: I removed the…
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
13
votes
1 answer

Error Binding Gridview: "The current TransactionScope is already complete"

I am doing cascading deletes in an event sent from a Gridview. The deletes are in a Transaction. Here is the simplified code: protected void btnDeleteUser_Click(object sender, EventArgs e) { DataContext db; db = new DataContext(); using…
cdonner
  • 37,019
  • 22
  • 105
  • 153
12
votes
1 answer

TransactionScope Complete() doesn't commit the transaction before exiting the USING statement

I am experiencing this weird behavior where the transaction gets committed only when the using exits and not when calling scope.Complete(); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) { …
user2312219
  • 443
  • 1
  • 5
  • 12
12
votes
2 answers

What does a TransactionScope really do

Looking into it I verified that for example the value o "myInt" is not rolledback in the following scenario int myInt = 10; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) { myInt=20; Transaction t =…
Leonardo
  • 10,737
  • 10
  • 62
  • 155