Tomcat 6.0.32 +Spring dbcp datasource关闭Tomcat出现严重异常
异常如下:
信息: Pausing Coyote HTTP/1.1 on http-8080 2014-3-6 14:52:50 org.apache.catalina.core.StandardService stop 信息: Stopping service Catalina 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 严重: The web application [/tsmanager] 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. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-2] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-3] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-4] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-5] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-6] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-7] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-8] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-9] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/tsmanager] appears to have started a thread named [scheduler_Worker-10] but has failed to stop it. This is very likely to create a memory leak. 2014-3-6 14:52:50 org.apache.coyote.http11.Http11Protocol destroy 信息: Stopping Coyote HTTP/1.1 on http-8080 [QC] INFO [main] org.springframework.context.support.AbstractApplicationContext.doClose(1002) | Closing Root WebApplicationContext: startup date [Thu Mar 06 14:52:41 CST 2014]; root of context hierarchy [QC] INFO [main] org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.stop(345) | Stopping beans in phase 2147483647 [QC] INFO [main] org.quartz.core.QuartzScheduler.standby(556) | Scheduler scheduler_$_yanshiying-PC1394088762202 paused. [QC] INFO [main] org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(422) | Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ac7fbb: defining beans [dataSource,jdbcTemplate,xxtsService,wpushDao,httpUtil,cronService,clearDataJob,getNsrInfoJob,scheduler,quartzManager]; root of factory hierarchy [QC] INFO [main] org.springframework.scheduling.quartz.SchedulerFactoryBean.destroy(760) | Shutting down Quartz Scheduler [QC] INFO [main] org.quartz.core.QuartzScheduler.shutdown(635) | Scheduler scheduler_$_yanshiying-PC1394088762202 shutting down. [QC] INFO [main] org.quartz.core.QuartzScheduler.standby(556) | Scheduler scheduler_$_yanshiying-PC1394088762202 paused. [QC] INFO [main] org.quartz.core.QuartzScheduler.shutdown(707) | Scheduler scheduler_$_yanshiying-PC1394088762202 shutdown complete.
Tomcat版本为:6.0.32
spring数据源配置如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/wzhpush"/> <property name="username" value="root"/> <property name="password" value="mysqladmin"/> </bean>
原因:https://issues.apache.org/jira/browse/DBCP-332
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.
解决方法:
import java.sql.DriverManager; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; /** * 解决 关闭Tomcat时报错:the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped * @author*/ public class WzhBasicDataSource extends BasicDataSource{ @Override public synchronized void close() throws SQLException { DriverManager.deregisterDriver(DriverManager.getDriver(url)); super.close(); } }