Java_web:hibernate+mysql超时

mysql如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接。

解决办法:

1,connection url中加参数: autoReconnect=true

jdbc.url=jdbc:mysql://ipaddress:3306/database?autoReconnect=true&autoReconnectForPools=true


2,用hibernate的话, 加如下属性:

<?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>
        <!-- Database connection settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">aaa</property>
        <property name="hibernate.connection.password">aaa</property>
		<!-- c3p0 -->
		 <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<property name="hibernate.c3p0.min_size">5</property>
		<property name="hibernate.c3p0.max_size">20</property>
		<property name="hibernate.c3p0.timeout">300</property>
		<property name="hibernate.c3p0.max_statements">50</property>
		<property name="hibernate.c3p0.idle_test_period">3000</property>
		<!-- others -->
		<property name="hibernate.show_sql">true</property>
 		<property name="hibernate.format_sql">true</property>
        <mapping resource="com/xinju/hibernate/Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


3,写个shell脚本,定时重启tomcat。terrible.

你可能感兴趣的:(Java_web:hibernate+mysql超时)