我正在尝试在独立的Web应用程序中实现jdbc-pool(自包含 - 不依赖于server.xml),以便可以将其移动到可能早于7.0的tomcat安装。
我使用sourceforge驱动程序(net.sourceforge.jtds.jdbc.Driver)连接到MSSQL Server
除了这个错误外,一切都运行良好:
严重:Web应用程序[/ jdbc-pool]似乎已经启动了 名为[[Pool-Cleaner]:Tomcat连接池[1-12524859]]的线程但是 未能阻止它。这很可能会造成内存泄漏。
基于this我确定我需要关闭jdbc-pool数据源。我在这篇帖子的最后一行遇到了麻烦:
>>如果在应用程序上下文中配置它,那么这很简单 表示您忘记在连接池上调用DataSource.close 您的Web应用程序已停止。
>这是令人困惑的建议,因为javax.sql.DataSource没有 close()方法。
为了调用close,必须将其强制转换为数据 您正在使用的来源。
如何找出我正在使用的数据源类型以及它的类在哪里?我可以以某种方式从驱动程序罐中提取它吗?
除了使用池的servlet之外,我正在使用ServletContextListener,以便我可以立即从contextInitialized方法开始使用池连接。我开始添加代码以在此ServletContextListener的contextDestroyed方法中终止连接,但挂起问号所在的位置:
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
public class JdbcPoolListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent myServletContextEvent) {
// initialize jdbc-pool datasource to start out with pooled connections
try {
Context myContext = (Context) new InitialContext().lookup("java:comp/env");
DataSource myDataSource = (DataSource) myContext.lookup("jdbc/db");
myServletContextEvent.getServletContext().setAttribute("JdbcPool", myDataSource);
} catch (NamingException e) {
System.out.println("Error initializing jdbc-pool datasource");
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent myServletContextEvent) {
// failed attempt to close the data source
ServletContext myServletContext = myServletContextEvent.getServletContext();
//DataSource myDataSource = (DataSource) myServletContext.getAttribute("JdbcPool");
DataSource dataSource = (DataSource)((???) myServletContext.getAttribute(contextAttribute)).getConfiguration().getEnvironment().getDataSource();
dataSource.close();
myServletContext.removeAttribute("JdbcPool");
// deregister JDBC driver to prevent Tomcat 7 from complaining about memory leaks
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
System.out.println(String.format("Deregistering jdbc driver: %s", driver));
} catch (SQLException e) {
System.out.println(String.format("Error deregistering driver %s", driver));
e.printStackTrace();
}
}
}
}
答案 0 :(得分:2)
解决了这个问题,我注意到tomcat.jdbc.pool.DataSourceProxy
有一个close方法,因此我将数据源转换为DataSourceProxy
,然后在其上调用closed。我现在不再在日志中获得tomcat内存泄漏错误。
<强> SOLUTION:强>
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
public class JdbcPoolListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent myServletContextEvent) {
// initialize jdbc-pool datasource to start out with pooled connections
try {
Context myContext = (Context) new InitialContext().lookup("java:comp/env");
DataSource myDataSource = (DataSource) myContext.lookup("jdbc/cf");
myServletContextEvent.getServletContext().setAttribute("JdbcPool", myDataSource);
} catch (NamingException e) {
System.out.println("Error initializing jdbc-pool datasource");
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent myServletContextEvent) {
// close datasource from proxy?
ServletContext myServletContext = myServletContextEvent.getServletContext();
DataSourceProxy myDataSource = (DataSourceProxy) myServletContext.getAttribute("JdbcPool");
myDataSource.close();
myServletContext.removeAttribute("JdbcPool");
// deregister JDBC driver to prevent Tomcat 7 from complaining about memory leaks
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
System.out.println(String.format("Deregistering jdbc driver: %s", driver));
} catch (SQLException e) {
System.out.println(String.format("Error deregistering driver %s", driver));
e.printStackTrace();
}
}
}
}