2012-05-03 33 views
2

我有一個在測試中工作正常的系統,但現在我已經將它們移動到生產服務器並且準備好引發交換機,即時通訊有問題。在Hibernate/Spring Transactions中優雅地處理陳舊的數據庫連接

如果應用程序閒置大約15分鐘,則春季事務服務中的數據庫連接將下降。這涉及到這種情況發生後,該應用程序的第一人與此

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Cannot open connection 
    org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596) 
    org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) 
    org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335) 
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105) 
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621) 
    parity.persistence.DataAccess$$EnhancerByCGLIB$$921ef13.find(<generated>) 
    parity.model.Configuration.getConfiguration(Configuration.java:84) 
    parity.model.Configuration.getSetting(Configuration.java:46) 
    parity.model.Configuration$$FastClassByCGLIB$$8355c3d0.invoke(<generated>) 
    net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
    org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:617) 
    parity.model.Configuration$$EnhancerByCGLIB$$5e96e8b9.getSetting(<generated>) 
    parity.model.OnlineStatus.getSiteStatus(OnlineStatus.java:50) 
    parity.action.site.SiteStatusInterceptor.intercept(SiteStatusInterceptor.java:16) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498) 
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434) 
root cause 

打招呼,如果你按F5鍵您的瀏覽器,它重新連接並運行良好。看起來春天正在按照第一個請求的要求進行一些工作,比如eek,我死了,在死亡的過程中,重新連接到數據庫。但我不知道。

我一直在尋找解決方法,但它看起來像除非我使用c3p0或weblogic每個人都是無能爲力。有沒有辦法來解決這個問題?這裏是我的配置文件

的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 

    <session-factory> 
     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
     <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
     <property name="connection.autocommit">false</property> 
     <property name="show_sql">false</property> 
     <property name="use_sql_comments">false</property> 

    </session-factory> 
</hibernate-configuration> 

交易service.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <bean id="boardingSessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="configLocations"> 
      <list> 
       <value>classpath:hibernate/boarding-hibernate.cfg.xml</value> 
       <value>classpath:boarding-hibernate.cfg.xml</value> 
      </list> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
    </bean> 

    <bean id="boardingTransactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="boardingSessionFactory" /> 
     <qualifier value="boarding" /> 
    </bean> 

    <tx:advice id="boardingTxAdvice" transaction-manager="boardingTransactionManager"> 
     <tx:attributes> 
      <tx:method name="get*" read-only="true" /> 
      <tx:method name="*" /> 
     </tx:attributes> 
    </tx:advice> 
</beans> 

筆記,我的Hibernate使用2個文件,一個是處理全球API中設置,還有一個應用程序本身具有特定於應用程序的設置。爲了達到這個目的,我認爲全球性的就是最重要的。

回答

1

Hibernate documentation說:

Hibernate自己的連接池算法,然而,相當 簡陋。它旨在幫助您開始使用,而不是用於生產系統的 ,或者甚至用於測試的性能 。您應該使用第三方池以獲得最佳性能和穩定性。

所以,答案很簡單:使用一個真正的連接池,並將其配置爲在將它們提供給應用程序之前測試連接。

+0

我的印象是春季交易服務處理連接池,你告訴我,它實際上處理池的休眠嗎? – scphantm

+0

閱讀Spring文檔的這一部分:http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#jdbc-datasource,以及這個。如果你沒有在Spring中使用連接池配置數據源,那麼LocalSessionFactoryBean將使用你在Hibernate配置文件中配置的內容,如LocalSessionFactoryBean javadoc中所述:http://static.springsource.org/spring/docs /3.1.x/javadoc-api/org/springframework/orm/hibernate3/LocalSessionFactoryBean.html –

+0

呃,我真的希望我們在測試中發現它,而不是在我們的代碼鎖定之後。哦,至少實施c3p0並不是什麼大事。 – scphantm