mysql 8小时断开连接解决方法

解决Mysql8小时无访问断开连接的方法

配置数据源目前测试通过的是c3p0;

我使用的是GUICE+JPA

以下是配置好的persistence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0" 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">

    <persistence-unit name="Default" >

        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>com.telematics.weixin.domain.SendRecord</class>

        <properties>

            <!-- MySQL Configuration -->

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>

            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/weixin?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true"/>

            <property name="hibernate.connection.username" value="root"/>

            <property name="hibernate.connection.password" value="root"/>

            <property name="hibernate.id.new_generator_mappings" value="true"/>

            <property name="hibernate.hbm2ddl.auto" value="update" />

            <property name="hibernate.show_sql" value="true"/>

            <!-- configuration pool via c3p0-->

            <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>

            <property name="hibernate.c3p0.min_size" value="5" />

            <property name="hibernate.c3p0.max_size" value="30" />

            <property name="hibernate.c3p0.time_out" value="1800" />

            <property name="hibernate.c3p0.max_statement" value="50" />

            <property name="hibernate.c3p0.acquire_increment" value="1" />

            <property name="hibernate.c3p0.idle_test_period" value="120" />

            <property name="hibernate.c3p0.validate" value="true" />

            <!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->

            <property name="hibernate.c3p0.testConnectionOnCheckin" value="true"/>

        </properties>

    </persistence-unit>

</persistence>

 

然后需要加上C3p0的依赖:

             <dependency>

                <groupId>c3p0</groupId>

                <artifactId>c3p0</artifactId>

                <version>0.9.1.2</version>

            </dependency>

            <dependency>

                <groupId>org.hibernate</groupId>

                <artifactId>hibernate-c3p0</artifactId>

                <version>3.6.7.Final</version>

            </dependency>

你可能感兴趣的:(mysql,c3p0)