如何避免 DB2 驱动程序类加载器 Tomcat 应用程序 .war 文件重新部署上的内存泄漏

use*_*343 4 java db2 tomcat jndi connection-pooling

IBM 良好支持的 JDBC 驱动程序与 Tomcat 良好支持的连接池相结合会产生内存泄漏。请参考Tomcat应用程序.war文件重新部署时的Classloader内存泄漏。

java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [DB2JccConfiguration.properties]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
    at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1327)
    at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1023)
    at com.ibm.db2.jcc.am.ud.run(Unknown Source)
    at java.security.AccessController.doPrivileged(AccessController.java:285)
    at com.ibm.db2.jcc.am.GlobalProperties.a(Unknown Source)
    at com.ibm.db2.jcc.am.GlobalProperties.d(Unknown Source)
    at com.ibm.db2.jcc.am.mq.run(Unknown Source)
    at java.util.TimerThread.mainLoop(Timer.java:567)
    at java.util.TimerThread.run(Timer.java:517)
Run Code Online (Sandbox Code Playgroud)

我不理解建议的解决方案,因为它与最推荐的将驱动程序 jar 包含在 Tomcat lib 目录中的做法相冲突。

我们需要共享部署和重新部署而不需要重新启动Tomcat。如果您有使用此软件组合和所描述问题的经验,请在此处分享您的解决方案。

pha*_*t0m 7

对于驱动程序版本 4.22.29,我当前在 ServletContextListener 中使用以下代码:

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    // This fixes the JDBC driver not unloading corectly on a context reload  for DB2 JDBC 4.22.29
    try {
        logger.debug("Trying to stop the timer");
        new com.ibm.db2.jcc.am.iq() {
            // instance initializer to execute the fix when the anonymous class is instantiated, i.e. now
            {
                if (a != null) {
                    a.cancel();
                } else {
                    logger.debug("Timer is null, skipped");
                }
            }
        };
        logger.debug("Stopped the timer");
    } catch (Exception e) {
        logger.error("Could not stop the DB2 timer thread", e);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:由于 DB2 驱动程序 JAR 似乎已被混淆,因此计时器存储 ( com.ibm.db2.jcc.am.iq.a) 对于其他驱动程序版本可能会有所不同。另外,您要子类化的类的构造函数可能会有副作用,在我的例子中没有副作用。

我是如何得到这个解决方案的

获取异常

java.lang.NullPointerException
    at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1600)
    at com.ibm.db2.jcc.am.wd.run(wd.java:49)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.ibm.db2.jcc.am.GlobalProperties.a(GlobalProperties.java:146)
    at com.ibm.db2.jcc.am.GlobalProperties.d(GlobalProperties.java:100)
    at com.ibm.db2.jcc.am.dr.run(dr.java:124)  <------- point of interest <----------
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Run Code Online (Sandbox Code Playgroud)

计时器的主要类是com.ibm.db2.jcc.am.dr.

使用 IntelliJ,我在其构造函数中设置了一个断点。等待断点命中:

在 GlobalProperties 中实例化

转到实例化的位置,在我的例子中是 GlobalProperties。查看安排的计时器。

iq.a.schedule()

找到一种访问方式iq.a:由于这是一个静态的受保护字段,我们可以从该类继承iq并从该类内部,访问父类的静态字段来cancel()调用a