The order of closing transactions and connections?

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
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());
}
}
}
 
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

--
 
Back
Top