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根本就不支持NestedTransaction,最新的Hibernate4也是如此。在配置文件中设置"nestedTransactionAllowed=true",其实只对JDBC事务起效(如使用Spring的JdbcTemplate)。

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

参考:

http://cowbane.iteye.com/blog/504270

解惑spring嵌套事务

http://www.iteye.com/topic/482984

SpringNested事务之再解惑

http://forum.springsource.org/showthread.php?62656-Unable-to-roll-back-when-transaction-propagation-quot-NESTED-quot

Unabletorollbackwhentransactionpropagation="NESTED"

http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/orm/hibernate3/HibernateTransactionManager.html

org.springframework.orm.hibernate3.HibernateTransactionManager

相关推荐