Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

On dev mysql LOCK IN SHARE MODE is described as...

SELECT ... LOCK IN SHARE MODE sets a shared mode lock on the rows read. A shared mode lock enables other sessions to read the rows but not to modify them. The rows read are the latest available, so if they belong to another transaction that has not yet committed, the read blocks until that transaction ends.

Does that mean that if I simply call

$dbc -> beginTransaction();

And either commit or rollback then the data read will be the latest given? Because it also says that using FOR UPDATE will block LOCK IN SHARE MODE from reading (ie make sure it is the latest info).

But if I wrap all my important updates and deletes in a transaction then is there still a need to lock rows, as above it says LOCK IN SHARE MODE will wait for a transaction to finish? Surely if it waits for transactions to finish then essentially I will always have the freshest data available by just using transactions and not having to lock when performing updates and deletes?

share|improve this question

migrated from stackoverflow.com Jan 2 '12 at 12:29

This question came from our site for professional and enthusiast programmers.

    
What's the exact problem you're trying to solve here? Transactions are (usually) isolated from each other. If there's a specific case when you want to see the data from another (uncommitted) transaction - you can use SELECT LOCK IN SHARE MODE. If it's a case of you starting a transaction in piece of PHP code and then using commit or rollback - actions are performed on that transaction, not some random "latest" one. –  N.B. Jan 2 '12 at 10:54

1 Answer 1

Performing SELECT...LOCK IN SHARE MODE can block UPDATE...LOCK IN SHARED MODE. Deadlocks can still occur. Needed rows for SELECT or UPDATE can still introduce locks you never intended to issue. What do I mean ?

It is sometimes necessary to internally lock primary key entries for your own good.

I once made three posts helping a developer realize this : Are InnoDB Deadlocks exclusive to INSERT/UPDATE/DELETE?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.