Hibernate : closing the session factory does not close the c3p0 connection pool

寵の児 提交于 2019-11-28 08:42:47

I had the same issue and successfully used the work-around offered in this bug report:

private void closeSessionFactory(SessionFactory factory) { 
   if(factory instanceof SessionFactoryImpl) {
      SessionFactoryImpl sf = (SessionFactoryImpl)factory;
      ConnectionProvider conn = sf.getConnectionProvider();
      if(conn instanceof C3P0ConnectionProvider) { 
        ((C3P0ConnectionProvider)conn).close(); 
      }
   }
   factory.close();
}

You have to reference the hibernate-c3p0-4.x.x jar.

I had the same issue and successfully used the ehnanced (January 2014) work-around offered in this bug report:

private static boolean closeSessionFactoryIfC3P0ConnectionProvider(SessionFactory factory) {

    boolean done = false;
    if(factory instanceof SessionFactoryImpl) {
        SessionFactoryImpl sf = (SessionFactoryImpl)factory;
        ConnectionProvider conn = sf.getConnectionProvider();
        if(conn instanceof C3P0ConnectionProvider) { 
            ((C3P0ConnectionProvider)conn).close(); 
            try {
                Thread.sleep(2000); //Let give it time...it is enough...probably
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            done = true;
        }
        factory.close();
    }
    return done;

}

You have to reference the hibernate-c3p0-4.x.x jar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!