Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to an uncommitted transaction when the connection is closed?

Are they rolled back immediately? Are they rolled back after some period of time? Are they left in an uncommitted state?

Is the behavior the same if connection pooling is used and the connections are simply reset?

like image 565
JohnnyM Avatar asked Oct 08 '09 18:10

JohnnyM


People also ask

What happens if a transaction is not committed?

As long as you don't COMMIT or ROLLBACK a transaction, it's still "running" and potentially holding locks. If your client (application or user) closes the connection to the database before committing, any still running transactions will be rolled back and terminated.

What happens uncommitted transaction Oracle?

A user disconnects from Oracle Database. The current transaction is committed. A user process terminates abnormally. The current transaction is rolled back.

Does transaction commit close the connection?

By default, in all app server and oracle db, autocommit is true. No matter you use connection pooling or direct connection[DriverManager] since commit is the operation on connection object it doesn't matter.

How do you check if there are uncommitted transactions in Oracle?

The easiest way is to check if there are any transactions is select * from gv$transaction. This query shows any open transactions in your Oracle system.


1 Answers

It can stay open while connection pooling applies. Example: command timeout can leave locks and TXN because the client sends as "abort".

2 solutions:

  • Test in the client, literally:

    IF @@TRANCOUNT <> 0 ROLLBACK TRAN

  • Use SET XACT_ABORT ON to ensured a TXN is cleaned up: Question 1 and Question 2

I always use SET XACT_ABORT ON.

From this SQL Team blog:

Note that with connection pooling, simply closing the connection without a rollback will only return the connection to the pool and the transaction will remain open until later reused or removed from the pool. This can result in locks begin held unnecessary and cause other timeouts and rolling block

From MSDN, section "Transaction Support" (my bold)

When a connection is closed, it is released back into the pool and into the appropriate subdivision based on its transaction context. Therefore, you can close the connection without generating an error, even though a distributed transaction is still pending. This allows you to commit or abort the distributed transaction at a later time.

like image 189
gbn Avatar answered Sep 28 '22 01:09

gbn