SSH 占用数据库连接不释放问题

SSH框架的项目在訪问数据库的时候。訪问完毕后一直占用链接,不释放。导致过了一段时间后,server没挂,就是有訪问数据库的连接是时候。一直卡住

解决的方法:

1.配置spring相应的hibernate文件:

           <prop key="hibernate.connection.release_mode">after_statement</prop> 事务提交后自己主动释放连接

2配置事务

    

<!--spring 声明式事务管理器 -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <!--Spring事务拦截器 -->
 <bean id="transactionInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager" />
  <property name="transactionAttributes">
   <props>
    <!-- 以browse、list、load、get及is开头的全部方法採用仅仅读型事务控制类型 -->
    <prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
    <!-- 全部方法均进行事务控制,假设当前没有事务。则新建一个事务 -->
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

 <!-- 自己主动代理类 -->
 <bean
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <list>
    <value>*Impl</value>
   </list>
  </property>

  <!-- 这个属性为true时。表示被代理的是目标类本身而不是目标类的接口 -->
  <property name="proxyTargetClass">
   <value>true</value>
  </property>

  <!-- 依赖注入上面定义的事务拦截器transactionInterceptor -->
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
 </bean>

猜你喜欢

转载自www.cnblogs.com/ldxsuanfa/p/10664241.html
今日推荐