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

Change the TransactionScope IsolationLevel after completing the transaction

When i save data in the database i used TransactionScope with IsolationLevel set to Serializable. TransactionOptions options = new TransactionOptions { IsolationLevel=IsolationLevel.Serializable }; using (var…
Manvinder
  • 4,495
  • 16
  • 53
  • 100
7
votes
4 answers

How to handle nested datacontext in the BL?

public class TestBL { public static void AddFolder(string folderName) { using (var ts = new TransactionScope()) { using (var dc = new TestDataContext()) { var folder = new Folder {…
Eran Betzalel
  • 4,105
  • 3
  • 38
  • 66
7
votes
1 answer

Unit Of Work Pattern with Database Transactions

I've been tried to get my head around this for a couple of days and there are lots of tutorials around unit of work and around TransactionScope but I can't find anything that talks about the two together. Any help much appreciated! I am using Entity…
Matt Austin
  • 219
  • 1
  • 3
  • 13
7
votes
3 answers

Why is my TransactionScope trying to use MSDTC when used in an EF Code First app?

I have just introduced a TransactionScope use to an MVC3 app using EF 4.3 Code First, against a SQL 2010 Express local DB. When I try a SaveChanges inside the scope, I get a "Provider failed to open" notice, with an inner exception about a missing…
ProfK
  • 49,207
  • 121
  • 399
  • 775
6
votes
3 answers

Why does the following SQL Server insert deadlock when run within a transaction?

I'm currently inserting a record into a SQL Server Table and then selecting the auto-increment ID as follows: (@p0 int,@p1 nvarchar(8))INSERT INTO [dbo].[Tag]([Some_Int], [Tag]) VALUES (@p0, @p1) SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]…
LaserJesus
  • 8,230
  • 7
  • 47
  • 65
6
votes
5 answers

TransactionInDoubtException using System.Transactions on SQL Server 2005

The underlying question to this post is "Why would a non-promoted LTM Transaction ever be in doubt?" I'm getting System.Transactions.TransactionInDoubtException and i can't explain why. Unfortunately i cannot reproduce this issue but according to…
Mark
  • 5,223
  • 11
  • 51
  • 81
6
votes
2 answers

TransactionScope not working with Parallel Extensions?

If i do the following: Using scope = New TransactionScope() entries.Content.ReadAs(Of IList(Of WebMaint)).AsParallel.ForAll(Sub(entry) _repos.Update(entry) …
coding4fun
  • 8,038
  • 13
  • 58
  • 85
6
votes
5 answers

Transaction Scope fails with BeginTransaction in Oracle : Connection is already part of a local or a distributed transaction

Having this strange behavior while using OracleConnection with TransactionScope. If i try to use connection.BeginTransaction() in a transaction scope i get simple elegant InvalidOperationException : Connection is already part of a local or a…
adt
  • 4,320
  • 5
  • 35
  • 54
6
votes
4 answers

C# - Usage of transactions in business layer (SQLServer 2005+ , Oracle) - good examples

I am gonna build a service using 3-tier architecture and I am really worried about how to handle operations in a transacted way. I know I have 2 options: IDbTransaction and TransactionScope... but I am not really decided for which one to go,…
Learner
  • 3,297
  • 4
  • 37
  • 62
6
votes
6 answers

SharePoint 2007: How can I perform a series of operations within a transaction?

I would love to know how to perform a series of operations in a SharePoint context within a transaction. For example, I would like to be able to do something like the…
Trent
  • 560
  • 4
  • 11
6
votes
1 answer

Transaction can't handle parallel commands via Task.WhenAll

I have some main table (like Companies) and a lot of dependent tables (like CompanyAddresses, CompanyPaymentInfos, etc.) in my Postgres DB: CREATE TABLE Companies ( Id uuid NOT NULL PRIMARY KEY, ...); CREATE TABLE CompanyAddresses( CompanyId uuid…
6
votes
0 answers

SQLServer. Unable to make ALLOW_SNAPSHOT_ISOLATION work in c# code (it works on Management Studio)

Recently I had to resolve lock problems such as Transaction was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. After reading several articles and analyzing on the context of my…
6
votes
0 answers

EF 6 not working with transaction scope when mixing ExecuteSqlCommand and LINQ to EF inside the same scope

I am using EF 6.1 with .NET 4.6.1. If I create a transaction scope with TransactionScopeAsync inside a using block on the dbContext and then update data in the database directly with dbContext.Database.ExecuteSqlCommand and then call the same entity…
goroth
  • 2,510
  • 5
  • 35
  • 66
6
votes
3 answers

TransactionScope causes TransactionManagerCommunicationException when used for DB unit tests

I'd like to run some tests of stored procedures in my database without actually affecting the data (or, to put it more exactly, without a lasting impact after the test has run). After some research I came up with the approach of using…
Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89
6
votes
3 answers

TransactionScope throws exception

Im sure this is so basic question but i can't found any solution on google neither here. when im trying to use that code block it throws an exception that 'System.Activities.Statements.TransactionScope': type used in a using statement must be…
eerrzz
  • 274
  • 1
  • 4
  • 19