diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java index 44ea98d385395..ab7a594f89bd2 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java @@ -255,6 +255,9 @@ private MergedSettings mergeSettings(PersistenceUnitDescriptor persistenceUnit) cfg.putIfAbsent(AvailableSettings.CONNECTION_HANDLING, PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION); + // Auto-close sessions before transaction completion, as they should be when using JTA. + cfg.putIfAbsent(AvailableSettings.AUTO_CLOSE_SESSION, "true"); + if (readBooleanConfigurationValue(cfg, WRAP_RESULT_SETS)) { LOG.warn("Wrapping result sets is not supported. Setting " + WRAP_RESULT_SETS + " to false."); } diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/TransactionScopedSession.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/TransactionScopedSession.java index 3eafb8353c051..21c89cf29d9fe 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/TransactionScopedSession.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/TransactionScopedSession.java @@ -18,7 +18,6 @@ import javax.persistence.criteria.CriteriaUpdate; import javax.persistence.metamodel.Metamodel; import javax.transaction.Status; -import javax.transaction.Synchronization; import javax.transaction.TransactionManager; import javax.transaction.TransactionSynchronizationRegistry; @@ -86,17 +85,13 @@ SessionResult acquireSession() { Session newSession = sessionFactory.openSession(); newSession.joinTransaction(); transactionSynchronizationRegistry.putResource(sessionKey, newSession); - transactionSynchronizationRegistry.registerInterposedSynchronization(new Synchronization() { - @Override - public void beforeCompletion() { - newSession.flush(); - } - - @Override - public void afterCompletion(int i) { - newSession.close(); - } - }); + // No need to flush or close the session upon transaction completion: + // Hibernate ORM itself registers a transaction that does just that. + // See: + // - io.quarkus.hibernate.orm.runtime.boot.FastBootMetadataBuilder.mergeSettings + // - org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.joinJtaTransaction + // - org.hibernate.internal.SessionImpl.beforeTransactionCompletion + // - org.hibernate.internal.SessionImpl.afterTransactionCompletion return new SessionResult(newSession, false, true); } else { //this will throw an exception if the request scope is not active