本文介绍了不使用事务的JPA行为不一致,传播SUPPORTS和OpenEntityManager模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Spring 4中使用JPA(Hibernate 4)来管理JTA事务。为了允许延迟初始化,即使在没有任何事务的情况下简单地从数据库中读取,我们也会添加OpenEntityManager模式。



你可以在GitHub上找到这些问题的测试用例

解决方案

请参阅:

http://docs.spring .IO /弹簧/文档/电流/ Javadoc的API /组织/ springframework的/ ORM / Hibernate3的/支持/ OpenSessionInViewFilter.html

(OpenEntityManagerInViewFilter应该以同样的方式工作)

lockquote

注意:此过滤器在默认情况下不会刷新休眠会话
并将刷新模式设置为FlushMode.NEVER。它假定用于
与处理
flush的服务层事务的组合:活动事务管理器将在读写事务期间临时将
刷新模式更改为FlushMode.AUTO,在每笔交易结束时将
冲洗模式重置为FlushMode.NEVER。 如果
打算在没有事务的情况下使用此过滤器,请考虑更改
默认刷新模式(通过flushMode属性)。

blockquote>

所以对于OEMIV和没有交易没有Flush,所以没有TransactionRequiredException。


We are using JPA (Hibernate 4) with Spring 4 managing the JTA transactions. To allow lazy initialization even when simply reading from the database without any transaction we added the "OpenEntityManager" pattern.

You can find a test case for these questions on GitHub https://github.com/abenneke/sandbox/tree/master/spring-hibernate4-transaction

We know that there is a difference between having no transaction synchronization at all and SUPPORTS synchronization. But the JPA behaviour seems to be inconsistent somehow:

  • If there is no transaction synchronization active, we get a TransactionRequiredException from Springs SharedEntityManagerCreator when trying to persist something using JPA.
  • If there is only a SUPPORTS transaction synchronization active, we don't get this exception and the persist request is silently ignored. This however is exactly the situation to avoid in the SharedEntityManagerCreator when creating the above exception.
  • If there is an "OpenEntityManager" pattern active, there is no exception also and the persist request is silently ignored as well.

In my opinion there is no real difference between these situations and I would expect to see this exception in all cases or none?

Thank you!

Update July 2015: I now raised this as an issue to Spring https://jira.spring.io/browse/SPR-13243

解决方案

See:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html

(OpenEntityManagerInViewFilter should work the same way)

So with OEMIV and no transaction there is no Flush and so, no TransactionRequiredException.

这篇关于不使用事务的JPA行为不一致,传播SUPPORTS和OpenEntityManager模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:48