java - JDBC 连接 : Not Recconecting after Timeout

标签 java spring hibernate tomcat jdbc

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

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.

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

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

服务器.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>

最佳答案

答案留在错误中;

autoReconnect=true

在您的连接 url 末尾添加此参数。

关于java - JDBC 连接 : Not Recconecting after Timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25083537/

相关文章:

java - 如何使用 POI Excel 以编程方式拉伸(stretch)列以适合文本?

java - Spring Integration 和 Transaction Management——需要多难?

mysql - hibernate生成错误的sql "select max(id) from my_table"

java - 不标记事务划分,只是通过session加载一个实体

java - 删除多对多条目,同时将两个对象都保留在数据库中

java - 由于找不到类错误,无法使用 bat 文件运行 Java 文件

java - Spring HibernateDaoSupport 创建多个 session

java - 使用 Ribbon 自动重试(无 zuul): not working + wrong documentation?

Java Hibernate 设置最小和最大列长度

java - Tomcat 9 无法加载资源 : the server responded with a status of 404 ()