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
1 answer

Database transactions in Zend Framework: Are they isolated?

Using Zend Framework, I need to (1) read a record from a MySQL database, and (2) immediately write back to that record to indicate that it has been read. I don't want other processes or queries to be able to read from or write to the same record in…
Luke Eller
  • 627
  • 1
  • 7
  • 23
7
votes
1 answer

ASP.NET and ThreadStatic as part of TransactionScope's implementation

I was wondering how TransactionScope class works to keep the transaction between different method calls (without the need to pass it as a parameter) and I came to this doubt. I've got two considerations about this question: 1 Looking into…
Fabio
  • 3,020
  • 4
  • 39
  • 62
7
votes
2 answers

TransactionScope Timeout occurs prematurely?

I'm using TransactionScope to do some batch insert and updates. Problem is, I'm getting timeout exceptions on a 30 min long operation even when I set the timeout of the TransactionScope to one hour. Also after the exception it inserts seemingly…
dstr
  • 8,362
  • 12
  • 66
  • 106
7
votes
2 answers

Entity Framework Transaction With Multiple Threads

I have an application running multiple threads. The threads do NOT share an ObjectContext (each thread has its own - I know they are not thread safe). However, the threads are all operating under a shared Transaction. The original thread creates a…
Jeff
  • 35,755
  • 15
  • 108
  • 220
7
votes
2 answers

TransactionScope always tries to promote to MSDTC

I am trying to use a transaction scope inside a loop. The entire loop takes place using a single connection to the database. I am using entity framework 4 for database access. During the second iteration of the loop, when the LINQ to Entites…
DCNYAM
  • 11,966
  • 8
  • 53
  • 70
7
votes
4 answers

NHibernate 3.0: TransactionScope and Auto-Flushing

In NHibernate 3.0, FlushMode.Auto does not work when running under an ambient transaction only (that is, without starting an NHibernate transaction). Should it? using (TransactionScope scope = new TransactionScope()) { ISession session =…
7
votes
2 answers

TransactionScope and database connections

Do TransactionScope work with closed database connections? using (var transaction = new TransactionScope(TransactionScopeOption.Required)) { // creates a new connection, does stuff, commit trans and close repos1.DoSomething(); //…
jgauffin
  • 99,844
  • 45
  • 235
  • 372
7
votes
1 answer

Azure failed to marshal transaction into propagation token for elastic transaction (Works for MSDTC)

In windows azure we have hosted two asp.net webapi project as app service. We need to enable distributed transaction here. We initiate transaction inside one api. Then inside that transaction scope we fetch propagation token of that transaction and…
Anup
  • 1,502
  • 2
  • 15
  • 31
7
votes
1 answer

Why TransactionScope timeout is defined in machine.config?

As we know from this answer, maxTimeout for TransactionScope is defined in machine.config. So, what the reason? Why we can't override it in app.config (web.config) or just in code?
Backs
  • 24,430
  • 5
  • 58
  • 85
7
votes
2 answers

what is practical use of System.Transactions?

I have seen System.Transactions namespace, and wondered, can I actually make a RDMBS with this namespace usage? But when I saw some examples, I do not understand how System.Transactions does anything beyond simple try catch and getting us…
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
7
votes
1 answer

Stored Procedure without transaction in Entity Framework

I'm calling a stored procedure in Entity Framework 6 that can create Databases and tables if necessary. It is throwing the error; Message "CREATE DATABASE statement not allowed within multi-statement transaction.\r\nALTER DATABASE statement not…
Founder
  • 626
  • 6
  • 19
7
votes
1 answer

Rollback transaction with 2 DbContexts in Entity Framework. Use TransactionScope or TransactionBegin?

I have a 2 DbContext's that I call both of them in one form as you can see here: dbcontext1.add(object); dbcontext1.save(); dbcontext2.add(object); dbcontext2.save(); So I have a question: if some problems happen and my record saved…
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
7
votes
2 answers

Is it important to Open a sql connection in the transactionscope

I created a sqlconnection, CN1. Then this CN1 is opened. Later in the code there is a transactionscope. If I execute a sql command on this CN1 connection, is this within transaction? Code looks like this; SqlConnection cn1 = new…
mkus
  • 3,357
  • 6
  • 37
  • 45
7
votes
1 answer

WCF Transactions without MSDTC

I need to coordinate a transaction between several SOAP services implemented in WCF. I know I can use the WS-Atomic transaction or oleTransactions. For some technical reasons, I cannot make any use of the MSDTC. Is there a way I can coordinate a…
user1275011
  • 1,552
  • 1
  • 16
  • 36
7
votes
2 answers

Do Azure storage-related APIs participate in System.Transactions?

I can't find any information on this anywhere and yet the question is simple. Can I wrap storage-related actions in a TransactionScope such that e.g. if there is a rollback, the uploaded file is rolled back also? If the native APIs don't do this…
georgiosd
  • 3,038
  • 3
  • 39
  • 51