迁移到hibernate 4 + spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程

时间:2015-11-04 12:35:06

标签: java spring hibernate transactions

我正在迁移到spring 4.2.2.RELEASE和hibernate 5并获得以下异常:

例外情况也发生在休眠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的定义:

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

和dao-context.xml的相关部分:

<!-- 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 {

我知道类似的问题是围绕SO问的,但我已经尝试过一些指导并阅读了一些指南,但似乎没有任何工作......

感谢指针/帮助/批评备注;)

S上。

编辑:

sessionFactory在GenericDaoImpl ...中设置:

private SessionFactory sessionFactory;

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

(我添加了@Qualifier(“sessionFactory”),因为IntelliJ抱怨多个bean(实际上指向同一个bean)) - &GT;我检查了dao-context.xml是否多次使用,但它不是

...和(在同一个类中)会话是这样获得的:

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

编辑2(对GUISSOUMA Issam的回答编辑的反应):

我也有像这样的豆子:

<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的引用)

- &GT;当我删除时也会发生同样的异常:

<context:component-scan base-package="be.fgov.just.cjr.dao" />
ps:感谢你坚持不懈地帮助我,问题是我现在无法测试东西,因为我从家里写这个(并且无法到达办公室的环境:( - &GT;如果你给出几个指示我将能够明天尝试它们但我现在只能依赖IDE的输出:( - &GT;我会尽量提供尽可能多的信息

我在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 ...

2 个答案:

答案 0 :(得分:1)

将@Repository注释添加到DAO。

删除此声明

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

来自您的XML

因为你有这个:

window.location

确保您的包只扫描一次(在一个xml文件中声明)

答案 1 :(得分:0)

我不太确定这个问题是什么'修复'但我确实得到了:

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

异常(所以这个问题已经解决了,我希望(如果我没有通过迁移过程再次获得此异常,我会将答案设置为正确答案)

我认为“无法找到当前的JTA事务”异常是由于我的配置如上所述+添加:

<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>

但是我不确定这是否是在被质疑者之一或以下的例外......

我不一定完全跟随GUISSOUMA Issam,但他给了我一些好的指示,所以我会提出他的回应......

继续......