java-MySQL语句取消计时器无法停止
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-MySQL语句取消计时器无法停止,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4580字,纯文字阅读大概需要7分钟。
内容图文
![java-MySQL语句取消计时器无法停止](/upload/InfoBanner/zyjiaocheng/887/7138fd8203ac4bae85447ab381be070a.jpg)
以下警告的原因是什么?
WARNING: The web application [TI] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Apr 28, 2016 11:03:25 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [TI] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:503)
java.util.TimerThread.mainLoop(Timer.java:526)
java.util.TimerThread.run(Timer.java:505)
每当我对代码进行更改后,tomcat就会自行重启.
我正在创建mysql连接,我想我正确关闭了它.
这是我的代码.
@RequestMapping(value = "/file/md5", method = RequestMethod.POST)
public ResponseEntity<?> md5Status(@RequestParam(required = true) String filename) {
Connection con = SqlUtility.getMysqlConnection(host, port, database,username, password);
SqlUtility.closeDbInstances(SqlUtility.mysqlConnection);
SqlUtility.closeDbInstances(con);
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
这是实用程序代码.
public static Connection mysqlConnection = null;
private static PreparedStatement preparedStatement = null;
private static ResultSet resultSet = null;
public static Connection getMysqlConnection(String host, int port, String database, String username,
String password) {
MysqlDataSource mds = null;
try {
if (mysqlConnection == null || mysqlConnection.isClosed()) {
if (database != null && host != null) {
while (mds == null) {
System.out.println("In Loop");
mds = new MysqlDataSource();
mds.setServerName(host);
mds.setPortNumber(port);
mds.setDatabaseName(database);
mds.setUser(username);
mds.setPassword(password);
try {
mysqlConnection = mds.getConnection();
} catch (CommunicationsException e) {
System.out.format(
"Error occurred while trying to get connection with database [%s]. Are your database host [%s] and port [%s] correct? %n%s%n",
database, host, port, e);
mds = null;
// Had to handle for retrying again and again
// This is because your mysql is bind on localhost change it in my.cfg
}
}
} else {
System.out.format(
"Error occurred while trying to get connection with database [%s] with username [%s] and password [%s].\n [ATTN] Make sure your DB adapter properties file contains all required attributes. %n",
database, username, password);
}
}
} catch (SQLException e) {
System.out.format(
"Error occurred while trying to get connection at host [%s] with database [%s] with username [%s] and password [%s] %n%s%n",
host, database, username, password, e);
// It might be because of the database privileges which must be grant to access
} catch (RuntimeException e) {
System.out.format("Error occurred while trying to get [%s] Connection %n%s%n", database, e);
}
if (mysqlConnection != null)
System.out.format("[%S] Connection Successfully Created%n", database);
return mysqlConnection;
}
public static void closeDbInstances(Connection con) {
System.out.println("Trying to close Database Connection...");
if (con != null && preparedStatement != null && resultSet != null) {
try {
resultSet.close();
preparedStatement.close();
con.close();
System.out.println("Database Connection Closed");
} catch (SQLException e) {
System.out.println("Error occurred while trying to close Database connection " + e);
}
}
}
解决方法:
(旧问题,但我遇到了相同的错误/警告消息)
您是否将JDBC池与QueryTimeoutInterceptor一起使用或调用statement.setQueryTimeout(320)函数.它调用一个内部mysqljdbc.jar线程来取消长时间运行的查询.
Tomcat hotdeployment无法识别它,因此线程留在后面运行,并在RAM中保持无效的Webapp上下文.
<Resource name="jdbc/BSManager" auth="Container"
type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="2" maxActive="100" maxIdle="10" maxWait="30000"
username="myuser" password="mypwd"
driverClassName="com.mysql.jdbc.Driver"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.QueryTimeoutInterceptor(queryTimeout=320)"
url="jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8"
validationQuery="SELECT 1" removeAbandoned="true" removeAbandonedTimeout="7200"
testOnBorrow="true" maxAge="1800000"
/>
此警告消息Web应用程序[TI]似乎已经启动了一个名为[MySQL语句取消计时器]的线程,但是即使在tomcat / conf / server.xml文件中激活了此侦听器,也未能停止它.我不知道查询超时后是否释放了死的webapp.我决定不对MySQL jdbc驱动程序使用查询超时.
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"
classesToInitialize="com.mysql.jdbc.NonRegisteringDriver" />
内容总结
以上是互联网集市为您收集整理的java-MySQL语句取消计时器无法停止全部内容,希望文章能够帮你解决java-MySQL语句取消计时器无法停止所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。