本人的数据库配置如下
<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&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
请各位大侠帮下忙,谢谢
<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&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
请各位大侠帮下忙,谢谢
解决方案 »
- 成员变量的声明时初始化和构造方法中初始化的区别
- Java如何改变图片中图像的内容
- 怎么可以在控制台只输入int 型 ,而输入其他类型时提示禁止。
- Runtime.getRuntime().addShutdownHook怎么没起作用?
- 反编译一个CLASS文件,不成功!!
- 我一个java问题!
- 初学java 跪求小牛给讲一下 为什么这个显示不出来窗口 我只想显示一个窗口出来
- 请问:怎样获得两个时间间隔的 秒/毫秒 数?
- jbuilder 5 的注册码???非常急!
- 怎样在java中调用windows的api函数
- 求助:javac、java命令编译运行java程序的步骤
- for是否应该避免定义变量?
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.
super.getHibernateTemplate().createQuery(sql);
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)的效果一样