baash05
baash05

Reputation: 4516

SQL begin transaction with no commit

If I begin a transaction, but never get to call COMMIT. What happens to the data? I have a smallish database (say a million or so SKU's).. I am exporting it in small even numbered chunks of 1024.. (third party constraints limit my file size).
I have to flag the records that have been exported..

eg. Update products set exported = 1 where sku = '1234';

Now every once and a while I have a problem that crashes the third party file writing tool. But this happens before the file for the given record was created.

So I was thinking if I call begin transaction before I update the records, and commit only after I've confirmed the file was built.

This may result in a few begin transactions that don't have their twin.

So two questions.. Is there a better way? (apart from getting rid of the buggy third party) Or what happens to records that were part of a transaction that was never committed?

Upvotes: 3

Views: 2903

Answers (1)

gbn
gbn

Reputation: 432672

Your transactions stay open with the locks until the connection is fully closed (not just returning to the connection pool). This is bad

To do an UPDATE without an explicit transaction and manage 1024-row chunks, do something like this

UPDATE TOP (1024)
   Products
SET
   exported = 1
OUTPUT
   INSERTED.*
WHERE
   exported = 0;

You can modify this to use a status column that has "Processing" "Exported" etc so you know when stuff was read but not exported

Upvotes: 5

Related Questions