0

I have migrated Spring from 3.1 to 4.1.3 and Hibernate 3.2 to 4.3.9

As part of migration I have modified below import and code

old import

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

new Import

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

and code changes

In hibernate 3 I have following code

public Session getCurrentSession() {
    Session session = getSession();
    session.setFlushMode(FlushMode.MANUAL);
    return session;
}

now I have modified according to new jars as below

public Session getCurrentSession() {
    Session session = currentSession();
    session.setFlushMode(FlushMode.MANUAL);
    return session;
    }

after the above changes I am getting below exception

    Could not obtain transaction-synchronized Session for current thread
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at org.springframework.orm.hibernate4.support.HibernateDaoSupport.currentSession(HibernateDaoSupport.java:129)

I am not using annotations in my application

I am not able to resolve the issue. Please help me in knowing possible reasons for the exception

Raj
  • 2,463
  • 10
  • 36
  • 52
  • Fix your transaction setup. This exception tells you that you don't have (properly) setup your transactions. – M. Deinum Oct 17 '16 at 10:41
  • @M.Deinum I have added tag in my configuration file, but still getting same issue. do I need to add anything more? Please confirm – Raj Oct 17 '16 at 10:44
  • Only adding that tag without adding `@Transactional` is pretty much useless. – M. Deinum Oct 17 '16 at 10:49
  • @Raj If you are not using Annotations in your project. Then, there isn't no benefit of using ``. – gschambial Oct 17 '16 at 11:04
  • @gschambial yes, but I gave a trail and it didn't worked :) – Raj Oct 17 '16 at 11:09
  • I have updated my answer. can you check on that? – gschambial Oct 17 '16 at 11:11
  • Please refer [http://stackoverflow.com/questions/26203446/spring-hibernate-could-not-obtain-transaction-synchronized-session-for-current](http://stackoverflow.com/questions/26203446/spring-hibernate-could-not-obtain-transaction-synchronized-session-for-current) – Raman Oct 17 '16 at 12:04

1 Answers1

1

I guess you need to add transactionManager for your sesssionFactory:

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

where, mySessionFactory is your Session Factory Bean Id.

As you said, you are not using Annotations in your project. Then, you must use AOP to enable Transaction Management at Method Level.

<tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="select*"  read-only="true" />
    <tx:method name="*" />
  </tx:attributes>
</tx:advice>
<aop:config>
  <aop:pointcut id="txPointcut" expression="execution(* example.MyClass.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>

Add Transaction Support for your method like this.

Add following entry in your POM, if you are using MAVEN or gradle or you can simply download and add jar to your classpath.

<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
gschambial
  • 1,383
  • 2
  • 11
  • 22
  • Make sure you are using `org.springframework.orm.hibernate4.HibernateTransactionManager` not `org.springframework.orm.hibernate3.HibernateTransactionManager` – gschambial Oct 17 '16 at 10:55
  • the application not using aop as well.. I can post my tx details if you want – Raj Oct 17 '16 at 11:33
  • @Raj There is no complex configuration for adding Transaction AOP support. I have updated answer for adding AOP support. – gschambial Oct 17 '16 at 11:43
  • @Raj any luck ? – gschambial Oct 18 '16 at 05:23
  • @ gschambial sorry for the late reply.. no luck yet.. is there any alternative so that we can avoid annotations?? as the current application not using annotations and I want to continue without annotations. or @Transactional is mandatory for spring4? – Raj Oct 19 '16 at 06:32
  • @Raj You can avoid annotations, if you add Tranaction support via XML as i have explained in my answer also. If you go for XML based support, then you need not to add annotations in your project for Transaction support. – gschambial Oct 19 '16 at 06:36
  • thanks for your response.. If i simply change the spring method getSession() to currentSession() iam getting below exception Could not obtain transaction-synchronized Session for current thread org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) at – Raj Oct 19 '16 at 06:42
  • Yes, I think because you need to add Transaction support. Therefore, i am suggesting you to add XML based support for your method. – gschambial Oct 19 '16 at 06:51