Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Committing a transaction early, within a transaction ? #1632

Closed
khkiley opened this issue Apr 13, 2024 · 4 comments
Closed

Committing a transaction early, within a transaction ? #1632

khkiley opened this issue Apr 13, 2024 · 4 comments

Comments

@khkiley
Copy link

khkiley commented Apr 13, 2024

For similar reasons to #1517, I need to acquire a single connection.

I have a large number of inbound streaming records I need to upsert, and would like to commit them at intervals so if something fails along the way (like on record 999999 of 1000000) the process doesn't need to start from scratch.

Effectively, I'm looking to do something like this:

const transaction = new sql.Transaction(/* [pool] */)
await transaction.begin()

request = new Request(transaction)

await request.query('create the #tmp table')
await request.query('COMMIT TRAN')
await request.query('OPEN TRAN')
await request.query('Insert some stufff into #tmp table')
await request.query('Upsert stuff from #tmp table to destination table')
await request.query('COMMIT TRAN')
await request.query('OPEN TRAN')
await request.query('Insert some more stufff into #tmp table')
await request.query('Upsert more stuff from #tmp table to destination table')
await request.query('COMMIT TRAN')
await request.query('OPEN TRAN')
await request.query('drop the #tmp table')
await request.query('COMMIT TRAN')
await request.query('OPEN TRAN')

await transaction.commit()

I'm assuming hoping, the connection remains steading through all those manual commit/opens, and as long as there is an open transaction when I issue the transaction.commit(), everything should be ok.

What could go wrong with this approach ?

Thanks,

Kurt

@dhensby
Copy link
Collaborator

dhensby commented Apr 13, 2024

What a novel approach, yep - that should work. I can't think of any immediate issues with doing that.

@khkiley
Copy link
Author

khkiley commented Apr 14, 2024

@dhensby Excellent! Thank you for the fast reply.

Does the connection remain with the transaction after an error? I would need to clean up and drop the temporary table.

Or is there a level of cleanup that naturally happens when a connection is returned, and reissued from the pool?

Thanks,

Kurt

@dhensby
Copy link
Collaborator

dhensby commented Apr 14, 2024

The connection will remain until commit or rollback is called unless you've manually set automatic rollback on error on the connection.

@khkiley
Copy link
Author

khkiley commented May 6, 2024

This looks like it is working well!

@khkiley khkiley closed this as completed May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants