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

What is TransactionScope default Timeout value?

When i create a TransactionScope object as followed: using (TransactionScope ts = new TransactionScope()) { // Do stuff... } What is the default transaction timeout of the given ts object?
Uri Abramson
  • 6,005
  • 6
  • 40
  • 62
24
votes
4 answers

Distributed transaction completed. Either enlist this session in a new transaction or the NULL transaction

Just curious if anyone else has got this particular error and know how to solve it? The scenario is as follow... We have an ASP.NET web application using Enterprise Library running on Windows Server 2008 IIS farm connecting to a SQL Server 2008…
Jimmy Chandra
  • 6,472
  • 4
  • 26
  • 38
21
votes
2 answers

TransactionScope TransactionAborted Exception - transaction not rolled back. Should it be?

(SQL SERVER 2008) If a Transaction Timeout error occurs within a TransactionScope (.Complete()) would you expect the transaction to be rolled back? Update: The error is actually being thrown in the closing curly brace (i.e. .Dispose()), not…
MT.
  • 791
  • 5
  • 15
  • 23
21
votes
4 answers

Difference between Implicit and Explicit Transaction

What is the difference between Implicit and Explicit transaction in Sql Server 2008? What happens in TransactionScope background? I'm using TransactionScope but in Sql server profiler I don't see "Begin transaction..." statement. How does it work?
Arian
  • 12,793
  • 66
  • 176
  • 300
21
votes
3 answers

TransactionScope: Avoiding Distributed Transactions

I have a parent object (part of a DAL) that contains, amongst other things, a collection (List) of child objects. When I'm saving the object back to the DB, I enter/update the parent, and then loop through each child. For maintainability, I've…
CJM
  • 11,908
  • 20
  • 77
  • 115
20
votes
1 answer

Using TransactionScope around a stored procedure with transaction in SQL Server 2014

I am using C# and ADO.Net with a TransactionScope to run a transaction in an ASP.Net app. This transaction is supposed to save some data across multiple tables and then send an email to subscribers. Question: is it a valid use of TransactionScope,…
Sunil
  • 20,653
  • 28
  • 112
  • 197
20
votes
1 answer

Connection pool corrupted by nested ADO.NET transactions (with MSDTC)

I can't find answer anywhere. I will show simple code fragment which presents how to easily corrupt connection pool. Connection pool corruption means that every new connection open try will fail. To experience the problem we need: to be in…
owerkop
  • 281
  • 3
  • 6
19
votes
2 answers

TransactionScope With Files In C#

I've been using TransactionScope to work with the database and it feels nice. What I'm looking for is the following: using(var scope=new TransactionScope()) { // Do something with a few files... scope.Complete(); } but…
avance70
  • 787
  • 1
  • 11
  • 22
18
votes
3 answers

Is there a way to use TransactionScope with an existing connection?

I have some code that works like the advised use of TransactionScope, but has an ambient connection instead of an ambient transaction. Is there a way to use a TransactionScope object with an existing connection, or is there an alternative in the…
Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197
18
votes
1 answer

Difference Between Transaction and TransactionScope

I am developing an application which communicates with an SQL Server 2005 database to execute some stored procedures. My client demands that all transactions be managed on the C# side and not by SQL Server, and so I am using…
User
  • 3,244
  • 8
  • 27
  • 48
17
votes
1 answer

Understanding TransactionScopeOptions: RequiresNew = Suppress + Required?

I believe I understand TransactionScopeOption.Suppress and TransactionScopeOption.Required but am having difficulty understanding what TransactionScopeOption.RequiresNew does. Based on the last explanation that I read, would the following two blocks…
Jaxidian
  • 13,081
  • 8
  • 83
  • 125
17
votes
6 answers

TransactionScope maximumTimeout

I use TransactionScope in this code: private void ExecuteSP() { bool IsComplete = false; SqlCommand sqlComm = null; //6 hours!!! TimeSpan ts1 = new TimeSpan(6, 0, 0); try { using (TransactionScope t = new…
user436862
  • 867
  • 2
  • 15
  • 32
16
votes
1 answer

TransactionScope, where is begin transaction on sql profiler?

i need to do something like this on a transaction context using(var context = new Ctx()) { using (TransactionScope tran = new TransactionScope()) { decimal debit = 10M; int id = 1; var data = context.Cashier .Where(w => w.ID ==…
Alexandre
  • 7,004
  • 5
  • 54
  • 72
16
votes
3 answers

EF: How do I call SaveChanges twice inside a transaction?

Using Entity Framework (code first in my case), I have an operation that requires me to call SaveChanges to update one object in the DB, and then SaveChanges again to update another object. (I need the first SaveChanges to resolve an issue where EF…
15
votes
4 answers

How to find the Transaction Status

I am using 'TransactionScope', and I need to just do some DML within the C# Code, which I have successfully. I need to find out that what is the status of the Transaction, that is has it completed successfully or not ? Because on the basis of the…
Muzaffar Ali Rana
  • 487
  • 4
  • 11
  • 22
1 2
3
69 70