数据库连接超时

发布于 2024-09-05 18:36:39 字数 3593 浏览 5 评论 0原文

我在网上读了很多关于这个问题的文章,但似乎没有一个明确的解决方案。请有人给我一个关于为什么我会遇到数据库超时的明确答案。该应用程序是托管在 Tomcat 5.5 服务器上的 GWT 应用程序。我使用 spring 并在 applicationContext.xml 中创建会话工厂,如下所示

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${connection.dialect}</prop>
            <prop key="hibernate.connection.username">${connection.username}</prop>
            <prop key="hibernate.connection.password">${connection.password}</prop>
            <prop key="hibernate.connection.url">${connection.url}</prop>
            <prop key="hibernate.connection.driver_class">${connection.driver.class}</prop>
            <prop key="hibernate.show_sql">${show.sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">20</prop>
            <prop key="hibernate.c3p0.timeout">1800</prop>
            <prop key="hibernate.c3p0.max_statements">50</prop>
            <prop key="hibernate.c3p0.idle_test_period">300</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>za.co.xxxx.traintrack.server.model.Answer</value>
            <value>za.co.xxxx.traintrack.server.model.Company</value>
            <value>za.co.xxxx.traintrack.server.model.CompanyRegion</value>
            <value>za.co.xxxx.traintrack.server.model.Merchant</value>
            <value>za.co.xxxx.traintrack.server.model.Module</value>
            <value>za.co.xxxx.traintrack.server.model.Question</value>
            <value>za.co.xxxx.traintrack.server.model.User</value>
            <value>za.co.xxxx.traintrack.server.model.CompletedModule</value>
        </list>
    </property>
</bean>

<bean id="dao" class="za.co.xxxx.traintrack.server.DAO">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="adminUsername" value="${admin.user.name}"/>
    <property name="adminPassword" value="${admin.user.password}"/>
    <property name="godUsername" value="${god.user.name}"/>
    <property name="godPassword" value="${god.user.password}"/>
</bean>

一切正常,直到第二天:

INFO   | jvm 1    | 2010/06/15 14:42:27 | 2010-06-15 18:42:27,804 WARN 
[JDBCExceptionReporter] : SQL Error: 0, SQLState: 08S01
INFO   | jvm 1    | 2010/06/15 14:42:27 | 2010-06-15 18:42:27,821 ERROR 
[JDBCExceptionReporter] : The last packet successfully received from the server 
was 38729 seconds ago.The last packet sent successfully to the server was 38729
seconds ago, which  is longer than the server configured value of 'wait_timeout'.
You should consider either expiring and/or testing connection validity before 
use in your application, increasing the server configured values for client
timeouts, or using the Connector/J connection property 'autoReconnect=true' to
avoid this problem.
INFO   | jvm 1    | 2010/06/15 14:42:27 | Jun 15, 2010 6:42:27 PM 
org.apache.catalina.core.ApplicationContext log
INFO   | jvm 1    | 2010/06/15 14:42:27 | SEVERE: Exception while dispatching
incoming RPC call

我读了很多不同的东西(没有一个起作用),请帮助

I have read so many articles on the Internet about this problem but none seem to have a clear solution. Please could someone give me a definite answer as to why I am getting database timeouts. The app is a GWT app that is being hosted on a Tomcat 5.5 server. I use spring and the session factory is created in the applicationContext.xml as follows

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${connection.dialect}</prop>
            <prop key="hibernate.connection.username">${connection.username}</prop>
            <prop key="hibernate.connection.password">${connection.password}</prop>
            <prop key="hibernate.connection.url">${connection.url}</prop>
            <prop key="hibernate.connection.driver_class">${connection.driver.class}</prop>
            <prop key="hibernate.show_sql">${show.sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">20</prop>
            <prop key="hibernate.c3p0.timeout">1800</prop>
            <prop key="hibernate.c3p0.max_statements">50</prop>
            <prop key="hibernate.c3p0.idle_test_period">300</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>za.co.xxxx.traintrack.server.model.Answer</value>
            <value>za.co.xxxx.traintrack.server.model.Company</value>
            <value>za.co.xxxx.traintrack.server.model.CompanyRegion</value>
            <value>za.co.xxxx.traintrack.server.model.Merchant</value>
            <value>za.co.xxxx.traintrack.server.model.Module</value>
            <value>za.co.xxxx.traintrack.server.model.Question</value>
            <value>za.co.xxxx.traintrack.server.model.User</value>
            <value>za.co.xxxx.traintrack.server.model.CompletedModule</value>
        </list>
    </property>
</bean>

<bean id="dao" class="za.co.xxxx.traintrack.server.DAO">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="adminUsername" value="${admin.user.name}"/>
    <property name="adminPassword" value="${admin.user.password}"/>
    <property name="godUsername" value="${god.user.name}"/>
    <property name="godPassword" value="${god.user.password}"/>
</bean>

All works fine untile the next day:

INFO   | jvm 1    | 2010/06/15 14:42:27 | 2010-06-15 18:42:27,804 WARN 
[JDBCExceptionReporter] : SQL Error: 0, SQLState: 08S01
INFO   | jvm 1    | 2010/06/15 14:42:27 | 2010-06-15 18:42:27,821 ERROR 
[JDBCExceptionReporter] : The last packet successfully received from the server 
was 38729 seconds ago.The last packet sent successfully to the server was 38729
seconds ago, which  is longer than the server configured value of 'wait_timeout'.
You should consider either expiring and/or testing connection validity before 
use in your application, increasing the server configured values for client
timeouts, or using the Connector/J connection property 'autoReconnect=true' to
avoid this problem.
INFO   | jvm 1    | 2010/06/15 14:42:27 | Jun 15, 2010 6:42:27 PM 
org.apache.catalina.core.ApplicationContext log
INFO   | jvm 1    | 2010/06/15 14:42:27 | SEVERE: Exception while dispatching
incoming RPC call

I have read so many different things (none of which worked), please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-09-12 18:36:39

看看这个链接 http://www.databasesandlife.com/automatic -reconnect-from-hibernate-to-mysql/

 In “hibernate.cfg.xml”:

<!-- hibernate.cfg.xml -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<property name="connection.provider_class">
   org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- no "connection.pool_size" entry! -->

然后创建一个文件“c3p0.properties”,该文件必须位于类路径的根目录中(即无法为应用程序的特定部分覆盖它):

# c3p0.properties
c3p0.testConnectionOnCheckout=true

Look at this link http://www.databasesandlife.com/automatic-reconnect-from-hibernate-to-mysql/

 In “hibernate.cfg.xml”:

<!-- hibernate.cfg.xml -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<property name="connection.provider_class">
   org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- no "connection.pool_size" entry! -->

Then create a file “c3p0.properties” which must be in the root of the classpath (i.e. no way to override it for particular parts of the application):

# c3p0.properties
c3p0.testConnectionOnCheckout=true
甜警司 2024-09-12 18:36:39

Romain,我没有 hibernate.cfg.xml (全部在 applicationContext.XML 中完成)

c3p0.properties

c3p0.testConnectionOnCheckout=true

applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           ">

    <context:annotation-config/>

    <context:component-scan base-package="za.co.cmsoftware.traintrack.server"/>

    <!-- Application properties -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${user.dir}/@propertiesFile@</value>
            </list>
        </property>
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${connection.dialect}</prop>
                <prop key="hibernate.connection.username">${connection.username}</prop>
                <prop key="hibernate.connection.password">${connection.password}</prop>
                <prop key="hibernate.connection.url">${connection.url}</prop>
                <prop key="hibernate.connection.driver_class">${connection.driver.class}</prop>
                <prop key="hibernate.show_sql">${show.sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">300</prop>
                <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>za.co.xxxx.traintrack.server.model.Answer</value>
                <value>za.co.xxxx.traintrack.server.model.Company</value>
                <value>za.co.xxxx.traintrack.server.model.CompanyRegion</value>
                <value>za.co.xxxx.traintrack.server.model.Merchant</value>
                <value>za.co.xxxx.traintrack.server.model.Module</value>
                <value>za.co.xxxx.traintrack.server.model.Question</value>
                <value>za.co.xxxx.traintrack.server.model.User</value>
                <value>za.co.xxxx.traintrack.server.model.CompletedModule</value>
            </list>
        </property>
    </bean>

    <bean id="dao" class="za.co.xxxx.traintrack.server.DAO">
        <property name="sessionFactory" ref="sessionFactory"/>
        <property name="adminUsername" value="${admin.user.name}"/>
        <property name="adminPassword" value="${admin.user.password}"/>
        <property name="godUsername" value="${god.user.name}"/>
        <property name="godPassword" value="${god.user.password}"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

Romain, I do not have a hibernate.cfg.xml (all done in applicationContext.XML)

c3p0.properties

c3p0.testConnectionOnCheckout=true

applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           ">

    <context:annotation-config/>

    <context:component-scan base-package="za.co.cmsoftware.traintrack.server"/>

    <!-- Application properties -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${user.dir}/@propertiesFile@</value>
            </list>
        </property>
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${connection.dialect}</prop>
                <prop key="hibernate.connection.username">${connection.username}</prop>
                <prop key="hibernate.connection.password">${connection.password}</prop>
                <prop key="hibernate.connection.url">${connection.url}</prop>
                <prop key="hibernate.connection.driver_class">${connection.driver.class}</prop>
                <prop key="hibernate.show_sql">${show.sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">300</prop>
                <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>za.co.xxxx.traintrack.server.model.Answer</value>
                <value>za.co.xxxx.traintrack.server.model.Company</value>
                <value>za.co.xxxx.traintrack.server.model.CompanyRegion</value>
                <value>za.co.xxxx.traintrack.server.model.Merchant</value>
                <value>za.co.xxxx.traintrack.server.model.Module</value>
                <value>za.co.xxxx.traintrack.server.model.Question</value>
                <value>za.co.xxxx.traintrack.server.model.User</value>
                <value>za.co.xxxx.traintrack.server.model.CompletedModule</value>
            </list>
        </property>
    </bean>

    <bean id="dao" class="za.co.xxxx.traintrack.server.DAO">
        <property name="sessionFactory" ref="sessionFactory"/>
        <property name="adminUsername" value="${admin.user.name}"/>
        <property name="adminPassword" value="${admin.user.password}"/>
        <property name="godUsername" value="${god.user.name}"/>
        <property name="godPassword" value="${god.user.password}"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文