Is there a way in SQL Server 2008 R2 to cause a timeout for a database modification involving a transaction? We have a scenario where our application code hangs or throws an exception and fails to perform a rollback or commit. This then causes other sessions to hang waiting for the transaction to complete.
Sql-server – SQL Server Transaction Timeout
lockingsql-server-2008-r2transaction
Related Question
- SQL Server Table Locking – How to Prevent Locking Entire Table on Insert or Update?
- Sybase – Non-Blocking Transaction Read Like Oracle and SQL Server 2014
- Sql-server – Sleeping sessions with an open transaction but empty input buffer
- Oracle Database troubleshooting enq: TX – row lock contention
- Mysql – SQL: How to you release or prevent update/insert locks while remaining in the transaction
- PostgreSQL – Handling Transactions Without Rollback on Error
- MySQL 8.0 – Metadata Lock Extension for Foreign Keys Causing Requests to Hang
Best Answer
Extending Mark's answer...
When a client timeout event occurs (.net CommandTimeout for example), the client sends an "ABORT" to SQL Server. SQL Server then simply abandons the query processing. No transaction is rolled back, no locks are released.
Now, the connection is returned to the connection pool, so it isn't closed on SQL Server. If this ever happens (via KILL or client reboot etc) then the transactions+locks will be cleared. Note that sp_reset_connection won't or doesn't clear them, even though it is advertised to do so
This detritus from the abort will block other processes.
The way to make SQL Server clear transactions+locks on client timeout (strictly, ABORT events) is to use SET XACT_ABORT ON.
You can verify this be opening 2 query windows in SSMS:
Window 1:
In menu Query..Query Options set a timeout of 5 seconds then run this
Window 2, this will wait forever (or hit your timeout)
SET XACT_ABORT ON has interesting side effects too:
The combination of this means that you can't use SAVEPOINTS (although, I can't recall exact behaviour) for partial commits/rollbacks. Which suits me
SO links on SET XACT_ABORT:
On nested stored procs:
On sp_reset_connection: