Skip to content

Native Hibernate 5 bootstrapping with JTA transaction manager requires hibernate.transaction.coordinator_class=jta to be set #25858

@sammyhk

Description

@sammyhk

Affects: Spring Framework 5.2.9.RELEASE, Hibernate 5.4.21.Final


Suspected connection leak in Spring Hibernation 5 integration with using JTA transaction manager.
Consider the following sample application context XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

	<context:component-scan
		base-package="com.github.sammyhk.test" />

	<tx:annotation-driven
		transaction-manager="transactionManager" mode="proxy"
		proxy-target-class="false" />

	<!-- Wildfly managed data source -->
	<jee:jndi-lookup id="dataSource"
		jndi-name="java:jboss/datasources/testDataSource"
		expected-type="javax.sql.DataSource" resource-ref="true" />

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value><![CDATA[com.github.sammyhk.test.MyEntity]]></value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect"><![CDATA[org.hibernate.dialect.Oracle10gDialect]]></prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager" />


	<!-- Using Hibernate Template to CRUD MyEntity -->
	<bean id="myEntityDao" class="com.github.sammyhk.test.MyEntityDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- Annotated with @org.springframework.transaction.annotation.Transactional, operate myEntityDao in a transaction -->
	<bean id="myService" class="com.github.sammyhk.test.MyServiceImpl">
		<property name="myEntityDao" ref="myEntityDao" />
	</bean>
</beans>

Calling myService will be suspected connection leak due to Hibernate session has NOT been close. Debugging found org.springframework.orm.hibernate5.SpringSessionSynchronization has NOT been registered due to org.springframework.orm.hibernate5.SpringSessionContext returned at line 122 while SpringSessionSynchronization registration is in line 136.

In this example we tried to use HibernateTransactionManager and it is working correctly, but using JtaTransactionManager encountered such issue.

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)status: backportedAn issue that has been backported to maintenance branchestype: documentationA documentation tasktype: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions