I want to lock a table row so that any other reading operations on that row will wait until the lock is released, and I want to avoid to have a lock on the entire table.
I have the following table (InnoDB)
CREATE TABLE account_balance (
account INTEGER UNIQUE,
balance INTEGER NOT NULL,
PRIMARY KEY (account)
);
I execute the following transaction to obtain a lock on a row
START TRANSACTION;
SELECT balance FROM account_balance WHERE account = 1 FOR UPDATE;
SELECT SLEEP(10);
COMMIT;
And I would like the following query to wait for the lock to be released
SELECT balance FROM account_balance WHERE account = 1;
To do it, the only way I found is to run the SELECT as follow
SET autocommit = 0; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SELECT ....;
It will wait for the lock to be released but I should prefix every SELECT
with autocommit and isolation level settings for this table.
Is there a way to configure autocommit=0
and transaction-isolation = SERIALIZABLE
ONLY at table level?
I know that I could set
[mysqld]
transaction-isolation = SERIALIZABLE
autocommit = 0
in my.cnf
, but I do not want to affect other operations done on other tables and schemas.
refs: