如何配置Hibernate C3P0池

时间:2014-08-14 18:09:30

标签: mysql hibernate connection-pooling c3p0

我遇到了hibernate连接池的问题,导致mysql会话断开连接 所以我做了一些谷歌搜索,我使用C3P0技术,因为我发现解决了这个问题,但错误仍然存​​在:8小时后连接丢失:
mysql timeout issue

这是我的hibernate配置文件:

    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.max_statement">100</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.idle_test_period">300</property>
    <property name="hibernate.c3p0.acquire_increment">2</property>
    <property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>
    <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    <property name="hibernate.c3p0.validate">true</property>
    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>


    <property name="hibernate.connection.url">jdbc:mysql://localhost/cabinet?characterEncoding=UTF-8</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.show_sql">true</property>

这是我的POM文件:

 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cabinet</groupId>
  <artifactId>cabinet</artifactId>
  <version>0.0.1</version>
  <description></description>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.9</version>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.12.1.GA</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-annotations</artifactId>
      <version>3.5.0-Final</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.3.16.3</version>
    </dependency>
    <dependency>
      <groupId>opensymphony</groupId>
      <artifactId>sitemesh</artifactId>
      <version>2.4.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-sitemesh-plugin</artifactId>
      <version>2.3.3</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-json-plugin</artifactId>
      <version>2.1.8</version>
    </dependency>
    <dependency>
      <groupId>com.jgeppert.struts2.jquery</groupId>
      <artifactId>struts2-jquery-plugin</artifactId>
      <version>3.7.1</version>
    </dependency>
    <dependency>
      <groupId>com.jgeppert.struts2.bootstrap</groupId>
      <artifactId>struts2-bootstrap-plugin</artifactId>
      <version>2.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-c3p0</artifactId>
      <version>4.3.4.Final</version>
    </dependency>  

是否有任何要更正的配置属性或要添加的依赖项?            

1 个答案:

答案 0 :(得分:1)

看起来您的c3p0配置被忽略了。我认为你有两个不同的hibernate-core版本,一个来自hibernate-annotations 3.5.0-Final,另一个来自hibernate-c3p0.4.3.4-Final。删除hibernate-annotations 3.5.0-Final依赖项,因为注释已经包含在hibernate-core中,因为v3.5你的hibernate-c3p0 4.3.4.Final依赖项将添加hibernate-core 4.3.4.Final。