@Transactional(readOnly=true) in Spring

本文解释了在Spring框架中使用@Transactional(readOnly=true)注解的意义,特别是当与Oracle、PostgreSQL、MySQL+InnoDB等多版本并发控制数据库结合使用时,这种注解如何将读取操作转换为非标准隔离级别,并详细阐述了其工作原理及与其他数据库隔离级别的对比。

http://www.skill-guru.com/blog/2010/12/19/transactionalreadonlytrue-in-spring/

 

@Transactional(readOnly=true) in Spring

What does this mean ?

When a Multi Version Concurrency Control database (Oracle, Postgresql, MySQL + InnoDb) is used, a read only transaction can be translated to the non standard isolation level: READ_ONLY.

The READ_ONLY isolation level provides the same protection as the SERIALIZED isolation level (no dirty reads, no unrepeatable reads, no phantom reads) but doesn’t allow any updates. It also doesn’t cause any lock contention because no locking is required (the database is able to revert back to previous versions of the srecords ignoring all new changes).

Ms Sql 2005 also has a similar isolation level: SNAPSHOT.

If you specify readOnly as true, the flush mode will be set as FlushMode.NEVER in the current Hibernate Session preventing the session from committing the transaction.

Furthermore, setReadOnly(true) will be called on the JDBC Connection, which is also a hint to the underlying database. If your database supports it (most likely it does), this has basically the same effect as FlushMode.NEVER, but it’s stronger since you cannot even flush manually.

Now let’s see how transaction propagation works.

If you don’t explicitly set readOnly to true, you will have read/write transactions. Depending on the transaction attributes (like REQUIRES_NEW), sometimes your transaction is suspended at some point, a new one is started and eventually committed, and after that the first transaction is resumed.

If a method in a read/write transaction calls a method that requires a readOnly transaction, the first one should be suspended, because otherwise a flush/commit would happen at the end of the second method.

Conversely, if you call a method from within a readOnly transaction that requires read/write, again, the first one will be suspended, since it cannot be flushed/committed, and the second method needs that.

In the readOnly-to-readOnly, and the read/write-to-read/write cases the outer transaction doesn’t need to be suspended (unless you specify propagation otherwise, obviously).

转载于:https://www.cnblogs.com/silentjesse/p/5066540.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值