Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I commit or rollback a read transaction?

I have a read query that I execute within a transaction so that I can specify the isolation level. Once the query is complete, what should I do?

  • Commit the transaction
  • Rollback the transaction
  • Do nothing (which will cause the transaction to be rolled back at the end of the using block)

What are the implications of doing each?

using (IDbConnection connection = ConnectionFactory.CreateConnection()) {     using (IDbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted))     {         using (IDbCommand command = connection.CreateCommand())         {             command.Transaction = transaction;             command.CommandText = "SELECT * FROM SomeTable";             using (IDataReader reader = command.ExecuteReader())             {                 // Read the results             }         }          // To commit, or not to commit?     } } 

EDIT: The question is not if a transaction should be used or if there are other ways to set the transaction level. The question is if it makes any difference that a transaction that does not modify anything is committed or rolled back. Is there a performance difference? Does it affect other connections? Any other differences?

like image 482
Stefan Moser Avatar asked Nov 21 '08 19:11

Stefan Moser


People also ask

When should a transaction be rolled back?

ROLLBACK in SQL is a transactional control language that is used to undo the transactions that have not been saved in the database. The command is only been used to undo changes since the last COMMIT.

Is transaction should end with either commit or rollback?

A transaction ends when it is committed or rolled back, either explicitly (with a COMMIT or ROLLBACK statement) or implicitly (when a DDL statement is issued). To illustrate the concept of a transaction, consider a banking database.

Which one is faster commit or rollback?

As we know COMMIT operation save changes made in a transaction to the database while ROLLBACK undo those changes. Its observed, generally COMMIT is a faster process than a ROLLBACK operation.

What happens if you don't rollback a transaction?

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


2 Answers

You commit. Period. There's no other sensible alternative. If you started a transaction, you should close it. Committing releases any locks you may have had, and is equally sensible with ReadUncommitted or Serializable isolation levels. Relying on implicit rollback - while perhaps technically equivalent - is just poor form.

If that hasn't convinced you, just imagine the next guy who inserts an update statement in the middle of your code, and has to track down the implicit rollback that occurs and removes his data.

like image 98
Mark Brackett Avatar answered Sep 19 '22 03:09

Mark Brackett


If you haven't changed anything, then you can use either a COMMIT or a ROLLBACK. Either one will release any read locks you have acquired and since you haven't made any other changes, they will be equivalent.

like image 43
Graeme Perrow Avatar answered Sep 19 '22 03:09

Graeme Perrow