3

I'm trying to build an application in Java(JDK1.8) with Connector/J and MySql. I'm told that Serializable is the highest level, but it affects performance, so Serializable is not commonly adopted.

But consider this situation:

There are two commits which are going to update the fields of the same row (commit A and commit B). If A and B happens concurrently and the isolation level is not Serializable, there would be data races, which makes the fields inconsistent. But in Serializable level, the two updates won't happen at the same time, so either A happens before B or B happens before A, and the row will either be in version A or in version B, but not some mix of A and B.

I thought Atomicity of ACID guarantees the synchronization of A and B. But it seems that the definition of Atomicity only guarantees one transaction happens "all or nothing", it says nothing about the concurrent commits.

So should I use Serializable to prevent the data race? Which one of the ACID actually guarantees the atomicity of one update?

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
heruji
  • 33
  • 7
  • 1
    Seems like you are trying to learn RDBMS concepts only because you are having to write some JDBC code. IMHO, it will be a lot easier for you if you first try to learn these (transactions...) directly on MySQL first. You can use MySQL workbench or command line client, begin/commit multiple transactions, with different isolations. Then it will be easier to grasp what JDBC has to offer and what to choose/use. – Bhesh Gurung Jun 09 '18 at 15:48

1 Answers1

1

No. To avoid the problem you described you don't need "serializable". There are also other isolation levels. What you are afraid of can only happen with "read uncommitted". For any other isolation level the fields within a single record will always be consistent.

mentallurg
  • 4,967
  • 5
  • 28
  • 36