org.hibernate.HibernateException: No Session found for current thread

Can't Tell picture Can't Tell · May 5, 2012 · Viewed 55.8k times · Source

I'm getting the above exception with Spring3 and Hibernte4

The following is my bean xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

   <context:annotation-config/>


   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/GHS"/>
      <property name="username" value="root"/>
      <property name="password" value="newpwd"/>
  </bean>

  <bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.example.ghs.model.timetable</value>
        </list>
    </property>
  </bean>

   <bean id="baseDAO"
      class="com.example.ghs.dao.BaseDAOImpl"/>
</beans>

My BaseDAO class looks like this

public class BaseDAOImpl implements BaseDAO{
private SessionFactory sessionFactory;

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

 @Override
 public Session getCurrentSession(){
     return sessionFactory.getCurrentSession();
 }
}

The following code throws the exception in the title

public class Main {
 public static void main(String[] args){
    ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("dao-beans.xml");
    BaseDAO bd = (BaseDAO) context.getBean("baseDAO");
    bd.getCurrentSession();
 }
}

Does anyone have an idea about how to solve this problem?

Answer

axtavt picture axtavt · May 5, 2012

getCurrentSession() only makes sense inside a scope of transaction.

You need to declare an appropriate transaction manager, demarcate boundaries of transaction and perform data access inside it. For example, as follows:

<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name = "sessionFactory" ref = "sessionFactory" />
</bean>

.

PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class);
TransactionTemplate tx = new TransactionTemplate(ptm);

tx.execute(new TransactionCallbackWithoutResult() {
    public void doInTransactionWithoutResult(TransactionStatus status) { 
        // Perform data access here
    }
});

See also: