简体   繁体   中英

migration to hibernate 4 + spring 4.2.2: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

I'm migrating to spring 4.2.2.RELEASE and hibernate 5 and get following exception:

The exception also occurs in hibernate 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

this is the definition of the 'lovDao' bean:

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

and relevant portions of the 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>

Now, this is the class definition of the lovDao:

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

and also GenericDaoImpl is transactional

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

Why doesn't lovDao can't obtain a transaction-synchronized Session?

ListOfValuesDaoImpl wasn't Transactional at first but threw the exact same exception; GenericDaoImpl was.

also GenericDao is Transactional: (not anymore)

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

I know that similar questions are asked around SO but I already tried quite some pointers and read quite some guides but nothing seemed to work...

thanks for pointers/help/critical remarks ;)

S.

edit:

the sessionFactory is set in GenericDaoImpl...:

private SessionFactory sessionFactory;

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

(I added the @Qualifier("sessionFactory") because IntelliJ complained about multiple beans (which in fact pointed to the same bean)) -> I checked whether the dao-context.xml is used multiple times but it isn't

...and (in that same class) the session is obtained like this:

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

edit 2 (reaction on edit of answer of GUISSOUMA Issam):

I also have beans like this one:

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

What do I do with those?

(they reference ListOfValuesDao...) (@Autowiring doesn't seem to help (according to IntelliJ) it doesn't find a reference to a ListOfValuesDao)

-> the same exception also occurs when I removed:

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

ps: thanks for your persistence in helping me, problem is I can't test out stuff now because I write this from home (and can't reach the environment on the office :( -> if you give a couple of pointers I'll be able to try them out tomorrow but I can only depend on the IDE's output for the present moment :( -> I'll try to give as much information as possible

I also have this in GenericDaoImpl:

private SessionFactory sessionFactory;

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

in combination with:

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

based on the edit of the answer I suppose this can cause the problem? (I can only check for sure tomorrow morning...)

edit 3: I've gotten quite some new information and instead of appending it all here I created a new question ...

Add @Repository annotation to your DAO.

remove this declaration

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

from your XML

because you have this:

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

Make sure your package is scanned only once(declared on in one xml file)

I'm not quite sure what 'fixed' this issue but I do get a:

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

exception now (so this question is resolved, I hope (I'll set my answer as correct answer if I don't get this exception again through the migrating process)

I think the "Unable to locate current JTA transaction" exception is due to my configuration as described above + the addition of:

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

to the:

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

But I'm not sure whether this is an exception on top of the questioned one or below...

I not necessarily completely followed GUISSOUMA Issam but he gave me some good pointers so I'll upvote his response...

to be continued...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM