Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Consider the following code which does not rollback the transaction if an exception is caught.

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 rollback the transaction?

like image 832
Craig Johnston Avatar asked Mar 08 '11 08:03

Craig Johnston


People also ask

What happens if you don't rollback a transaction?

If you neither commit nor rollback the transaction, the transaction will continue to exist indefinitely.

Does a transaction automatically rollback?

Transactions in the SQL server are rollbacked automatically. However, with the rollback SQL statement, you can manually rollback a transaction based on certain conditions. In this article, you will see what a transaction is and how it can be rollbacked both manually and automatically.

What transactions should be rollback?

You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction. This does not include changes made to local variables or table variables.

When would you implement a rollback transaction?

4. ROLLBACK: If any error occurs with any of the SQL grouped statements, all changes need to be aborted. The process of reversing changes is called rollback. This command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.


2 Answers

The best is to generate your transaction inside a using block like this:

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

If you code fails before the call to commit, it will automatically be rolled back as the using block is exited.

like image 57
Øyvind Bråthen Avatar answered Oct 11 '22 17:10

Øyvind Bråthen


It will leave an open transaction on the database, which could potential block other queries.

Taken from here:

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

  • Always access tables in the same order across transactions in your application. The likelihood of a deadlock increases when you access tables in a different order each time you access them.

  • Keep transactions as short as possible. Do not make blocking or long-running calls from a transaction. Keep the duration of the transactions short. One approach is to run transactions close to the data source. For example, run a transaction from a stored procedure instead of running the transaction from a different computer.

  • Choose a level of isolation that balances concurrency and data integrity. The highest isolation level, serializable, reduces concurrency and provides the highest level of data integrity. The lowest isolation level, read uncommitted, gives the opposite result.

like image 22
Neil Knight Avatar answered Oct 11 '22 17:10

Neil Knight