How Can I check if a dotnet transaction is closed or not ?
Asked
Active
Viewed 4,503 times
3 Answers
3
Your title asks one thing and your question asks another. so, I am going with your title.
If you want to know if the transaction is rolled back or set to rollback only, you can check
transaction.WasRolledBack // true if transaction is rolled back
Here, transaction
is an instance of ITransaction
Edit (based on your comment):
var isRolledBack = false;
using (var connection = new SqlConnection())
{
using (var transaction = connection.BeginTransaction())
{
try
{
// do your stuff here with transaction
}
catch (Exception ex)
{
transaction.Rollback();
isRolledBack = true;
throw;
}
}
}
Now, you can check the isRolledBack
flag to see if the transaction is rolled back

Mahesh Velaga
- 21,633
- 5
- 37
- 59
-
I am sorry..for that mistake - I mean diff in heading and subject. I am using SqlTransaction in C#. I am not sure...about ITransaction – Relativity Dec 16 '10 at 00:38
-
@Anish: Updated the answer for sql transaction – Mahesh Velaga Dec 16 '10 at 01:07
-
Transaction.Rollback() can also throw exceptions. You should catch those too. – Dan Bechard May 31 '16 at 12:59
1
using(TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)){
try{
//Do something;
scope.Complete(); //denotes the transaction completed successful.
}
catch(TransactionAbortedException ex)
{
//scope.Complete(); is never called, the transaction rolls back automatically.
}
catch(ApplicationException ex)
{
}
}

bleepzter
- 9,607
- 11
- 41
- 64
0
If you are on SQL server, you can use DBCC OPENTRAN

Raj More
- 47,048
- 33
- 131
- 198
-
2It's pretty clear from the question that it is not for SQL Server. – Greg Sansom Dec 16 '10 at 00:29