本人的数据库配置如下
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.fytri"/>

<!-- ****************  MySQL********************* -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/fytriic?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="user" value="js"/>
<property name="password" value="123456"/>
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="10"/>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="150"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="10"/>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
 <property name="mappingResources">
    <list>      
      <value>com/fytri/model/goods/Goods.hbm.xml</value>      </list>
</property>
 <property name="hibernateProperties">
 <value>
      hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
      hibernate.hbm2ddl.auto=update
      hibernate.show_sql=true
      hibernate.format_sql=false
  </value>
 </property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
      ......省略对应的bean
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>///java程序使用如下
         String sql="from Emailnotice";
         Session session=this.getSessionFactory().openSession();
     Query query =session.createQuery(sql);
     List list=query.list();
     try{session.close();}catch(Exception ex){ex.printStackTrace();}
     //System.out.println("connection is close:"+(session.isOpen()==true));       //此够运行结果为false;
return list;
//问题如下,
| 1790 | js   | localhost:4280 | fytriic | Sleep   |   17 |       | NULL
     |
| 1791 | js   | localhost:4281 | fytriic | Sleep   |   17 |       | NULL
     |
| 1792 | js   | localhost:4282 | fytriic | Sleep   |   17 |       | NULL
     |
| 1797 | js   | localhost:4287 | fytriic | Sleep   |   16 |       | NULL
     |
| 1798 | js   | localhost:4288 | fytriic | Sleep   |   16 |       | NULL
     |
| 1799 | js   | localhost:4289 | fytriic | Sleep   |   16 |       | NULL
     |
| 1800 | js   | localhost:4290 | fytriic | Sleep   |   16 |       | NULL
     |
| 1801 | js   | localhost:4291 | fytriic | Sleep   |   16 |       | NULL
     |
| 1802 | js   | localhost:4292 | fytriic | Sleep   |   16 |       | NULL
     |
+------+------+----------------+---------+---------+------+-------+------------
-----+
50 rows in set (0.00 sec)
数据库连接不断增多,直到too many connections
请各位大侠帮下忙,谢谢

解决方案 »

  1.   

    It seems that sessions have not been closed actually :1st try this.getSessionFactory().closeSession() 2nd change session to use getHibernateTemplate().createQuery(sql);
      

  2.   

    thanks yyz2007
    the method2 is the same result,I had try.
    the method1, I try to find this method, but don't have it. this.getSessionFactory() don't have the method closeSession(). don't you know why.
      

  3.   

    你只open Session 没见到你去 close呀? open 每次都会新建吧另外看文档上说 getSession 这个得到的session 不用你自己去关闭 像楼上说的 如果你用了spring 就让spring托管hibernate的session 用 getHibernateTemplate就可以了
      

  4.   

    你的 DAO 必须 extends HibernateDaoSupport
      

  5.   

    你的 DAO 必须 extends HibernateDaoSupport
      

  6.   

    It seems that annotation transaction not closed? You can check it.
      

  7.   

    extends HibernateDaoSupport
    super.getHibernateTemplate().createQuery(sql);
      

  8.   

    String sql="from Emailnotice";
      Session session=this.getSessionFactory().openSession();
      Query query =session.createQuery(sql);
      List list=query.list();
      try{session.close();}catch(Exception ex){ex.printStackTrace();}
      //System.out.println("connection is close:"+(session.isOpen()==true)); //此够运行结果为false;
    return list;
    有close 呀,另getHibernateTemplate().createQuery(sql)的效果一样