JDBC连接:超时后不连接

时间:2014-08-01 15:23:36

标签: java spring hibernate tomcat jdbc

在我的应用程序中不使用连接一段时间后,下一次检索有效连接的尝试会引发JDBC异常:

  

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功收到的最后一个数据包是81,155,040毫秒前。成功发送到服务器的最后一个数据包是81,155,040毫秒之前。比服务器配置的值' wait_timeout'更长。您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值,或者使用Connector / J连接属性" autoReconnect = true'避免这个问题。

我想知道应该更改哪个配置以避免此问题。

我通过Hibernate SessionFactory管理应用程序级别的连接,其方式是池级和连接管理在此级别完全不可见。在处理SessionFactory时,我是否应该尝试检查和检索全新的连接?及时:我也使用Spring。

的Server.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">

  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
  <Listener className="org.apache.catalina.core.JasperListener"/>
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

  <GlobalNamingResources>

        <Resource auth="Container"
                  description="User database that can be updated and saved" 
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
                  name="UserDatabase" 
                  pathname="conf/tomcat-users.xml" 
                  type="org.apache.catalina.UserDatabase"
        />

        <Resource auth="Container" 
                  driverClassName="com.mysql.jdbc.Driver" 
                  initialSize="20"                              
                  global="mysql/GestaoProjetos" 
                  maxActive="800" 
                  maxIdle="20" 
                  maxWait="30000" 
                  minEvictableIdleTimeMillis="10000" 
                  name="mysql/GestaoProjetos" 
                  password="laranja" 
                  removeAbandoned="true" 
                  removeAbandonedTimeout="300" 
                  testOnBorrow="true" 
                  testWhileIdle="true" 
                  timeBetweenEvictionRunsMillis="10000" 
                  type="javax.sql.DataSource" 
                  url="jdbc:mysql://localhost/GestaoProjetos?autoReconnect=true" 
                  username="tomcat" 
                  validationQuery="SELECT 1"
                  validationInterval="30000"
        />

  </GlobalNamingResources>

  <Service name="Catalina">

      <Connector connectionTimeout="20000" 
                 port="80" 
                 protocol="HTTP/1.1" 
                 redirectPort="443"
      />

      <Connector port="9" 
                 protocol="AJP/1.3" 
                 redirectPort="443"
      />

      <Engine   defaultHost="localhost" 
                name="Catalina">

                <Realm  className="org.apache.catalina.realm.UserDatabaseRealm" 
                        resourceName="UserDatabase"
                />

                <Host appBase="webapps" 
                      autoDeploy="true" 
                      name="localhost" 
                      unpackWARs="true" 
                      xmlNamespaceAware="false" 
                      xmlValidation="false">

                        <Context docBase="GestaoProjetos" 
                                 path="/GestaoProjetos" 
                                 reloadable="true" 
                                 source="org.eclipse.jst.jee.server:GestaoProjetos"
                                 crossContext="true"
                        />

                </Host>
      </Engine>
  </Service>
</Server>

hibernate.cfg.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="SessionFactoryUtil">
        <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="hibernate.connection.username">USERNAME</property>
        <property name="hibernate.connection.password">PASSWORD</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/GestaoProjetos?autoReconnect=true</property>
        <property name="hibernate.connection.pool_size">200</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.connection.release_mode">after_transaction</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.generate_statistics">true</property>
        <!-- Automatic schema creation (begin) === -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- property name="mappingJarLocations">file:/WEB-INF/lib/</property> --> 
    </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

答案是留在错误中;

  

autoReconnect的= TRUE

将此参数添加到连接网址的末尾。