HibernateTransactionManager error in Spring MVC


I'm trying to add user to to Database from Spring MVC controller. and I'm getting the following exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

My code is the following:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.postgresql.Driver" />
  <property name="url" value="jdbc:postgresql://" />
  <property name="username" value="doolloop2" />
  <property name="password" value="doolloop" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
     <property name="mappingLocations">
    <property name="hibernateProperties">
  <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
  <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.hbm2ddl.auto">update</prop> 
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />

<bean id="txService" class="com.doolloop.services.TxDataBaseServiceImpl">
        <property name="sessionFactory">
        <ref local="sessionFactory"/>

Then my Service looks like this:

public class TxDataBaseServiceImpl implements TxDataBaseService {

 private SessionFactory sessionFactory;

 public void setSessionFactory (SessionFactory sessionFactory)
       this.sessionFactory = sessionFactory;

 public void add(User user) {
  Session session = this.sessionFactory.getCurrentSession();

   Transaction tr = session.getTransaction();

Now on my Servlet I'm wiring the class

 private TxDataBaseServiceImpl service;

     @RequestMapping(value="/signup.dlp",method = RequestMethod.POST)
       public ModelAndView addUser(@Valid User user, BindingResult result){
        logger.info("Return View");
        registrationValidation.validate(user, result);
                /// all required validations....
       ModelAndView mv = new ModelAndView("/Accounts/signupSuccess");
       return mv;

The question is what am I doing wrong?


This is the error:

private TxDataBaseServiceImpl service;

You are trying to autowire the implementation class instead of the interface, and hence you get the raw bean injected, not the transactional proxy. Do it like this:

private TxDataBaseService service;

Always prefer coding against interfaces because a) Joshua Bloch says so :-) and b) a lot of the functionality in Spring is built upon interfaces.

Need Your Help

Did P/Invoke environment change in .NET 4.0?

delphi .net-4.0 pinvoke

I've started upgrading a .NET 2.0 WinForms application to .NET 4.0. Well, OK, the upgrade process was just a matter of switching platform target, but making it actually work. I assumed that's all...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.