简体   繁体   中英

HibernateSystemException: No Hibernate Session bound to thread

I am getting HibernateSystemException although I did everything that is mentioned on different forums.

Here is a part of applicationContext.xml

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

I also placed @Transactional annotaion above my class.

@Transactional
public class MyClassImpl

A) this is the wrong transaction manager:

<bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

you need org.springframework.orm.hibernate3.HibernateTransactionManager , as you can see in 13.3.3 Hibernate > Declarative transaction demarcation .

DataSourceTransactionManager is for plain JDBC, not for Hibernate (see 12.3.8 JDBC > DataSourceTransactionManager ).

B) you need this line also:

<tx:annotation-driven transaction-manager="transactionManager" />

Have you checked that this markup is present in you application context file?

<context:annotation-config />

It is necessary to consider your annotations.

Did you include the tx namespace in your configuration?在此处输入图像描述

Before doing any request, you could try this piece of code:

Session session = SessionFactoryUtils.getSession(dataSource, null, null);
TransactionSynchronizationManager.bindResource(dataSource, new SessionHolder(session));

Please keep me informed.

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