简体   繁体   中英

Transaction isolation and concurrency

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM