HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to perform server-side transaction timeout if COMMIT TRANSACTION will never arrive?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
nevercommitarrivesidetimeoutperformwilltransactionhowserver

Problem

How can SQL Server terminate one of its transactions if it exceeds preset timeout?

Imagine the following transaction performed by traveling salesman's SQL client application, which is submitted command-by-command (i.e. not as single batch) and normally takes 2 seconds:

BEGIN TRANSACTION
    INSERT INTO PurchaseOrder 
    INSERT INTO PurchaseOrderLineItem 
    INSERT INTO PurchaseOrderLineItem 
    INSERT INTO PurchaseOrderLineItem 
          :
    INSERT INTO PurchaseOrderLineItem 
COMMIT TRANSACTION


Now, if mobile connection permanently drops1 during sending line item 51 (thus dropping the link to SQL server etc.), ABORT command will never arrive from client side. How can SQL Server time out and rollback the transaction when client which opened the transaction is permanently lost? (Condition example: no new command arrived for 30 seconds in that transaction.)

The problem I'm trying to avoid is that all other SQL commands accessing PurchaseOrderLineItem table will wait infinitely for the one described above. How to clean way for other queued commands? (If the client application is unable to submit entire transaction batch at once.)

I can see advices to set client timeout but they are of no help if client is permanently lost.

__

1) other possible accidents leading to problem: application crashes and exits without cleanup, PC goes to blue screen of death, laptop runs out-of-battery, network device stops responding etc...

Solution

The underlying OS is capable of detecting all the above mentioned possible disconnects and break the communication channel (TCP, net pipe etc). This will result in the rollback. Don't try to outsmart this.

Of course, one could ask why not

  • use a reliable queueing communication channel (eg. MSMQ)



  • submit the entire order in one call rather than 100 calls



  • commit the order line by line in 'pending' state



Any of the above would work. 30 second transactions would not work.

Context

StackExchange Database Administrators Q#94292, answer score: 3

Revisions (0)

No revisions yet.