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

I was integrating the Spring and Hibernate on a web application. While do so, I end up with the hibernate exception as follows,

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


First we need to understand that the getCurrentSession() only makes sense inside a scope of transaction. We need to declare an appropriate transaction manager, demarcate boundaries of transaction and perform data access inside it. For example, as follows:

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

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

<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<property name="hibernateProperties">
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="">${} </prop>

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>

At the service and DAO classes, the getSession() method only makes sense inside a scope of transaction, If you are trying to get sessionFactory.getCurrentSession() without defining your class in transactional scope then you will get above exception because getCurrentSession() can be use only in transactional scope. Please make your class transaction using @Transactional annnotation

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeDao employeeDao;

@Transactional(readOnly = false)
public List listEmployees() {
return employeeDao.listEmployees();

And from my DAO class

public List listEmployees() {
try {
return (List) sessionFactory.getCurrentSession().createCriteria(Employee.class).list();
}catch(Exception e) {
return null;

only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.


One Comment

  1. Posted November 3, 2015 at 11:28 am | Permalink | Reply

    public List getXXXXXList(Long xxx,
    Long xxx, Long xxxx) {

    Add the annotation @SuppressWarnings(“unchecked”) it will work

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: