Migration from Hibernate 3.6 to 5.4 broke session management?!

Hi Guys,

I have a legacy app that used Spring 4.3 with Hibernate 3.6 which I migrated to Spring 5.3 and Hibernate 5.4. I have this piece of code in a abstract ComonHibernateDAOImpl which is extended in every DAO class I have and serves some common Hibernate tasks.

public abstract class CommonHibernateDAOImpl<ENTITY extends Serializable> extends HibernateDaoSupport implements CommonHibernateDAO<ENTITY> {


  @Override
  public final List<ENTITY> findAll(final Class<ENTITY> entityClass) {
    return getHibernateTemplate().loadAll(entityClass);
  }
}

While this code worked perfectly fine in Hibernate 3.6, is stops working in 5.4 with following error:

Caused by: org.springframework.orm.hibernate5.HibernateSystemException: Calling method 'createCriteria' is not valid without an active transaction (Current status: NOT_ACTIVE); nested exception is org.hibernate.HibernateException: Calling method 'createCriteria' is not valid without an active transaction (Current status: NOT_ACTIVE)
        at deployment.myApp.war//org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:267)
        at deployment.myApp.war//org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:370)
        at deployment.myApp.war//org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:334)
        at deployment.myApp.war//org.springframework.orm.hibernate5.HibernateTemplate.loadAll(HibernateTemplate.java:519)
        at deployment.myApp.war//com.anes.ilogic.common.integration.dao.impl.CommonHibernateDAOImpl.findAll(CommonHibernateDAOImpl.java:233)
        at deployment.myApp.war//com.cl.ilogic.core.vs.service.VsValidationServiceImpl.reload(VsValidationServiceImpl.java:245)
        at deployment.myApp.war//com.cl.ilogic.core.vs.service.VsValidationServiceImpl.init(VsValidationServiceImpl.java:298)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at deployment.myApp.war//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1930)
        at deployment.myApp.war//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1872)
        at deployment.myApp.war//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
        ... 83 more
Caused by: org.hibernate.HibernateException: Calling method 'createCriteria' is not valid without an active transaction (Current status: NOT_ACTIVE)
        at deployment.myApp.war//org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:347)
        at deployment.myApp.war/jdk.proxy21/jdk.proxy21.$Proxy213.createCriteria(Unknown Source)
        at deployment.myApp.war//org.springframework.orm.hibernate5.HibernateTemplate.lambda$loadAll$4(HibernateTemplate.java:520)
        at deployment.myApp.war//org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:367)
        ... 95 more

If this worked before, then it is because your Spring environment was different previously. As far as I can tell from the Git history, the SessionFactory.currentSession(), which is used by HibernateTemplate, always required that a transaction is running to execute queries. Now, HibernateTemplate seems to have a fallback, where it opens and closes a session when there is no currentSession available.

So my guess is, the new Spring version somehow configures hibernate.current_session_context_class to thread which it didn’t do before.

Yes, I have in fact added this setting during the course of the migration because I thought this would solve my error, which was there before. But even without that flag, the same error comes up. But it appears to me that this is more of a Spring problem than a Hibernate issue.

Check if Spring configures this setting somehow and whether you can disable this.

1 Like

I found the error, it was indeed a Spring misconfiguration. Thank you!