|
54 | 54 | import org.hibernate.cfg.AttributeConverterDefinition; |
55 | 55 | import org.hibernate.cfg.Environment; |
56 | 56 | import org.hibernate.cfg.beanvalidation.BeanValidationIntegrator; |
| 57 | +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; |
57 | 58 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
58 | 59 | import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory; |
59 | 60 | import org.hibernate.integrator.spi.Integrator; |
|
77 | 78 | import org.hibernate.secure.spi.GrantedPermission; |
78 | 79 | import org.hibernate.secure.spi.JaccPermissionDeclarations; |
79 | 80 | import org.hibernate.service.ServiceRegistry; |
| 81 | +import org.hibernate.service.spi.ServiceBinding; |
80 | 82 | import org.hibernate.service.spi.ServiceRegistryImplementor; |
| 83 | +import org.hibernate.service.spi.Stoppable; |
81 | 84 | import org.hibernate.tool.schema.spi.DelayedDropRegistryNotAvailableImpl; |
82 | 85 | import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; |
83 | 86 |
|
@@ -1213,9 +1216,24 @@ public EntityManagerFactoryBuilder withDataSource(DataSource dataSource) { |
1213 | 1216 |
|
1214 | 1217 | @Override |
1215 | 1218 | public void cancel() { |
| 1219 | + cleanup(); |
1216 | 1220 | // todo : close the bootstrap registry (not critical, but nice to do) |
1217 | 1221 | } |
1218 | 1222 |
|
| 1223 | + private void cleanup() { |
| 1224 | + // Stop and de-register the ConnectionProvider to prevent connections lying around |
| 1225 | + if ( standardServiceRegistry instanceof ServiceRegistryImplementor && |
| 1226 | + standardServiceRegistry instanceof ServiceBinding.ServiceLifecycleOwner ) { |
| 1227 | + final ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) standardServiceRegistry; |
| 1228 | + final ServiceBinding.ServiceLifecycleOwner lifecycleOwner = (ServiceBinding.ServiceLifecycleOwner) serviceRegistry; |
| 1229 | + final ServiceBinding<ConnectionProvider> binding = serviceRegistry.locateServiceBinding( ConnectionProvider.class ); |
| 1230 | + if ( binding != null && binding.getService() instanceof Stoppable ) { |
| 1231 | + lifecycleOwner.stopService( binding ); |
| 1232 | + binding.setService( null ); |
| 1233 | + } |
| 1234 | + } |
| 1235 | + } |
| 1236 | + |
1219 | 1237 | /** |
1220 | 1238 | * Used by extensions : Hibernate Reactive |
1221 | 1239 | */ |
@@ -1245,21 +1263,32 @@ public void generateSchema() { |
1245 | 1263 | catch (Exception e) { |
1246 | 1264 | throw persistenceException( "Error performing schema management", e ); |
1247 | 1265 | } |
1248 | | - |
1249 | | - // release this builder |
1250 | | - cancel(); |
| 1266 | + finally { |
| 1267 | + // release this builder |
| 1268 | + cancel(); |
| 1269 | + } |
1251 | 1270 | } |
1252 | 1271 |
|
1253 | 1272 | @Override |
1254 | 1273 | public EntityManagerFactory build() { |
1255 | | - final SessionFactoryBuilder sfBuilder = metadata().getSessionFactoryBuilder(); |
1256 | | - populateSfBuilder( sfBuilder, standardServiceRegistry ); |
1257 | | - |
| 1274 | + boolean success = false; |
1258 | 1275 | try { |
1259 | | - return sfBuilder.build(); |
| 1276 | + final SessionFactoryBuilder sfBuilder = metadata().getSessionFactoryBuilder(); |
| 1277 | + populateSfBuilder( sfBuilder, standardServiceRegistry ); |
| 1278 | + |
| 1279 | + try { |
| 1280 | + final EntityManagerFactory emf = sfBuilder.build(); |
| 1281 | + success = true; |
| 1282 | + return emf; |
| 1283 | + } |
| 1284 | + catch (Exception e) { |
| 1285 | + throw persistenceException( "Unable to build Hibernate SessionFactory", e ); |
| 1286 | + } |
1260 | 1287 | } |
1261 | | - catch (Exception e) { |
1262 | | - throw persistenceException( "Unable to build Hibernate SessionFactory", e ); |
| 1288 | + finally { |
| 1289 | + if ( !success ) { |
| 1290 | + cleanup(); |
| 1291 | + } |
1263 | 1292 | } |
1264 | 1293 | } |
1265 | 1294 |
|
|
0 commit comments