Could not obtain transaction-synchronized Session for current thread

I autowire a DAO with the controller. Do not ask me, ‘Hey stupid, why do you want to couple the controller with a DAO’. Because I’m setting up the environment. Service tier is not yet ready. Ok!

package org.grassfield.vriksha.controller;

import java.util.List;

import org.grassfield.common.dao.IUserRoleDAO;
import org.grassfield.common.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class VrikshaController {
    @Autowired
    IUserRoleDAO userRoleDAO;
    
    
    public IUserRoleDAO getUserRoleDAO() {
        return userRoleDAO;
    }


    public void setUserRoleDAO(IUserRoleDAO userRoleDAO) {
        this.userRoleDAO = userRoleDAO;
    }


    @RequestMapping("/welcome")
    public ModelAndView helloWorld(){
        System.out.println("1");
        List<UserRole> list = this.userRoleDAO.list();
        String message = "<h3>Welcome</h3>"+list;
        return new ModelAndView("welcome", "message", message);
    }
}

Okay, While hitting the page, it is very eager to show me an error page. Could not obtain transaction-synchronized Session for current thread

 
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    org.grassfield.common.dao.UserRoleDAO.list(UserRoleDAO.java:26)

It is my mistake on the configuration XML. Let’s add the annotation-driven tag.

    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

It is not sufficient.

bean schema locations.

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:p=”http://www.springframework.org/schema/p&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xmlns:tx=”http://www.springframework.org/schema/tx&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd”&gt;

Here you go! This will help you to resolve this error.

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s