apache-tomcat 7.0.88无故停止问题

使用tomcat 7.0.88发布的java web项目,启动一段时间(大概一天左右)服务会莫名其妙的重启,在后台的catalina.out 查看日志,发现如下一段可以记录:

INFO: Deployment of web application directory /home/apache-tomcat-7.0.88/webapps/manager has finished in 47 ms
Jun 07, 2018 6:35:29 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-80"]
Jun 07, 2018 6:35:29 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 07, 2018 6:35:29 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 14940 ms
Jun 08, 2018 8:35:14 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-80"]
Jun 08, 2018 8:35:14 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-bio-8009"]
Jun 08, 2018 8:35:14 AM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Jun 08, 2018 8:35:14 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing WebApplicationContext for namespace 'spring-servlet': startup date [Thu Jun 07 06:35:21 CST 2018]; parent: Root WebApplicationContext
Jun 08, 2018 8:35:14 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing Root WebApplicationContext: startup date [Thu Jun 07 06:35:21 CST 2018]; root of context hierarchy
Jun 08, 2018 8:35:14 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
SEVERE: The web application [] registered the JDBC driver [oracle.jdbc.driver.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Jun 08, 2018 8:35:14 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing WebApplicationContext for namespace 'spring-servlet': startup date [Thu Jun 07 06:35:27 CST 2018]; parent: Root WebApplicationContext
Jun 08, 2018 8:35:14 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing Root WebApplicationContext: startup date [Thu Jun 07 06:35:27 CST 2018]; root of context hierarchy
Jun 08, 2018 8:35:14 AM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-bio-80"]
Jun 08, 2018 8:35:14 AM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["ajp-bio-8009"]
Jun 08, 2018 8:35:14 AM org.ap

这句话引起了我的质疑“[oracle.jdbc.driver.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Jun 08, 2018 8:35:14 AM org.springframework.context.support.AbstractApplicationContext doClose”
大概意思是 当web停止的时候,jdbc注销失败,为了防止内存泄漏,jdbc驱动需要强制注销。
但是结果还是停止了,这有可能是强制注销失败,导致了内存泄漏。
查看tomcat官网有如下解释:
因为tomcat自带了DBCP数据库连接池,很多用户在使用DBCP时遇到了这个问题,并建议在 DBCP 的 BasicDataSource的close方法里执行反注册驱动的行为来解决这个警告。但DBCP的开发者认为这个应该是使用者的责任,不愿意接受这种建议,具体信息请查https://issues.apache.org/jira/browse/DBCP-332
我摘录了一段“
Description
BasicDataSource’s method close() doesn’t deregister JDBC driver. This causes permgen memory leaks in web server environments, during context reloads. For example, using Tomcat 6.0.26 with Spring, and BasicDataSource declared in Spring context, there is a message printed at web application reload:

SEVERE: A web application registered the JBDC 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.

I was able to fix it by overriding close method this way:

public class XBasicDataSource extends BasicDataSource {
@Override
public synchronized void close() throws SQLException {
DriverManager.deregisterDriver(DriverManager.getDriver(url));
super.close();
}
}
but I think it should be probably the default behavior of BasicDataSource. Or perhaps there should be some flag/setting on BasicDataSource, named “deregisterDriverAtClose” or so.

基本意思是数据源不会去注销jdbc,这个原因会导致内存泄漏。
到此我们明白了,tomcat不会去注销jdbc,需要我们手动去注销,重写close()方法。
后续在关闭jdbc连接的时就会去注销jdbc。

猜你喜欢

转载自blog.csdn.net/a743044559/article/details/80617930