8

I an using Spring 3.1 + Hibernate 4.x in my web application. In my DAO, i am saving User type object as following

sessionFactory.getCurrentSession().save(user);

But getting following exception:

org.hibernate.HibernateException: save is not valid without active transaction

I googled and found similar question on SO, with following solution:

   Session session=getSessionFactory().getCurrentSession();
   Transaction trans=session.beginTransaction();
   session.save(entity);
   trans.commit();

That solves the problem. But in that solution, there is lot of mess of beginning and committing the transactions manually.

Can't i use sessionFactory.getCurrentSession().save(user); directly without begin/commit of transactions manually?

I try to use @Transactional on my service/dao methods too, but the problem persists.

EDIT : Here is my Spring Config File:

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


  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${db.driverClassName}" p:url="${db.url}"
        p:username="${db.username}" p:password="${db.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.myapp.entities" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>


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

</beans>

I am using following Hibernate 4 dependencies:

<!-- Hibernate Dependency -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.7.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.1.Final</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>${cglib.version}</version>
            <scope>runtime</scope>
        </dependency>

Please help.

Community
  • 1
  • 1
Arun Kumar
  • 6,534
  • 13
  • 40
  • 67
  • can you post spring configuration file – Jigar Parekh Oct 25 '12 at 09:59
  • 1
    http://stackoverflow.com/questions/1079114/spring-transactional-annotation-best-practice has some good advice on this subject. I would definitely create the Transactions in the service layer (not DAO layer), either with `@Transactional` or via AOP – andyb Oct 25 '12 at 10:13
  • @JigarParekh I have posted the Config File Content and updated my question. – Arun Kumar Oct 25 '12 at 10:22
  • andyb : Yes.It is good practice to place @Transactional at your Service classes. I have placed the annotation on my Service method too, but the problem remains same. – Arun Kumar Oct 25 '12 at 10:28

3 Answers3

9

Basically what needs to be done is to remove from the applicationContext.xml file the following line for Hibernate properties:

<prop key="hibernate.current_session_context_class">thread</prop>

Once that is remove, Hibernate makes use of Spring for transaction management

Good luck to you all.

hoang nguyen
  • 2,119
  • 5
  • 21
  • 20
  • I removed that line but after that this exception came `org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here` – Arun Kumar Oct 25 '12 at 10:15
  • See also http://stackoverflow.com/questions/18832889/spring-transactions-and-hibernate-current-session-context-class – user11153 Sep 18 '14 at 11:36
  • I down voted your answer because I thought hibernate.current_session_context_class had nothing to do with this error. But removing it actually resolved the error. Sorry. Can you share a link that explains this? Thanks. – Asif Feb 20 '15 at 00:58
4

i think you are using Hibernate 4.x then why you are using hibernate 3 transaction manager in application context file?

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

i think it should be

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

just try to use hibernate 4 transaction manager along with @Transactional attribute it should work.

Jigar Parekh
  • 6,163
  • 7
  • 44
  • 64
  • `java.lang.ClassNotFoundException: org.hibernate.engine.spi.SessionFactoryImplementor`. I think `org.springframework.orm.hibernate4.HibernateTransactionManager` is not a valid class. – Arun Kumar Oct 25 '12 at 10:46
  • it is valid class http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/orm/hibernate4/HibernateTransactionManager.html looks like you are missing some jar, if you are using maven then post pom file. – Jigar Parekh Oct 25 '12 at 10:48
  • Yes it is. I'll try to locate the missing jar. – Arun Kumar Oct 25 '12 at 11:18
  • I have listed the hibernate 4 dependencies in my updated question. – Arun Kumar Oct 25 '12 at 12:18
  • Updating transaction manager to version 4 doesn't work. Still getting the same exception : `org.hibernate.HibernateException: save is not valid without active transaction`. – Arun Kumar Oct 25 '12 at 13:00
  • can you try to configure property of session facotory `org.hibernate.transaction.JDBCTransactionFactory ` – Jigar Parekh Oct 25 '12 at 13:22
  • I tried with this property. But ` java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.transaction.JDBCTransact??ionFactory` is appearing. I doubt that `org.hibernate.transaction.JDBCTransact‌​ionFactory` is not included in **hibernate-core-4.1.7.Final**. – Arun Kumar Oct 25 '12 at 14:08
  • looks like copy paste issue try whole JDBCTransact‌ionFactory – Jigar Parekh Oct 25 '12 at 14:11
1

Using your working version of the Spring XML, and the @Transactional annotated DAO class, are you defining the DAO in your Spring XML ? (Perhaps as a prototype) Because if you're not, then so far as I can see, your DAO is not going to be AOP'ed for transactional aspects. I think that's the easiest way. This example is from the Spring 3 Doc, section 10.5.6 Using @Transactional.

  <!-- this is the service object that we want to make transactional -->
  <bean id="fooService" class="x.y.service.DefaultFooService"/>

  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

...where you might substitute DefaultFooService for your DAO.

Nicholas
  • 15,916
  • 4
  • 42
  • 66