简体   繁体   English

为每笔交易设置隔离级别

[英]isolation level set on every transaction

When we set isolation level in @Transactional annotation, then Isolation level is set on Connection attribute, 当我们在@Transactional批注中设置隔离级别时,则在Connection属性上设置了隔离级别,

so when the transaction is began then it must be firing SET TRANSACTION ISOLATION LEVEL READ COMMITTED query. 因此,当事务开始时,它必须触发SET TRANSACTION ISOLATION LEVEL READ COMMITTED查询。 (This is my assumption) (这是我的假设)

So does it fires this query on every Transaction? 那么它是否在每个事务上都触发该查询?

does it requires to set transaction isolation on every transaction is spring? 是否需要在春季的每个事务上都设置事务隔离?

Spring will run the statement to change the isolation only if you have set an isolation level in the annotation (obvious) and the isolation level is not the same one as the one currently set on the connection. 只有在注释中设置了隔离级别(显而易见)且隔离级别与连接上当前设置的隔离级别不同时,Spring才会运行该语句来更改隔离。 At the end of the transaction, Spring will revert this and set the previous isolation level. 在事务结束时,Spring将还原该值并设置先前的隔离级别。 Also, I think you get a nice exception if you mix isolation levels, but I cannot find that in the code now. 另外,我认为如果混合使用隔离级别,您会得到一个很好的例外,但是现在我无法在代码中找到它。

By default, oracle's connection isolation level is set to read committed, so you don't need to specify the isolation level unless you have set the default to serializable... and if you set it, then no damage done, as spring won't do anything. 默认情况下,oracle的连接隔离级别设置为已读,因此除非您将默认值设置为可序列化...,否则无需指定隔离级别。如果设置了默认值,则不会造成破坏,因为spring不会什么都不要做。

All of this is in the AbstractPlatformTransactionManager , DataSourceTransactionManager and DataSourceUtils if you fancy looking under the hood :). 如果您想在幕后:),所有这些都在AbstractPlatformTransactionManagerDataSourceTransactionManagerDataSourceUtils

Depends which db you are using default isolation level can be different. 取决于您使用默认隔离级别的数据库可以不同。 For instance my local mySQL and Google Cloud SQL have REPEATABLE-READ. 例如,我的本地mySQL和Google Cloud SQL具有REPEATABLE-READ。 To check these settings I used: 要检查这些设置,我使用了:

SHOW VARIABLES WHERE Variable_name ='tx_isolation'

does it requires to set transaction isolation on every transaction is spring? 是否需要在春季的每个事务上都设置事务隔离?

No. To change REPEATABLE-READ to READ COMMITTED on every transaction without touching @Tansactional annotations I added this to my persistence.xml 不需要。要在每个事务上将REPEATABLE-READ更改为READ COMMITTED而不接触@Tansactional批注,请将其添加到persistence.xml中

<property name="hibernate.connection.isolation">2</property>

Where 哪里

1: READ UNCOMMITTED
2: READ COMMITTED
4: REPEATABLE READ
8: SERIALIZABLE

Now, at the beginning of every new transaction hibernate does 现在,在每个新事务开始时,hibernate都会执行

SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED

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

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