HHH-16150 Fix schema not being dropped on bootstrap failure with the "create-drop" strategy

This commit is contained in:
Yoann Rodière 2023-02-07 16:32:01 +01:00 committed by Christian Beikov
parent df4135c7f0
commit 97d5057751
2 changed files with 37 additions and 1 deletions

View File

@ -457,7 +457,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
for ( Integrator integrator : serviceRegistry.getService( IntegratorService.class ).getIntegrators() ) {
integrator.disintegrate( this, serviceRegistry );
integratorObserver.integrators.remove( integrator );
serviceRegistry.close();
}
}

View File

@ -10,6 +10,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -26,6 +27,8 @@ import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.testing.transaction.TransactionUtil2;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hibernate.cfg.AvailableSettings.HBM2DDL_DATABASE_ACTION;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_JDBC_DRIVER;
@ -37,6 +40,8 @@ import static org.hibernate.cfg.AvailableSettings.JPA_JDBC_PASSWORD;
import static org.hibernate.cfg.AvailableSettings.JPA_JDBC_URL;
import static org.hibernate.cfg.AvailableSettings.JPA_JDBC_USER;
import org.assertj.core.api.Assertions;
/**
* @author Steve Ebersole
*/
@ -102,6 +107,38 @@ public class JakartaSchemaToolingTests {
}
}
@Test
public void testCreateDropWithFailureInBetween() {
// Make sure that when using the "create-drop" database action, when a failure occur after schema is created,
// the schema is correctly dropped.
assertThatThrownBy( () -> buildSessionFactory(
JAKARTA_HBM2DDL_DATABASE_ACTION, Action.CREATE_DROP,
JAKARTA_JDBC_DRIVER, Environment.getProperties().get( AvailableSettings.DRIVER ),
JAKARTA_JDBC_URL, Environment.getProperties().get( AvailableSettings.URL ),
JAKARTA_JDBC_USER, Environment.getProperties().get( AvailableSettings.USER ),
JAKARTA_JDBC_PASSWORD, Environment.getProperties().get( AvailableSettings.PASS ),
// Simulates a failure from e.g. the Hibernate Search observer
AvailableSettings.SESSION_FACTORY_OBSERVER, new SessionFactoryObserver() {
@Override
public void sessionFactoryCreated(org.hibernate.SessionFactory factory) {
throw new RuntimeException( "Simulated failure" );
}
}
) )
.hasRootCauseMessage( "Simulated failure" );
// Now check that the schema was dropped: queries should fail.
try ( SessionFactoryImplementor sessionFactory = buildSessionFactory(
JAKARTA_HBM2DDL_DATABASE_ACTION, Action.NONE,
JAKARTA_JDBC_DRIVER, Environment.getProperties().get( AvailableSettings.DRIVER ),
JAKARTA_JDBC_URL, Environment.getProperties().get( AvailableSettings.URL ),
JAKARTA_JDBC_USER, Environment.getProperties().get( AvailableSettings.USER ),
JAKARTA_JDBC_PASSWORD, Environment.getProperties().get( AvailableSettings.PASS )
) ) {
assertThatThrownBy( () -> tryQuery( sessionFactory ) ).isNotNull();
}
}
private SessionFactoryImplementor buildSessionFactory(Object... settingPairs) {
final Properties settings = CollectionHelper.toProperties( settingPairs );