2015-11-04 63 views
0

我正在遷移到spring 4.2.2.RELEASE和hibernate 5並獲得以下異常:遷移到hibernate 4 + spring 4.2.2:org.hibernate.HibernateException:無法獲取當前線程的事務同步會話

異常也發生在休眠4:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'NotificationService' defined in class path resource [business-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: 
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'lovDao' threw exception; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1518) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    ... 127 more 
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: 
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'lovDao' threw exception; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121) 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514) 
    ... 135 more 
:org.springframework.beans.PropertyBatchUpdateException:Failed properties: Property 'lovDao' threw exception; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121) 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    Truncated. see log file for complete stacktrace 

這是 'lovDao' bean的定義:在DAO-context.xml中的

<bean id="ListOfValuesDao" class="be.fgov.just.cjr.dao.core.ListOfValuesDaoImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

和相關部分:

<!-- Enable the configuration of transactional behavior based on annotations --> 
<bean id="transactionManager" name="transactionManager" 
     class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

<!-- Enable the configuration of transactional behavior based on annotations --> 
<tx:annotation-driven transaction-manager="transactionManager"/> 

<context:component-scan base-package="be.fgov.just.cjr.dao" /> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="datasource"/> 
    <property name="packagesToScan" value="be.fgov.just.cjr.model"/> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop> 
      <prop key="hibernate.dialect">be.fgov.just.cjr.oracle.OracleDialectWithXmlTypeSupport</prop> 
      <prop key="hibernate.search.autoregister_listeners">false</prop> 
      <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop> 
      <prop key="hibernate.use_sql_comments">true</prop> 
      <prop key="hibernate.generate_statistics">true</prop> 
      <prop key="hibernate.cache.use_structured_entries">true</prop> 
      <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 
      <prop key="hibernate.cache.use_query_cache">true</prop> 
      <prop key="hibernate.cache.use_second_level_cache">true</prop> 
      <prop key="hibernate.show_sql">false</prop> 
     </props> 
    </property> 
</bean> 

現在,這是lovDao的類定義:

@Transactional 
//@Repository removed for testing (didn't work) 
public class ListOfValuesDaoImpl extends GenericDaoImpl implements ListOfValuesDao { 

也GenericDaoImpl是事務

@Transactional 
//@Repository removed for testing (didn't work) 
public class GenericDaoImpl implements GenericDao { 

爲什麼不lovDao無法獲得交易 - 同步會話?

ListOfValuesDaoImpl起初不是Transactional,但拋出了完全相同的異常; GenericDaoImpl是。

也GenericDao是事務性的:(不再)

//@Transactional removed for testing (didn't work) 
//@Repository removed for testing (didn't work) 
public interface GenericDao { 

我知道,類似的問題問了一圈左右,但我已經嘗試過相當長的一段指針和閱讀相當一些導遊,但似乎沒有任何工作...

感謝指針/幫助/批評意見;)

S.

編輯:

SessionFactory則是在GenericDaoImpl設置...:

private SessionFactory sessionFactory; 

@Autowired 
public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
} 

(我加了@Qualifier( 「SessionFactory的」),因爲的IntelliJ抱怨多豆(這實際上指向同一個bean)) - >我查了一下道context.xml中是否多次使用,但它不是

...和(在同一類)獲得這樣的會議:

public Session getSession() { 
    return sessionFactory.getCurrentSession(); 
} 

編輯2(對e GUISSOUMA伊薩姆的答案)的DIT:

我也有豆像這樣的:

<bean id="DossierDao" class="be.fgov.just.cjr.dao.dossier.DossierDaoImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
    <property name="lovDao" ref="ListOfValuesDao"/> 
    <property name="dossierValidator" ref="DossierValidator"/> 
</bean> 

我該怎麼跟那些做什麼?

(他們引用ListOfValuesDao ...) (@Autowiring似乎並沒有幫助(根據的IntelliJ)它沒有找到一個參照ListOfValuesDao)

- >相同的異常也發生在我刪除:

<context:component-scan base-package="be.fgov.just.cjr.dao" /> 

PS :感謝你們堅持幫助我,問題是我現在無法測試出東西,因爲我從家裏寫下這些東西(並且無法到達辦公室的環境:( - >如果你給我幾個指針,我可以明天嘗試一下,但我現在只能依靠IDE的輸出:( - >我會盡量給出儘可能多的信息

我也有這個在GenericDaoImpl:

private SessionFactory sessionFactory; 

@Autowired 
public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
} 

結合:

<bean id="GenericDao" class="be.fgov.just.cjr.dao.GenericDaoImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

基礎上,我想這可能會導致問題的答案的編輯? (我只能檢查確保明天早上...)

編輯3:我已經得到了相當長的一段新的信息,而不是在這裏附加這一切我created a new question ...

回答

1

添加@Repository註解你的DAO。

從XML刪除該聲明

<bean id="ListOfValuesDao" class="be.fgov.just.cjr.dao.core.ListOfValuesDaoImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

,因爲你有這樣的:

<context:component-scan base-package="be.fgov.just.cjr.dao" /> 

確保你的包被掃描一次(在一個XML文件中聲明的)

+0

沒了,也沒解決的問題(同除外)(我還加(一)倉庫以後有(一)交易無果的父母...... )((a)的意思是@但評論框不喜歡;) – Bamboomy

+0

你如何從sessionFactory獲得你的會話? –

+0

我在編輯中也添加了它:IntelliJ抱怨sessionFactory bean多次使用(但在檢查時這些引用指向同一個bean) – Bamboomy

0

我不太確定這個問題「固定」是什麼,但我確實得到了一個:

org.hibernate.HibernateException:Unable to locate current JTA transaction 

例外現在(所以這個問題解決了,我希望(我將設置我的答案是正確的答案,如果我不通過遷移過程中再次得到這個例外)

我覺得「

<prop key="hibernate.current_session_context_class">jta</prop> 

到:

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="datasource"/> 
     <property name="packagesToScan" value="be.fgov.just.cjr.model"/> 
     <property name="hibernateProperties"> 
      <props> 
無法找到當前JTA事務」 +加入如上所述的例外是由於我的配置

但我不確定這是否是一個例外,在被質疑的人之上或以下...

我不一定完全遵循GUISSOUMA伊薩姆,但他給了我一些好的指針,所以我會upvote他的迴應。 ..

未完待續......

相關問題