如何关闭上下文卸载连接池? [英] How to shutdown connection pool on context unload?
问题描述
在开发了几个webapps之后,所有的spring都有类似的连接池,hibernate和c3p0作为connectionpool我想调查一下我每次注意到的问题:
连接池保持连接,直到关闭tomcat(或者你的应用服务器) 。
今天我用这四个依赖关系创建了最基本的项目:
org.springframework:spring-web
org.springframework:spring-orm
org.hibernate:hibernate-core
c3p0:c3p0
(加上特定的JDBC驱动程序)。
我的web.xml只创建一个ContextLoaderListener
< context-param>设置应用程序上下文。
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/applicationContext.xml
< / param-value>
< / context-param>
< listener>
< listener-class>
org.springframework.web.context.ContextLoaderListener
< / listener-class>
< / listener>
应用程序上下文由两个bean组成 - 数据源和会话工厂:
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< / bean>
< bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource>
< property name =driverClass>
< value> org.postgresql.Driver< / value>
< / property>
< property name =jdbcUrl>
<值> jdbc:postgresql:// localhost / mydb< /值>
< / property>
< property name =user>
<值> usr< /值>
< / property>
< property name =password>
<值> pwd< /值>
< / property>
< / bean>
当我启动webapp并查看jconsole的MBeans或检查我的DBMS的打开连接时,我注意到
问题:当我告诉tomcat停止webapp时,它们仍然存在!
我创建了另一个ServletContextListener,它只实现了contextDestroyed方法,并以编程方式关闭sessionFactory(并且还注销了也未自动完成的JDBC驱动程序)。
代码是:
@Override
public void contextDestroyed(ServletContextEvent sce){
...
sessionFactory()。close();
//取消注册sql驱动程序
枚举< Driver> drivers = DriverManager.getDrivers();
while(drivers.hasMoreElements()){
Driver driver = drivers.nextElement();
尝试{
DriverManager.deregisterDriver(driver);
log.info(deregistering jdbc driver:+ driver);
} catch(SQLException e){
log.error(error deregistering jdbc driver:+ driver,e);
}
}
}
但是,真的吗?有没有内置的机制,我不知道?
你有没有试图指定销毁方法? / p>
< bean class =com.mchange.v2.c3p0.ComboPooledDataSourcedestroy-method =close>
after developing several webapps that all had a similar setup with spring, hibernate and c3p0 as connectionpool I wanted to investigate a problem that i noticed everytime: Connectionpool keeps the connections until you shutdown tomcat (or your application server).
Today i create the most basic project I could with these four dependencies:
org.springframework:spring-web
org.springframework:spring-orm
org.hibernate:hibernate-core
c3p0:c3p0
(plus the specific JDBC driver).
My web.xml only creates a ContextLoaderListener that sets up the application context.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
application context consists of two beans - datasource and session factory:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>org.postgresql.Driver</value>
</property>
<property name="jdbcUrl">
<value>jdbc:postgresql://localhost/mydb</value>
</property>
<property name="user">
<value>usr</value>
</property>
<property name="password">
<value>pwd</value>
</property>
</bean>
When i start the webapp and either look into jconsole's MBeans or check my DBMS for open connections i notice the three initial connections made by c3p0.
PROBLEM: When i tell tomcat to stop the webapp, they still remain!
I created another ServletContextListener that only has the contextDestroyed method implemented and programatically shuts down the sessionFactory (and also deregisters the JDBC drivers which is also not done automatically). Code is:
@Override
public void contextDestroyed(ServletContextEvent sce) {
...
sessionFactory().close();
// deregister sql driver(s)
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
log.info("deregistering jdbc driver: " + driver);
} catch (SQLException e) {
log.error("error deregistering jdbc driver: " + driver, e);
}
}
}
But is that really it? Is there no built-in mechanism that I am not aware of?
Have you tried to specify the destroy-method?
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
这篇关于如何关闭上下文卸载连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!