![](/img/trans.png)
[英]Spring Hibernate JPA - No JTA TransactionSynchronizationRegistry
[英]Issues with finding objects queried with HQL or session filter in Hibernate 4 + Spring 4 + JTA
我们最近从Weblogic 10.3上的Hibernate 3.2.3,Spring 3.2.2迁移到了Hibernate 4.3.11和Spring 4.2.1。
现在在事务中,当创建休眠对象并将其添加到休眠对象的持久性集合中,然后又使用HQL(使用hibernateTemplate)或Hibernate的“查询”功能(在同一事务中且在事务结束之前)进行查询时,Hibernate无法找到添加的对象。 在升级之前,它可以与Hibernate 3和Spring 3一起使用,但是现在失败了。
例如,在Psuedo代码中,说我有一个具有以下属性的库类。
class Library{
private Collection<Books> books;
}
在交易中,我执行以下操作-
...
Book book1 = new Book();
book1.setAuthor("Patrick Holt");
library.getBooks().add(book1);
然后在同一事务中,将Hibernate的“查询”与过滤器配合使用,以查找作者的书籍,例如
Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
Query q = s.createFilter(library.getBooks(), "where this.author = :authorName");
q.setParameter("authorName", "Patrick Holt");
List l = q.list();
上例中的q.list()返回0个结果。 升级前将返回1个结果。 升级后,我得到0个结果。
我了解了在升级到Hibernate 4和Spring 4时currentSessionContext中的一些更改,但是我不确定在不更改代码的情况下需要进行哪些更改才能使行为像升级之前一样。
休眠对象在hbm.xml文件中定义,这是我对会话工厂的配置。
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<!--
Tried adding the following properties, but it didn't work either
<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop>-->
</props>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="mappingJarLocations">
<list>
<value>/WEB-INF/lib/app-1.0.jar</value>
</list>
</property>
</bean>
交易管理器定义:
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"/>
数据源可通过jndi使用。
关于解决此问题我需要做什么的任何想法?
我遇到的问题与Spring Jira Issue SPR-13848中描述的完全一样。 据我了解,似乎OpenSessionInViewFilter提早打开了一个请求范围的会话,并且与稍后启动的事务同步存在问题。
我无法摆脱OSIV,因为它会在整个应用程序中造成影响。 但是,我发现休眠状态具有可以完成类似功能以防止延迟加载的属性-
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
这解决了问题。 添加此属性使我可以删除OSIV,并且休眠的AUTO刷新模式现在可以在事务中按预期工作。 我还使用了CMTTransactionFactory。 我所有的休眠属性:
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
</props>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.