Martin Blore
Martin Blore

Reputation: 2195

MySQL Transaction - Reading during Transaction

If I start a transaction in MySQL and update a row like so:

UPDATE table SET SomeValue ...... WHERE Id = 1;

Am I ok to immediately do this:

SELECT SomeValue FROM table WHERE Id = 1;

...during the same connection/session whilst the transaction is still not yet committed, assuming now that I'm getting the updated SomeValue, and safe to assume that nothing can update that SomeValue until I've committed the transaction?

Thanks.

Upvotes: 1

Views: 876

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562310

No other session can update that row until you commit.

You can test this for yourself, by opening two terminal windows, run the mysql shell, and try starting a transaction and updating in one window, but do not commit. Then try updating in the second window. It hangs, until the first window commits, or the innodb lock wait timeout is reached.

SELECTing from the table has no effect on locks one way or the other.

Upvotes: 2

Related Questions