优雅地处理Hibernate / Spring Transactions中陈旧的数据库连接

时间:2012-05-03 16:46:34

标签: spring hibernate

我有一个在测试中运行良好的系统,但现在我已将它们移动到生产服务器并准备好抛出开关,我遇到了问题。

如果应用程序闲置大约15分钟,则spring事务服务中的数据库连接会丢失。在这之后来到应用程序的第一个人受到了这个

的欢迎
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,它会重新连接并运行正常。看起来Spring正在按照第一个请求行事,走向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 个答案:

答案 0 :(得分:1)

Hibernate documentation说:

  然而,

Hibernate自己的连接池算法非常简单   简陋。它旨在帮助您入门,而不是   打算用于生产系统,甚至用于性能   测试。您应该使用第三方池以获得最佳性能   稳定性。

因此,答案很简单:使用真实的连接池,并将其配置为在将连接提供给应用程序之前测试连接。