![](/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.