views:

141

answers:

2

Is there any way that I can make sure that a stored procedure completely finishes before another instance of it is started?

I have to do 3 things in the procedure and if two instances are running at the same time it will mess up a boundary case.

Example: Count rows, if < X insert a row, return calculated Y

if multiple instances of the stored proc can run at the same time I could go past my X target. Chance is small, but its there.

DB we're using is MYSQL5, but also wondering for MSSQL

+4  A: 

Use transactions. Check row count afterwards and roll back if it's gone over.

Anon.
I don't see how this would solve my problem. Would this not also leave the ability for both now to roll back at the same time? I'm probably missing something here.
dilbert789
A: 

You can try leveraging locking reads to allow concurrency to ensure that you're operating as a singleton.

NOTE: This will block all other calls and they will execute serially. Make sure this is the behavior you're after.

scwagner