首页 > 编程知识 正文

c3p0连接池死锁,oracle数据库连接不释放

时间:2023-05-05 16:04:08 阅读:49532 作者:4611

1、Spring+Hibernate+c3p0连接池配置:

? XML版本=' 1.0 '编码=' utf-8 '? beans xmlns=' http://www.spring框架. org/schema/beans ' xmlns 3360 xsi=' http://www.w3.org/2001/XML方案-实例' xmlns : p=' http://www.spring framework.org/schema/p ' xmlns 3360 context=' http://www.spring context ' xmlns : tx=' http://www.spring框架. org/schema/tx ' xmlns 3360 AOP=' http://www.spring framework.org AOP ' xsi :方案位置=' http://www.spring framework.org/schechema 方案spring-beans-3.2.xsd http://www.spring framework.org /方案ma/context 3358 www.spring framework.org /方案ma/content spring-context-3.2.xsd http://www.spring framework.org/schema/tx http://www.spring framework.org/schema/tttema spring-tx-3.2.xsd http://www.spring帧work.org/schema/AOP http://www.spring帧work.org/schema/AOP/spring-AOP-3.2.xsd ' default-auto wire=' by name ' context 3360 property-placeholder location=' class path 3360 jdbcopopopt 2.C3 P0.combopooleddatasource ' destroy-method=' close ' propertynamme e e } '/property name=' JDBC URL ' value=' $ { property name=' user ' value=' $ { JDBC.username }/'/property name=' minpoolsize ' value='5'/property name=' property name=' max statement property name=' idleconnectiontestperiod ' value=' 3000 '/property name=' log in time out ' vavavalue bean bean id=' session factory ' class=' org.spring framework.ORM.hibernate3. annotation.annotationsessionfactorybean property property name=' hibernate properties ' propspropkey=' hibernate.show _ SQL ' true/proppropkey=' hibernate.DDD proppropkey=' hibernate.format _ SQL ' proppropkey=' hibernate.use _ SQL _ comments ' true/prop/props/propertrt

ackage="com" /> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <!-- 支持事务 @Transactional 标记 --> <tx:annotation-driven transaction-manager="transactionManager" /></beans>

2、遇到的问题:

在项目中使用this.getHibernateTemplate().getSessionFactory().openSession();获得Session进行操作数据库,但是出现当查询多次之后,连接池就满了,不会继续查询数据库,导致整个程序被挂起的情况。

具体代码如下:

public class ArticleDaoImpl implements ArticleDao {private HibernateTemplate hibernateTemplate;public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public List<article> findAllArticle(int uid) {String sql="select * from article where auid="+uid+" and (asid=1 or asid=3 or asid=5)";/* 使用这种方式得到的线程不会自动关闭,当开启次数过多时,会抛出异常,导致整个程序被挂起*/ Session session=hibernateTemplate.getSessionFactory().openSession();SQLQuery query=session.createSQLQuery(sql).addEntity(article.class);List<article> articleList=query.list();return articleList;}}

3、报错时候的日志信息:

2014-06-06 15:16:50,654 [btpool0-4] DEBUG AbstractBeanFactory : Returning cached instance of singleton bean 'transactionManager'2014-06-06 15:16:50,655 [btpool0-4] DEBUG AbstractPlatformTransactionManager : Creating new transaction with name [com.*.*.service.ActivityService.findAllByPage]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''2014-06-06 15:16:50,655 [btpool0-4] DEBUG HibernateTransactionManager : Opened new Session [org.hibernate.impl.SessionImpl@4bdc0bc7] for Hibernate transaction2014-06-06 15:16:50,655 [btpool0-4] DEBUG HibernateTransactionManager : Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@4bdc0bc7]2014-06-06 15:16:50,655 [btpool0-4] DEBUG BasicResourcePool : trace com.mchange.v2.resourcepool.BasicResourcePool@7bfbfeae [managed: 20, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@564389b7)2014-06-06 15:16:50,656 [btpool0-4] DEBUG HibernateTransactionManager : Exposing Hibernate transaction as JDBC transaction [com.mchange.v2.c3p0.impl.NewProxyConnection@6a02938d]2014-06-06 15:16:50,657 [btpool0-4] DEBUG BasicResourcePool : acquire test -- pool is already maxed out. [managed: 20; max: 20]2014-06-06 15:16:50,657 [btpool0-4] DEBUG BasicResourcePool : awaitAvailable(): com.mchange.v2.c3p0.impl.NewPooledConnection@564389b72014-06-06 15:16:50,658 [btpool0-4] DEBUG BasicResourcePool : trace com.mchange.v2.resourcepool.BasicResourcePool@7bfbfeae [managed: 20, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@564389b7)

原来以this.getHibernateTemplate().getSessionFactory().openSession();这种方式打开的session不会自动关闭,所以查询多次之后连接池就满了,不会继续查询。

4、解决方法:

将获得session的方式从this.getHibernateTemplate().getSessionFactory().openSession();改为this.getHibernateTemplate().getSessionFactory().getCurrentSession();

具体代码如下:

public class ArticleDaoImpl implements ArticleDao {private HibernateTemplate hibernateTemplate;public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public List<article> findAllArticle(int uid) {String sql="select * from article where auid="+uid+" and (asid=1 or asid=3 or asid=5)";Session session=hibernateTemplate.getSessionFactory().getCurrentSession();/* Session session=hibernateTemplate.getSessionFactory().openSession(); * 使用这种方式得到的线程不会自动关闭,当开启次数过多时,会抛出异常,导致整个程序被挂起*/SQLQuery query=session.createSQLQuery(sql).addEntity(article.class);List<article> articleList=query.list();return articleList;}}

5、获取session的几种方法的分析:

(1)this.getsession实际上是调用了父类中的方法获得session。使用spring管理hibernate的SessionFactory的时候,这个方法会从session池中拿出一个session。这样做有可能有问题,就是超session池连接数的时候,spring无法自动的关闭session。 不推荐使用。

(2)this.getHibernateTemplate().getSessionFactory().OpenSession。这种方法从spring管理的sessionFactory中创建一个session,此session不是线程绑定的。这种方法不用手动管理事务,但是同一个线程多次的开启和关闭session,浪费系统资源和影响执行效率,正常情况下还是不要用了。(需要手动关闭连接)

(3)this.getHibernateTemplate().getSessionFactory().getCurrentSession()。从spring管理的sessionFactory中创建一个绑定线程的session.,spring会根据该线程的执行情况来自动判断是关闭session还是延迟关闭。这样做可以避免手动的管理事务,同时一个线程最多开启和关闭一次session又可以提高程序的性能。 极力推荐使用这种方法。(可以完美解决本博客的问题)

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。