我们在F4实例上部署了GAE标准应用程序,其中Java8连接到CloudSQL PG数据库。我们设置了一个类PostgresPool implements ServletContextListener
,最初创建了一个包含10个连接的连接池。在部署过程中,所有操作均按预期工作,并且可以看到已创建的连接,但是在升级应用程序时,我们希望继续使用现有池,或者销毁现有池并重新创建一个新池,但是我们只能得到一个另外10个打开的连接,我们必须手动重新启动数据库实例或删除以前的版本。
我知道连接池是在ServletContext上创建的,似乎新部署正在创建自己的新ServletContext,因此代码最终没有检测到前一个池来关闭它。
在知道GAE似乎不调用contextDestroyed方法的情况下,这里的最佳实践是什么?需要做些什么来确保我们最终不会出现数百个空闲连接?
这是我们的代码段,主要是contextInitialized和contextDestroyed(不确定是否在App Engine上调用了该代码)
private static ServletContext servletContext = null;
@Override
public void contextInitialized(ServletContextEvent sce) {
servletContext = sce.getServletContext();
DataSource pool = (DataSource) servletContext.getAttribute(POOL_NAME);
try
{
if (pool == null || pool.getConnection() == null) {
pool = createConnectionPool();
servletContext.setAttribute(POOL_NAME, pool);
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
HikariDataSource pool = (HikariDataSource) servletContext.getAttribute(POOL_NAME);
if (pool != null) {
pool.close();
}
servletContext = null;
}