Hibernate transaction management


#1

As part of hibernate migration from 3 to 5, i am facing some issues related to the transactional entity.
I am using Spring and hibernate and in this case, i am using spring managed Transactional annotation.
Consider the case where One method with transactional calls 2 different methods with/without the transaction.
In hibernate 3 it used to work properly. But after upgrade to hibernate 5 setEMSStatus is failing as subscribedEms is null.

//Class1
@Transactional(rollbackFor = Exception.class)
public void validateAndSave(List<EmsDto> gridList) {
	//Removing unnecessary code
	emsEntityManager.createAndSaveSubscribedEms(EmsDto.getMessageName(), EmsDto.getDescription(), SubscribeEmsStatus.UNKNOWN.toString());
	//Removing unnecessary code
    emsEntityManager.setEMSStatus(entry.getKey(), SubscribeEmsStatus.VALID.toString());
}
	
//Class2
@Transactional
public void createAndSaveSubscribedEms(final String messageName, final String description, final String status) {
    session.save(SubscribedEms.create(messageName, description, status));
}
// Note: No transactional and it was working earlier in hibernate 3	
public void setEMSStatus(String emsName, String newStatus) {
    SubscribedEms subscribedEms = (SubscribedEms) session.createCriteria(SubscribedEms.class).
        add(Restrictions.eq(SubscribedEms_.messageName, emsName)).
        uniqueResult();
    subscribedEms.setStatus(newStatus);
}

I came across a similar issue


I want to know if there is any change in the way Transaction is managed.


#2

This is not a Hibernate issue, it’s a Spring configuration one. So, you need to ask the question on the Spring forum.


#3

I agree @Transactional is from spring. I have upgraded just hibernate and no changes were made to spring.

Below is the configuration xml (Spring 4.3.4, Hibernate 5.1.15 & Jboss 10).
Should i set any hibernate property or any spring-hibernate configuration ?

I have started discussion but still no help from the spring community

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"
        >

    <context:annotation-config />

    <context:component-scan base-package="com.company.product.da">
        <context:exclude-filter type="regex" expression="com\.company\.product\..*Test\$.*"/>
        <context:exclude-filter type="regex" expression="com\.company\.product\..*TestSupport\$.*"/>
    </context:component-scan>

    <context:component-scan base-package="com.company.product.entity.platform.event">
        <context:exclude-filter type="regex" expression="com\.companthis partct\..*Test\$.*"/>
    </context:component-scan>

    <jee:jndi-lookup id="jtaTransactionManager" jndi-name="java:/TransactionManager"/>
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="jtaTransactionManager"/>

    <import resource="classpath:spring/database-common.xml"/>

    <bean id="eventListenerIntegrator" class="com.company.product.persist.hibernate.EventListenerIntegrator"/>

    <bean id="hibernateSessionFactory" class="com.company.product.persist.hibernate.CustomHibernateSessionFactoryBean"
        depends-on="migrationVersionVerifier">
        <constructor-arg ref = "customInfinispanRegionFactory"/>
        <constructor-arg ref = "eventListenerIntegrator"/>
        <property name="hibernateProperties" ref="optionValueStoreProperties" />
        <property name="packagesToScan" value="com.company.product.entity" />
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>