-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting a different connection handling mode and auto-close mod…
…e for non-injected sessions With this patch, hopefully people can safely use EntityManagerFactory.createEntityManager() and opening transactions manually? Signed-off-by: Yoann Rodière <yoann@hibernate.org>
- Loading branch information
Showing
3 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/JTASessionOpener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.hibernate.orm.runtime.session; | ||
|
||
import org.hibernate.FlushMode; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionBuilder; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.context.spi.CurrentTenantIdentifierResolver; | ||
import org.hibernate.engine.spi.SessionFactoryImplementor; | ||
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; | ||
|
||
/** | ||
* A delegate for opening a JTA-enabled Hibernate ORM session. | ||
* <p> | ||
* The main purpose of this class is to cache session options when possible; | ||
* if we didn't care about caching, we could just replace any call to | ||
*/ | ||
public class JTASessionOpener { | ||
public static JTASessionOpener create(SessionFactory sessionFactory) { | ||
final CurrentTenantIdentifierResolver currentTenantIdentifierResolver = sessionFactory | ||
.unwrap(SessionFactoryImplementor.class).getCurrentTenantIdentifierResolver(); | ||
if (currentTenantIdentifierResolver == null) { | ||
// No tenant ID resolver: we can cache the options. | ||
return new JTASessionOpener(sessionFactory, createOptions(sessionFactory)); | ||
} else { | ||
// There is a tenant ID resolver: we cannot cache the options. | ||
return new JTASessionOpener(sessionFactory, null); | ||
} | ||
} | ||
|
||
private static SessionBuilder<?> createOptions(SessionFactory sessionFactory) { | ||
return sessionFactory.withOptions() | ||
.autoClose(true) // .owner() is deprecated as well, so it looks like we need to rely on deprecated code... | ||
.connectionHandlingMode( | ||
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION) | ||
.flushMode(FlushMode.ALWAYS); | ||
} | ||
|
||
private final SessionFactory sessionFactory; | ||
private final SessionBuilder<?> cachedOptions; | ||
|
||
public JTASessionOpener(SessionFactory sessionFactory, SessionBuilder<?> cachedOptions) { | ||
this.sessionFactory = sessionFactory; | ||
this.cachedOptions = cachedOptions; | ||
} | ||
|
||
public Session openSession() { | ||
SessionBuilder<?> options = cachedOptions != null ? cachedOptions : createOptions(sessionFactory); | ||
return options.openSession(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters