1

There is a Spring batch job with one delete and many inserts within a transaction with isolation level READ_COMMITTED:

delete from TABLE_A;
insert into TABLE_A values (...);
insert into TABLE_A values (...);
...

However, when I tried to select from the DB TABLE_A while the batch job was still running (not yet committed), the select was not blocked by exclusive lock which I expected because there was a full table delete in the transaction. May I know if MariaDB is having some special locking mechanism?

1

1 Answer 1

2

It provided the data as if you looked at the table before the DELETE started. That's all your SELECT can see with READ_COMMITTED until the moment that the DELETE COMMITs. (No, you won't get a mixture of old data and new data.)

(Quoting Bill Karwin...)

Locking behavior is always up to the implementation. That is, both MySQL and Microsoft SQL Server are "special" in that their respective implementation is different. The developers have made choices about how to support concurrent access to data. MySQL's InnoDB storage engine was designed to behave similar to Oracle database, which uses Multiversion Concurrency Control (MVCC). In that design, writers do not block readers by default. The reader can read a copy of the data, even while the writer has updated or deleted it.

6
  • Is this behavior MariaDb / MySql DB specific? I saw from other documents that under READ_COMMITTED, other DB vendor will apply exclusive lock once delete is being executed and before transaction is committed.
    – bboy
    Commented Feb 27 at 13:32
  • @bboy Can you name a DB vendor that does that? Commented Feb 27 at 18:31
  • @BillKarwin, MS SQL Server. Details as below. Therefore, is MariaDB/MySql handling special? Official Link : With an intent exclusive (IX) lock, no other transactions can modify the same set of data; read operations can take place only with the use of the NOLOCK hint or read uncommitted isolation level.
    – bboy
    Commented Feb 28 at 11:04
  • By default, a DELETE statement always acquires an intent exclusive (IX) lock on the table object and pages it modifies, an exclusive (X) lock on the rows it modifies, and holds those locks until the transaction completes.
    – bboy
    Commented Feb 28 at 11:13
  • 1
    Locking behavior is always up to the implementation. That is, both MySQL and Microsoft SQL Server are "special" in that their respective implementation is different. The developers have made choices about how to support concurrent access to data. MySQL's InnoDB storage engine was designed to behave similar to Oracle database, which uses Multiversion Concurrency Control (MVCC). In that design, writers do not block readers by default. The reader can read a copy of the data, even while the writer has updated or deleted it. Commented Feb 28 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.