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).

最新文章

  1. Vertica集群扩容实验过程记录
  2. [nRF51822] 12、基础实验代码解析大全 · 实验19 - PWM
  3. 【WPF系列】基础学习-WPF架构概览
  4. ue4 UE4Editor.lib找不到
  5. STL之迭代器
  6. unity3d遍历出Cube里面所有子对象
  7. xml_02
  8. Android屏幕相关设置
  9. stm32中断学习总结
  10. 广义后缀树(GST)算法的简介
  11. Xp根据数据库insert获取微信聊天记录
  12. NOIP2017感悟
  13. 【java】:多线程面试题
  14. IDEA引入spring的命名空间
  15. codeforces 777C
  16. IDEA进行远程调试
  17. try catch和spring事务
  18. linux 查看磁盘读写:iotop
  19. Appium -选择、操作元素4
  20. Java基础-SSM之mybatis多对多关联

热门文章

  1. C++泛型编程(2)--通过排序和查找元素理解迭代器
  2. 《JavaScript-The Definitive Guide》读书笔记:函数定义和函数调用
  3. 读书笔记《疯狂人类进化史》,第五章,关于xing ai这件事
  4. [Python设计模式] 第11章 迪米特法则——最少知识原则
  5. shell相关知识点
  6. MySql之查询基础与进阶
  7. LeetCode Permutations问题详解
  8. zabbix 创建主机、主机群组、监控第一台服务器
  9. 【BZOJ3585】mex
  10. RandomAccessFile类理解