diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index c0176cffe1..3baeba0b7d 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -380,10 +380,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { } this.defaultSessionOpenOptions = withOptions(); - this.temporarySessionOpenOptions = withOptions() - .autoClose( false ) - .flushMode( FlushMode.MANUAL ) - .connectionHandlingMode( PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT ); + this.temporarySessionOpenOptions = buildTemporarySessionOpenOptions(); this.fastSessionServices = new FastSessionServices( this ); this.observer.sessionFactoryCreated( this ); @@ -406,6 +403,13 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { } } + private SessionBuilder buildTemporarySessionOpenOptions() { + return withOptions() + .autoClose( false ) + .flushMode( FlushMode.MANUAL ) + .connectionHandlingMode( PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT ); + } + private void prepareEventListeners(MetadataImplementor metadata) { final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); @@ -481,11 +485,26 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { } public Session openSession() throws HibernateException { - return this.defaultSessionOpenOptions.openSession(); + final CurrentTenantIdentifierResolver currentTenantIdentifierResolver = getCurrentTenantIdentifierResolver(); + //We can only use reuse the defaultSessionOpenOptions as a constant when there is no TenantIdentifierResolver + if ( currentTenantIdentifierResolver != null ) { + return this.withOptions().openSession(); + } + else { + return this.defaultSessionOpenOptions.openSession(); + } } public Session openTemporarySession() throws HibernateException { - return this.temporarySessionOpenOptions.openSession(); + final CurrentTenantIdentifierResolver currentTenantIdentifierResolver = getCurrentTenantIdentifierResolver(); + //We can only use reuse the defaultSessionOpenOptions as a constant when there is no TenantIdentifierResolver + if ( currentTenantIdentifierResolver != null ) { + return buildTemporarySessionOpenOptions() + .openSession(); + } + else { + return this.temporarySessionOpenOptions.openSession(); + } } public Session getCurrentSession() throws HibernateException { @@ -1423,8 +1442,9 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { public StatelessSessionBuilderImpl(SessionFactoryImpl sessionFactory) { this.sessionFactory = sessionFactory; - if ( sessionFactory.getCurrentTenantIdentifierResolver() != null ) { - tenantIdentifier = sessionFactory.getCurrentTenantIdentifierResolver().resolveCurrentTenantIdentifier(); + CurrentTenantIdentifierResolver tenantIdentifierResolver = sessionFactory.getCurrentTenantIdentifierResolver(); + if ( tenantIdentifierResolver != null ) { + tenantIdentifier = tenantIdentifierResolver.resolveCurrentTenantIdentifier(); } queryParametersValidationEnabled = sessionFactory.getSessionFactoryOptions().isQueryParametersValidationEnabled(); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/discriminator/DiscriminatorMultiTenancyTest.java b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/discriminator/DiscriminatorMultiTenancyTest.java index d9d22b9391..f15e825257 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/discriminator/DiscriminatorMultiTenancyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/discriminator/DiscriminatorMultiTenancyTest.java @@ -193,9 +193,10 @@ public class DiscriminatorMultiTenancyTest extends BaseUnitTestCase { } } - public void doInHibernate(String tenant, - Consumer function) { + public void doInHibernate(String tenant, Consumer function) { currentTenantResolver.currentTenantIdentifier = tenant; - TransactionUtil.doInHibernate( this::sessionFactory, tenant, function); + //Careful: do not use the #doInHibernate version of the method which takes a tenant: the goal of these tests is + // to verify that the CurrentTenantIdentifierResolver is being applied! + TransactionUtil.doInHibernate( this::sessionFactory, function); } } \ No newline at end of file diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/transaction/TransactionUtil.java b/hibernate-testing/src/main/java/org/hibernate/testing/transaction/TransactionUtil.java index 67de5eb55c..541640390b 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/transaction/TransactionUtil.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/transaction/TransactionUtil.java @@ -35,6 +35,8 @@ import org.hibernate.dialect.SQLServerDialect; import org.hibernate.dialect.SybaseASE15Dialect; import org.hibernate.engine.jdbc.spi.JdbcServices; +import org.junit.Assert; + import org.jboss.logging.Logger; /** @@ -44,6 +46,28 @@ public class TransactionUtil { private static final Logger log = Logger.getLogger( TransactionUtil.class ); + public static void doInHibernate(Supplier factorySupplier, Consumer function) { + final SessionFactory sessionFactory = factorySupplier.get(); + Assert.assertNotNull( "SessionFactory is null in test!", sessionFactory ); + //Make sure any error is propagated + try ( Session session = sessionFactory.openSession() ) { + final Transaction txn = session.getTransaction(); + txn.begin(); + try { + function.accept( session ); + } + catch (Throwable e) { + try { + txn.rollback(); + } + finally { + throw e; + } + } + txn.commit(); + } + } + /** * Hibernate transaction function *