I have a table with columns: * ID - primary key * DATA - some data I want to select * LOCKED - 0 or 1 (1 if row was read)
I want to create sql mechanism to get every row only one times but many users trying to read from this table at the same time.
Currently, body of my stored produdure looks something like that:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
PREPARE stmt FROM @findQuery; << select row with LOCKED = 0
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM @updateQuery; << set LOCKED = 1
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
COMMIT;
This solution works, but for big load I catch deadlocks exceptions. Is there any better (faster) solution?
I work with Springboot and JDBCTemplate.