SSH 佔用資料庫連接不釋放問題

來源:互聯網
上載者:User

SSH架構的項目在訪問資料庫的時候,訪問完成後一直佔用連結,不釋放,導致過了一段時間後,伺服器沒掛,就是有訪問資料庫的串連是時候,一直卡住

解決辦法:

 

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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.