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 35f6135bf9..2c0196725c 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -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(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java index 9260cfb382..3204f5213e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/jakarta/JakartaSchemaToolingTests.java @@ -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 );