我知道有一些关于此的问题,答案似乎是将驱动程序放在Tomcat的lib文件夹中。但是我仍然收到警告。为什么会这样?
这是我的pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>provided</scope>
</dependency>
在我的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
这是我建立数据库连接的唯一方法:
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();
}
}
当我关闭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:
我做错了什么?
答案 0 :(得分:1)
内存泄漏是由于MySQL的“#Abandonded connection cleanup thread&#39;”所致。该线程从第一个请求开始,并保存对webapp的类加载器的引用。使用classesToInitialize
,您也可以防止此内存泄漏。
要防止此特定内存泄漏,您应编辑tomcat / conf / server.xml并更改
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
到
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" classesToInitialize="com.mysql.jdbc.NonRegisteringDriver" />