简体   繁体   中英

JDBC Connection: Not Recconecting after Timeout

After some time without using a connection in my application, the next attempt to retrieve a valid connection raises an JDBC exception:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 81,155,040 milliseconds ago. The last packet sent successfully to the server was 81,155,040 milliseconds ago. 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.

I'd like to know which configuration should I change in order to avoid this issue.

I'm managing the connections in application level through a Hibernate SessionFactory, in ways that the pooling and connection management is totally invisible at this level. Should I have to do some kind of attempting to check and retrieve a brand new connection while dealing with SessionFactory? In time: I'm using Spring too.

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>

The answer is stays in the error;

autoReconnect=true

add this parameter end of your connection url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM