| See http://stackoverflow.com/questions/38978468/hibernate-4-and-5-does-not-close-c3p0-connection-pool-if-the-database-is-down Configure Hibernate: public void init(...) { try { serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).enableAutoClose().build(); config.buildSessionFactory(serviceRegistry); } catch (Exception e) { closeSessionFactory(...); } } Close Hibernate session factory: public synchronized static void closeSessionFactory(...) { sessionFactory.close(); StandardServiceRegistryBuilder.destroy(serviceRegistry); } Show c3p0 pools: public static List<String> getPooledDataSources(){ ArrayList<String> pdsList = new ArrayList<>(); @SuppressWarnings("rawtypes") Set sources = C3P0Registry.getPooledDataSources(); for (Object source : sources) { PooledDataSource current = (PooledDataSource) source; pdsList.add("Name: " + current.getDataSourceName() + ". C3P0 identity token: " + current.getIdentityToken()); } return pdsList; } Now, if I configure then close Hibernate multiple times while my application runs, there is no problem. Printing the C3P0 pooled data sources shows that there is only one PooledDataSource. But if I apply the following rule on the database server: iptables -I INPUT -s webServerIpAddress -j DROP Causing it to drop packets from my application, then closing and reconfiguring Hibernate a couple times... now printing the C3P0 pooled data sources shows that there are multiple pooled data sources: Hibernate isn't closing the old ones. Why? This problem on all versions of Hibernate I have tested, including 5.2.2 the newest version at time of this writing. |