ADO.NET: Do you need to cancel a transaction?

Consider the following code that does not roll back a transaction if an exception is detected.

transaction = connection.BeginTransaction();
command.Transaction = transaction;
try {
    // interact with database here
catch {}
finally {
    connection.Close();
}

What are the consequences of this and is it necessary to roll back a transaction?

+5
source share
2 answers

It will leave an open transaction in the database, which could potentially block other requests.

Taken from here :

Consider the following general guidelines when using transactions so that you can avoid deadlocks:

  • Always refer to tables in the same order between transactions in your expression. The likelihood of a deadlock increases when you access the table in a different order each time you access them.

  • . . . , . , .

  • , concurrency . , , concurrency . , , .

+2

:

using( /*code to create the transaction you want )
{
  //perform your transaction here
  transaction.Commit();
}

, .

+14

All Articles