Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to call rollback if I never commit?

Tags:

I am connecting to a SQL Server using no autocommit. If everything is successful, I call commit. Otherwise, I just exit. Do I need to explicitly call rollback, or will it be rolled back automatically when we close the connection without committing?

In case it matters, I'm executing the SQL commands from within proc sql in SAS.

UPDATE: It looks like SAS may call commit automatically at the end of the proc sql block if rollback is not called. So in this case, rollback would be more than good practice; it would be necessary.

Final Update: We ended up switching to a new system, which seems to me to behave the opposite of our previous one. On ending the transaction without specifying committing or rolling back, it will roll back. So, the advice given below is definitely correct: always explicitly commit or rollback.

like image 979
Derek Avatar asked Nov 01 '11 19:11

Derek


People also ask

Can we rollback without commit?

On ending the transaction without specifying committing or rolling back, it will roll back. So, the advice given below is definitely correct: always explicitly commit or rollback.

What happens if you don't commit a transaction?

If you don't commit, then your transaction will remain OPEN indefinitely - which you can see in sys. dm_tran_active_transactions / sys. dm_tran_database_transactions / sys.

Can rollback be done after commit?

After you commit the transaction, the changes are visible to other users' statements that execute after the commit. You can roll back (undo) any changes made during the transaction with the ROLLBACK statement (see ROLLBACK.

What happens if we don't commit in Oracle?

Oracle recommends that you explicitly end transactions in application programs using either a COMMIT or ROLLBACK statement. If you do not explicitly commit the transaction and the program terminates abnormally, then Oracle Database rolls back the last uncommitted transaction.


1 Answers

It should roll back on close of connection. Emphasis on should for a reason :-)

Proper transaction and error handling should have you always commit when the conditions for commit are met and rollback when they aren't. I think it is a great habit to always commit or rollback when done and not rely on disconnect/etc. All it takes is one mistake or incorrectly/not closed session to create a blocking chain nightmare for all :-)

like image 69
Mike Walsh Avatar answered Nov 25 '22 12:11

Mike Walsh