问题描述
我们在Spring 4中使用JPA(Hibernate 4)来管理JTA事务。为了允许延迟初始化,即使在没有任何事务的情况下简单地从数据库中读取,我们也会添加OpenEntityManager模式。
你可以在GitHub上找到这些问题的测试用例
请参阅:
http://docs.spring .IO /弹簧/文档/电流/ Javadoc的API /组织/ springframework的/ ORM / Hibernate3的/支持/ OpenSessionInViewFilter.html
(OpenEntityManagerInViewFilter应该以同样的方式工作)
lockquote
所以对于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:
(OpenEntityManagerInViewFilter should work the same way)
So with OEMIV and no transaction there is no Flush and so, no TransactionRequiredException.
这篇关于不使用事务的JPA行为不一致,传播SUPPORTS和OpenEntityManager模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!