I have managed to solve this issue.
The answer was somewhat in the direction of Akina's Answer. But not quite exactly.
The way I solved it did indeed involved an additional table but not like the way He suggested.
I created an additional table to store meta data about transactions.
Eg: I had table_key like this
CREATE TABLE `journals` ( `id` bigint NOT NULL AUTO_INCREMENT, `type` smallint NOT NULL DEFAULT '0', `trans_no` bigint NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `transaction` (`type`,`trans_no`))
So I created a meta_journals table like this
CREATE TABLE `meta_journals` ( `type` smallint NOT NULL, `next_trans_no` bigint NOT NULL, PRIMARY KEY (`type`),)
and seeded it with all the different types of journals and the next sequence number.
And whenever I insert a new transaction to the journals
I made sure to increment the next_trans_no
of the corresponding type
in the meta_transactions
table. This increment operation is issued inside the same database TRANSACTION
, i.e. inside the BEGIN
AND COMMIT
This allowed me to use the exclusive lock acquired by the UPDATE
statement on the row of meta_journals
table. So when two insert statement is issued for the journal concurrently, One had to wait until the lock acquired by the other transaction is released by COMMIT
ing.