简体   繁体   English

Tomcat警告我有关内存泄漏的信息,即使mysql连接器位于lib文件夹中

[英]Tomcat warns me about memory leak even though mysql connector is in lib folder

I know there are a few questions about this and the answer seems to put the Driver in the lib folder of the Tomcat. 我知道有关此问题,答案似乎将驱动程序放入Tomcat的lib文件夹中。 However I am still getting a warning. 但是我仍然收到警告。 Why might this be? 为什么会这样呢?

Here is my pom.xml: 这是我的pom.xml:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
    <scope>provided</scope>
</dependency>

and in my Tomcat\\lib folder I have: 在我的Tomcat \\ lib文件夹中,我有:

Korays-MacBook-Pro:apache-tomcat-8.0.23 koraytugay$ ls lib/mysql-connector-java-5.1.35-bin.jar 
lib/mysql-connector-java-5.1.35-bin.jar

This is the only method I have that makes a db connection: 这是我建立数据库连接的唯一方法:

import java.sql.*;

public class TicketDAO {
    public static void insertTicketToDB(String name)
            throws ClassNotFoundException, SQLException,
            IllegalAccessException, InstantiationException {
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "ticketsdb";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "";
        String password = "";
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url + dbName, userName, password);
        String insertTableSQL = "INSERT INTO ticket(name) VALUES(?)";
        PreparedStatement preparedStatement = conn.prepareStatement(insertTableSQL);
        preparedStatement.setString(1, name);
        preparedStatement.executeUpdate();
        conn.close();
    }
}

When I shutdown Tomcat I will see: 当我关闭Tomcat时,我会看到:

23-Jun-2015 14:31:12.693 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. 

This is very likely to create a memory leak. Stack trace of thread:

What is it that I am doing wrong? 我做错了什么?

Memory leaks manifests due to MySQL's 'Abandonded connection cleanup thread'. 内存泄漏的表现是由于MySQL的“ Abandonded connection cleanup thread”。 This thread starts with the first request and holds a reference to the webapp's classloader. 该线程从第一个请求开始,并包含对Webapp的类加载器的引用。 With classesToInitialize you can prevent this memory leak too. 使用classesToInitialize也可以防止此内存泄漏。

To prevent this particular memory leak you should edit your tomcat/conf/server.xml and change 为了防止这种特殊的内存泄漏,您应该编辑tomcat / conf / server.xml并进行更改

<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

to

<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" classesToInitialize="com.mysql.jdbc.NonRegisteringDriver" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM