当我的应用程序保持空闲一段时间后,我无法连接到数据库。我的数据库是Mysql,服务器是Tomcat。应用程序是使用Spring-Hibernate构建的。我的连接设置persistance.xml
如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="GTPU" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/application_db_name"/>
<property name="hibernate.connection.username" value="***"/>
<property name="hibernate.connection.password" value="***"/>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
</properties>
</persistence-unit>
</persistence>
但是,如果经过一段时间后尝试,它将成功连接到数据库。
Mysql变量设置如下
-------------------------------------
Variable_name Value
-------------------------------------
connect_timeout 10
delayed_insert_timeout 300
innodb_lock_wait_timeout 50
innodb_rollback_on_timeout OFF
interactive_timeout 28800
lock_wait_timeout 31536000
log_output FILE
net_read_timeout 30
net_write_timeout 60
slave_net_timeout 3600
wait_timeout 28800
--------------------------------------
可能是什么原因?我该如何解决。
我使用连接池库解决了它-http: C3P0
//www.mchange.com/projects/c3p0/#idleConnectionTestPeriod
使用属性idle_test_period,该属性在指定时间间隔后对数据库服务器执行ping操作。
<property name="hibernate.c3p0.idle_test_period" value="100" />
<property name="hibernate.c3p0.preferredTestQuery" value="select 1;" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句