The order of closing transactions and connections?

F

Frank Rizzo

When I commit a transaction, what should be the order of closing
resources? Transaction first, and then the Connection? Or vice versa?
Or doesn't matter? Currently I have this. Is this correct?

public void CommitTransaction()
{
CheckTransactionState(true);
try
{
_transaction.Commit();
}
finally
{
CloseTransaction();
CloseConnection();
}
}

private void CloseConnection()
{
if (_connection != null)
{
try
{
_connection.Close();
_connection.Dispose();
_connection = null;
}
catch (Exception ex)
{
Log.AddToLog(ex.ToString());
}
}
}
private void CloseTransaction()
{
if (_transaction != null)
{
try
{
_transaction.Dispose();
_transaction = null;
}
catch (Exception ex)
{
Log.AddToLog(ex.ToString());
}
}
}
 
F

Frans Bouma [C# MVP]

Frank said:
When I commit a transaction, what should be the order of closing
resources? Transaction first, and then the Connection? Or vice versa?
Or doesn't matter? Currently I have this. Is this correct?

public void CommitTransaction()
{
CheckTransactionState(true);
try
{
_transaction.Commit();
}
finally
{
CloseTransaction();

not necessary. Commit / Rollback end the transaction. After that you
just clean up the connection as you do in your CloseConnection method.

Frans

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top