tags:

views:

895

answers:

1

does a transaction in a stored procedure do any locking to prevent others from updating tables?

also do i need to explicitly put in rollback logic or will the transaction automatically roll back if an error occurs because it never reaches the commit command.

A: 

Does a transaction in a stored procedure do any locking to prevent others from updating tables?

When you perform some DML on an InnoDB table, the rows affected get locked until the end of transaction (doesn't matter is it inside a stored procedure or not).

You can modify the locked rows inside the same transaction that locked it.

To explicitly lock some rows, issue:

SELECT  *
FROM    table
WHERE   condition
FOR UPDATE

Except for commiting or rolling back the transaction, there is no other way to unlock the rows

Do i need to explicitly put in rollback logic or will the transaction automatically roll back if an error occurs because it never reaches the commit command.

You'll need to perform a rollback explicitly.

Quassnoi
i guess what i'm asking is will the rows stay locked for the duration of the transaction. meaning if i have multiple updates will the lock be released after each update or only after all the updates are complete because they are in a transaction.
richs
The lock will retain until the end of the transaction, that's the whole point of locking :)
Quassnoi