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.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();
}

View File

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

View File

@ -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<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
*