简体   繁体   English

事务隔离和并发

[英]Transaction isolation and concurrency

I'm trying to build an application in Java(JDK1.8) with Connector/J and MySql. 我正在尝试使用Connector / J和MySql在Java(JDK1.8)中构建应用程序。 I'm told that Serializable is the highest level, but it affects performance, so Serializable is not commonly adopted. 我被告知Serializable是最高级别,但它会影响性能,因此Serializable并不常用。

But consider this situation: 但请考虑这种情况:

There are two commits which are going to update the fields of the same row (commit A and commit B). 有两个提交将更新同一行的字段(提交A和提交B)。 If A and B happens concurrently and the isolation level is not Serializable, there would be data races, which makes the fields inconsistent. 如果A和B同时发生且隔离级别不是Serializable,则会有数据争用,这会使字段不一致。 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. 但在Serializable级别,这两个更新不会同时发生,所以要么A发生在B之前,B发生在A之前,而行要么在版本A中,要么在版本B中,而不是A和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. 我认为ACID的Atomicity保证了A和B的同步。但似乎Atomicity的定义只保证一个事务“全有或全无”发生,它对并发提交没有任何说明。

So should I use Serializable to prevent the data race? 那么我应该使用Serializable来防止数据竞争? Which one of the ACID actually guarantees the atomicity of one update? 哪一个ACID实际上保证了一次更新的原子性?

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. 对于任何其他隔离级别,单个记录中的字段将始终保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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