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>