HHH-13687 TenantSchemaResolver not called in integration test after upgrade from

This commit is contained in:
Sanne Grinovero 2019-11-11 19:05:25 +00:00 committed by Sanne Grinovero
parent 66515a2e4e
commit 164e1fc7cc
3 changed files with 56 additions and 11 deletions

View File

@ -380,10 +380,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
} }
this.defaultSessionOpenOptions = withOptions(); this.defaultSessionOpenOptions = withOptions();
this.temporarySessionOpenOptions = withOptions() this.temporarySessionOpenOptions = buildTemporarySessionOpenOptions();
.autoClose( false )
.flushMode( FlushMode.MANUAL )
.connectionHandlingMode( PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT );
this.fastSessionServices = new FastSessionServices( this ); this.fastSessionServices = new FastSessionServices( this );
this.observer.sessionFactoryCreated( 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) { private void prepareEventListeners(MetadataImplementor metadata) {
final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
@ -481,12 +485,27 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
} }
public Session openSession() throws HibernateException { public Session openSession() throws HibernateException {
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(); return this.defaultSessionOpenOptions.openSession();
} }
}
public Session openTemporarySession() throws HibernateException { public Session openTemporarySession() throws HibernateException {
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(); return this.temporarySessionOpenOptions.openSession();
} }
}
public Session getCurrentSession() throws HibernateException { public Session getCurrentSession() throws HibernateException {
if ( currentSessionContext == null ) { if ( currentSessionContext == null ) {
@ -1423,8 +1442,9 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
public StatelessSessionBuilderImpl(SessionFactoryImpl sessionFactory) { public StatelessSessionBuilderImpl(SessionFactoryImpl sessionFactory) {
this.sessionFactory = sessionFactory; this.sessionFactory = sessionFactory;
if ( sessionFactory.getCurrentTenantIdentifierResolver() != null ) { CurrentTenantIdentifierResolver tenantIdentifierResolver = sessionFactory.getCurrentTenantIdentifierResolver();
tenantIdentifier = sessionFactory.getCurrentTenantIdentifierResolver().resolveCurrentTenantIdentifier(); if ( tenantIdentifierResolver != null ) {
tenantIdentifier = tenantIdentifierResolver.resolveCurrentTenantIdentifier();
} }
queryParametersValidationEnabled = sessionFactory.getSessionFactoryOptions().isQueryParametersValidationEnabled(); queryParametersValidationEnabled = sessionFactory.getSessionFactoryOptions().isQueryParametersValidationEnabled();
} }

View File

@ -193,9 +193,10 @@ public class DiscriminatorMultiTenancyTest extends BaseUnitTestCase {
} }
} }
public void doInHibernate(String tenant, public void doInHibernate(String tenant, Consumer<Session> function) {
Consumer<Session> function) {
currentTenantResolver.currentTenantIdentifier = tenant; 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);
} }
} }

View File

@ -35,6 +35,8 @@ import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.SybaseASE15Dialect; import org.hibernate.dialect.SybaseASE15Dialect;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.junit.Assert;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
/** /**
@ -44,6 +46,28 @@ public class TransactionUtil {
private static final Logger log = Logger.getLogger( TransactionUtil.class ); private static final Logger log = Logger.getLogger( TransactionUtil.class );
public static void doInHibernate(Supplier<SessionFactory> factorySupplier, Consumer<Session> 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 * Hibernate transaction function
* *