diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/LeakingStatementCachingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/LeakingStatementCachingTest.java index 59e0f360e26f..0d37fe5224b6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/LeakingStatementCachingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/LeakingStatementCachingTest.java @@ -85,7 +85,12 @@ class LeakingStatementCachingTest { @AfterAll public void tearDown(SessionFactoryScope scope) { - SINGLE_CONNECTION_DATASOURCE.close(); + try { + scope.getSessionFactory().getSchemaManager().dropMappedObjects( false ); + } + finally { + SINGLE_CONNECTION_DATASOURCE.close(); + } } @Test @@ -127,7 +132,8 @@ public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) { serviceRegistryBuilder.applySetting( AvailableSettings.CONNECTION_PROVIDER, new DataSourceConnectionProvider() ); - serviceRegistryBuilder.applySetting( AvailableSettings.SHOW_SQL, false ); + serviceRegistryBuilder.applySetting( AvailableSettings.SHOW_SQL, true ); + serviceRegistryBuilder.applySetting( AvailableSettings.HBM2DDL_AUTO, "create" ); serviceRegistryBuilder.applySetting( AvailableSettings.LOG_JDBC_WARNINGS, false ); serviceRegistryBuilder.getSettings().remove( AvailableSettings.URL ); } @@ -135,30 +141,39 @@ public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) { private static class SingleConnectionDataSource implements javax.sql.DataSource { - private final BlockingQueue connectionQueue = new ArrayBlockingQueue<>( 1 ); + private BlockingQueue connectionQueue; public SingleConnectionDataSource() { - DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl(); - connectionProvider.configure( - PropertiesHelper.map( ConnectionProviderBuilder.getConnectionProviderProperties() ) ); - try { - connectionQueue.add( new ConnectionWrapper( connectionProvider.getConnection(), this ) ); - } - catch (SQLException e) { - throw new RuntimeException( e ); - } } @Override public Connection getConnection() { try { - return connectionQueue.take(); + if ( connectionQueue == null ) { + connectionQueue = new ArrayBlockingQueue<>( 1 ); + connectionQueue.add( buildConnectionWrapper() ); + } + if ( connectionQueue.isEmpty() ) { + throw new IllegalStateException( "A problem occurred retrieving the connection" ); + } + Connection connection = connectionQueue.take(); + if ( connection == null || connection.isClosed() ) { + throw new IllegalStateException( "A problem occurred retrieving the connection" ); + } + return connection; } - catch (InterruptedException e) { + catch (Exception e) { throw new RuntimeException( e ); } } + private ConnectionWrapper buildConnectionWrapper() throws SQLException { + DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl(); + connectionProvider.configure( + PropertiesHelper.map( ConnectionProviderBuilder.getConnectionProviderProperties() ) ); + return new ConnectionWrapper( connectionProvider.getConnection(), this ); + } + @Override public Connection getConnection(String username, String password) { throw new UnsupportedOperationException();