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
5
votes
4 answers

Is it possible to rollback committed data with TransactionScope?

The goal is simple - rollback data inserted by a unit test. Here is how it goes. In a unit test, a method is called that creates a new connection and inserts some data. After that a unit test creates a new connection and tries to find what has been…
Schultz9999
  • 8,717
  • 8
  • 48
  • 87
5
votes
1 answer

How to enlist with a TransactionScope?

Short Version How do i enlist in an ongoing TransactionScope? Long Version If you use a TransactionScope, you can create an "ambient" transaction: using (TransactionScope scope = new TransactionScope()) { //...stuff happens, then you…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
5
votes
2 answers

How to use entity framework transaction in raw query?

I am using entity framework but doing my operations with raw queries. My operations are like following: Check if recırd exist with integration_id Delete record if exit Insert new record So I am using transaction using (var transaction = await…
barteloma
  • 6,403
  • 14
  • 79
  • 173
5
votes
4 answers

High volume site using ADO.NET TransactionScope vs ExecuteCommand on NOLOCK, READ UNCOMMITTED directly?

Just read this interesting article by Omar on his blog Linq to SQL solve Transaction deadlock and Query timeout problem using uncommitted reads and at the end Javed Hasan started arguing with him about his solution to the nolock situation on a…
Ray
  • 12,101
  • 27
  • 95
  • 137
5
votes
3 answers

How to wrap IDbTransactions in a TransactionScope

I have several code methods that look like this: using (var connection = this.connectionFactory.GetConnection()) { connection.Open(); using (var transaction = connection.BeginTransaction()) { using (var command =…
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
5
votes
2 answers

Problems with TransactionScope in ASP.NET

I've build a class to synchronize data between two different datasources. This synchronization is divided into multiple parts (and methods). Every method has his own TransactionScope and the methods are run sequentially. Everytime I Run this code I…
user740229
  • 51
  • 1
  • 1
  • 3
5
votes
1 answer

Context is lost when reassigning Transaction.Current in async/await code

When reassigning Transaction.Current, I seem to lose the original TransactionScopeAsyncFlowOption behavior of my TransactionScope. The code after the second await loses it's Transaction.Current value. Sample code (Linqpad): async Task Main() { …
ThomasDC
  • 484
  • 3
  • 11
5
votes
2 answers

Hangfire Transactional Process (Unit of Work)

In .Net Framework code which is below, it is ensured that someEntity object is inserted into db and Publish opertion will be executed after. However, in .Net Core I could not manage to do this. When I try to run this piece of code, Platform…
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25
5
votes
2 answers

TransactionScope across AppDomains and processes

Is it real to use System.Transactions (primarily TransactionScope) across different AppDomains and processes? DependentTransaction works only inside one AppDomain.
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
5
votes
1 answer

Distributed transactions accross Azure SQL and on premises sql server causes errors

Is it possible to have a transaction between an on premises SQL Server instance and an Azure SQL database. I have the following tests cases. public class TransactionsTest { [Fact] public void Test1() { var premisesDatabaseContext…
Justin Lessard
  • 10,804
  • 5
  • 49
  • 61
5
votes
2 answers

What happens to Transaction if TransactionScope.Complete not called at the end of the Using block

I am using the SQL Server 2012 and There are multiple SQL Connections to same database inside the TransactionScope Using block. But if first Update SQL Operation does not result desired output then I am skipping next call to SQL Operation and also…
Bhalchandra K
  • 2,631
  • 3
  • 31
  • 43
5
votes
2 answers

Nhibernate with TransactionScope Error - DTC transaction prepre phase failed -- Upgrade to Nhibernate 3.0

I am getting the following exception when using Nhibernate and ADO.Net operations inside the transaction Scope.Eg. It was fine with Nhibernate 2.1 but now upgraded to 3.0 which throws error. using (var scope = new…
Prabu
  • 159
  • 2
  • 9
5
votes
2 answers

TransactionScope/SqlTransaction timeout extension

Is it possible to extend the timeout of a transaction (with SQL Server) once the transaction has started?
enashnash
  • 1,578
  • 1
  • 15
  • 36
5
votes
0 answers

Why does Transaction.Current become null after Cross-AppDomain call?

Consider the following small program that simply creates a TransactionScope, prints Transaction.Current, calls a method in another AppDomain (that takes a while to execute) and then prints Transaction.Current upon return. using System; using…
DeCaf
  • 6,026
  • 1
  • 29
  • 51
5
votes
3 answers

EF6 with TransactionScope - IsolationLevel.ReadUncommitted but got ReadCommitted first

There is a performance and lock issue when using EF for a update-from-query case on MSSQL 2008. So I put ReadUncommitted transaction isolationlevel, hoping to resolve it, like this, Before using (MyEntities db = new MyEntities()) { // large…