P
Peter Strøiman
Hi.
Since my situation is a little complex I have simplified it for this post.
I start a database transaction. Do some work with it in a try-finally block,
and in the finally block I dispose resources. I would like to in my finally
block detect if we are leaving because of the code actually just leaving the
block (normally or through a return statement) or an exception occurred. If
we are leaving normally I want to commit the transaction, and if we are
leaving because an exception is thrown, I want to abort the transaction.
SqlConnection conn = CreateNewOpenConnection(...)
try
{
SqlTransaction trans = conn.BeginTransaction()
// do work
}
finally
{
if (exception occurred)
trans.Rollback();
else
trans.Commit();
trans.Dispose();
conn.Dispose();
}
Is this possible. I'm using the .NET framework 2.0
Thanks in advance,
Peter Strøiman
Since my situation is a little complex I have simplified it for this post.
I start a database transaction. Do some work with it in a try-finally block,
and in the finally block I dispose resources. I would like to in my finally
block detect if we are leaving because of the code actually just leaving the
block (normally or through a return statement) or an exception occurred. If
we are leaving normally I want to commit the transaction, and if we are
leaving because an exception is thrown, I want to abort the transaction.
SqlConnection conn = CreateNewOpenConnection(...)
try
{
SqlTransaction trans = conn.BeginTransaction()
// do work
}
finally
{
if (exception occurred)
trans.Rollback();
else
trans.Commit();
trans.Dispose();
conn.Dispose();
}
Is this possible. I'm using the .NET framework 2.0
Thanks in advance,
Peter Strøiman