gpt4 book ai didi

java - JDBC 连接 : Not Recconecting after Timeout

转载 作者:行者123 更新时间:2023-11-28 22:02:40 25 4
gpt4 key购买 nike

在我的应用程序中没有使用连接一段时间后,下一次尝试检索有效连接会引发 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com