@@ -85,7 +85,12 @@ class LeakingStatementCachingTest {
8585
8686 @ AfterAll
8787 public void tearDown (SessionFactoryScope scope ) {
88- SINGLE_CONNECTION_DATASOURCE .close ();
88+ try {
89+ scope .getSessionFactory ().getSchemaManager ().dropMappedObjects ( false );
90+ }
91+ finally {
92+ SINGLE_CONNECTION_DATASOURCE .close ();
93+ }
8994 }
9095
9196 @ Test
@@ -127,38 +132,48 @@ public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) {
127132
128133 serviceRegistryBuilder .applySetting ( AvailableSettings .CONNECTION_PROVIDER ,
129134 new DataSourceConnectionProvider () );
130- serviceRegistryBuilder .applySetting ( AvailableSettings .SHOW_SQL , false );
135+ serviceRegistryBuilder .applySetting ( AvailableSettings .SHOW_SQL , true );
136+ serviceRegistryBuilder .applySetting ( AvailableSettings .HBM2DDL_AUTO , "create" );
131137 serviceRegistryBuilder .applySetting ( AvailableSettings .LOG_JDBC_WARNINGS , false );
132138 serviceRegistryBuilder .getSettings ().remove ( AvailableSettings .URL );
133139 }
134140 }
135141
136142 private static class SingleConnectionDataSource implements javax .sql .DataSource {
137143
138- private final BlockingQueue <Connection > connectionQueue = new ArrayBlockingQueue <>( 1 ) ;
144+ private BlockingQueue <Connection > connectionQueue ;
139145
140146 public SingleConnectionDataSource () {
141- DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl ();
142- connectionProvider .configure (
143- PropertiesHelper .map ( ConnectionProviderBuilder .getConnectionProviderProperties () ) );
144- try {
145- connectionQueue .add ( new ConnectionWrapper ( connectionProvider .getConnection (), this ) );
146- }
147- catch (SQLException e ) {
148- throw new RuntimeException ( e );
149- }
150147 }
151148
152149 @ Override
153150 public Connection getConnection () {
154151 try {
155- return connectionQueue .take ();
152+ if ( connectionQueue == null ) {
153+ connectionQueue = new ArrayBlockingQueue <>( 1 );
154+ connectionQueue .add ( buildConnectionWrapper () );
155+ }
156+ if ( connectionQueue .isEmpty () ) {
157+ throw new IllegalStateException ( "A problem occurred retrieving the connection" );
158+ }
159+ Connection connection = connectionQueue .take ();
160+ if ( connection == null || connection .isClosed () ) {
161+ throw new IllegalStateException ( "A problem occurred retrieving the connection" );
162+ }
163+ return connection ;
156164 }
157- catch (InterruptedException e ) {
165+ catch (Exception e ) {
158166 throw new RuntimeException ( e );
159167 }
160168 }
161169
170+ private ConnectionWrapper buildConnectionWrapper () throws SQLException {
171+ DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl ();
172+ connectionProvider .configure (
173+ PropertiesHelper .map ( ConnectionProviderBuilder .getConnectionProviderProperties () ) );
174+ return new ConnectionWrapper ( connectionProvider .getConnection (), this );
175+ }
176+
162177 @ Override
163178 public Connection getConnection (String username , String password ) {
164179 throw new UnsupportedOperationException ();
0 commit comments