日期:2014-05-16  浏览次数:20689 次

spring+hibernate配置c3p0无法释放数据库连接
弄了3天,还是没有搞定。
配置:
winxp+tomcat5.0.28+hibernate3.1+spring1.2.6+struts1.0+mysql5.0.17,做了一个小blog。没有配置连接池时,运转良好。用c3p0配置后,每次都会出现3或者5个新连接。然后发现数据库连接不能释放。出现很多sleep连接。直到全部连接占满。
出现:错误表现:
javax.servlet.ServletException: Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Connections could not be acquired from the underlying database!; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database

数据库的表现是:

类似的问题,但没有都没有很好的解决方法。
引用
06-21-2006 spring论坛一问题。
http://forum.springframework.org/showthread.php?t=26124
JavaEye
求助,Spring+Hibernate连接池无法释放问题,贴出详细代码
http://www.iteye.com/topic/32378
下面是applicationContext.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!-- for properties files -->
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:c3p0.properties</value>
   </list>
  </property>
 </bean>

<!-- for dataSource -->
<bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${jdbc.driver}" />
  <property name="jdbcUrl" value="${jdbc.url}" />

  <property name="properties">
   <props>
    <prop key="c3p0.minPoolSize">${hibernate.c3p0.minPoolSize}</prop>
    <prop key="hc3p0.maxPoolSize">${hibernate.c3p0.maxPoolSize}</prop>
    <prop key="hc3p0.timeout">${hibernate.c3p0.timeout}</prop>
    <prop key="c3p0.max_statement">${hibernate.c3p0.max_statement}</prop>
    <prop key="user">${jdbc.username}</prop>
    <prop key="password">${jdbc.password}</prop>
    <prop key="c3p0.testConnectionOnCheckout">true</prop>
   </props>
  </property>
 </bean>

	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="mappingResources">
            <list>
                <value>com/voff/hibernate/Blog.hbm.xml</value>
            </list>
        </property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">
				true
				</prop>
				<prop key="hibernate.connection.provider_class">
                org.hibernate.connection.C3P0ConnectionProvider
               </prop>
			</props>
		</property>
	</bean>
	
	 <!-- transactionManager -->
	 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    
    <bean id="BlogDao" class="com.voff.dao.BlogDaoimpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    
    	<!-- spring dao proxy -->
    <bean id="BlogDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>