您的位置:首页 > 其它

使用proxool连接池 ,连接没有关闭的原因

2014-02-20 16:51 387 查看
遇到的问题: 

    使用如下方法查询,连接不能正常关闭: 

        方式1:   SQLQuery query=getSession().createSQLQuery(sql.toString());

           如果事务配置正确的话,方式1可以.

        方式2:  Session session = this.getHibernateTemplate().getSessionFactory().openSession();
             List<Object[]> cList= session.createSQLQuery(sql).list();

           该方式不受spring事务管理,需要修改为如下方式:

                Session session = this.getSession();
Query query = session.createSQLQuery(sql);
List<Object[]> cList= query.list();

          这样修改后,如果事务配置正确,则session能正常关闭.

      如果事务配置不正确, 为了以防万一,可以在代码中添加如下代码,关闭session:    releaseSession(session);//  这是spring自己提供的方法

    正确的事务配置如下:

   <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 哪些类哪些方法使用事务 -->
<aop:config>
<aop:pointcut id="allServiceMethod"
expression="execution(* com.bbc.*.*.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />
</aop:config>
<!-- 事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

 当时由于事务配置中的包路径写错了,导致session关闭没有通过spring事务控制,造成session一直未关闭. 结果强制使用 releaseSession(session); 也到实现了session关闭.其实当事务配置正确后,就不需要 releaseSession(session);  了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐