Hibernate不支持Nested Transaction

Spring-Orm中HibernateTransactionManager类文档中均有一段话:
引用
On JDBC 3.0, this transaction manager supports nested transactions via JDBC 3.0 Savepoints. The AbstractPlatformTransactionManager.setNestedTransactionAllowed(boolean) "nestedTransactionAllowed"} flag defaults to "false", though, as nested transactions will just apply to the JDBC Connection, not to the Hibernate Session and its cached objects. You can manually set the flag to "true" if you want to use nested transactions for JDBC access code which participates in Hibernate transactions (provided that your JDBC driver supports Savepoints). Note that Hibernate itself does not support nested transactions! Hence, do not expect Hibernate access code to semantically participate in a nested transaction.

也就是说,Hibernate根本就不支持Nested Transaction,最新的Hibernate4也是如此。在配置文件中设置"nestedTransactionAllowed=true",其实只对JDBC事务起效(如使用Spring的JdbcTemplate)。

经过测试,Hibernate框架托管下的Nested Transaction方法(子事务)抛出异常时,Spring日志会提示Rolling back transaction to savepoint,不过所谓“回滚”的是使用JDBC操作的内容,如果事务中没有JDBC操作等于是没有效果的。子事务操作会被保存到Hibernate的缓存中,并在下一次flush时被提交。

参考代码:http://www.iteye.com/problems/81929

参考:
http://cowbane.iteye.com/blog/504270
解惑 spring 嵌套事务
http://www.iteye.com/topic/482984
Spring Nested 事务之再解惑
http://forum.springsource.org/showthread.php?62656-Unable-to-roll-back-when-transaction-propagation-quot-NESTED-quot
Unable to roll back when transaction propagation="NESTED"
http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/orm/hibernate3/HibernateTransactionManager.html
org.springframework.orm.hibernate3.HibernateTransactionManager

猜你喜欢

转载自xiaoyaozjl.iteye.com/blog/1496812