See http Edit : //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 { Issue was actually that my serviceRegistry = new StandardServiceRegistryBuilder destroy () .applySettings(config.getProperties()).enableAutoClose().build(); config.buildSessionFactory(serviceRegistry); } catch ( was not being called due to an Exception e) { closeSessionFactory( thrown elsewhere . ..); } }
Close Hibernate session factory:
public synchronized static void closeSessionFactory The close ( ... ) { sessionFactory. call on the SessionFactory does not actually close() ; StandardServiceRegistryBuilder. the underlying c3p0 conenction pool, but the serviceRegistry destroy( serviceRegistry ) ; }
Show c3p0 pools:
public static List<String> getPooledDataSources(){ ArrayList<String> pdsList = new ArrayList<>(); @SuppressWarnings("rawtypes") Set sources = C3P0Registry does . 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 Please 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 report . |
|