Hibernate属性保持连接活跃

时间:2015-09-17 03:42:35

标签: mysql hibernate properties connection

有任何Hibernate属性配置可以保持与MySql的确定数量的连接吗?

提前谢谢。

2 个答案:

答案 0 :(得分:1)

我想你问的是连接池。你可以配置它,例如使用c3p0 喜欢这个

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tutorials</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.
connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.min_size">7</property>
        <property name="hibernate.c3p0.max_size">53</property>
        <property name="hibernate.c3p0.timeout">100</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">1000</property>
        <property name="hibernate.c3p0.validate">true</property>
        <property name="hibernate.connection.provider_class">org.hibernate.service.
jdbc.connections.internal.C3P0ConnectionProvider</property>
        <mapping resource="com/javacodegeeks/Student.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

了解更多here

答案 1 :(得分:0)

直到我遇到上面的博客解释说,c3p0从来没有为我工作过 org.hibernate版本必须等于c3p0版本, 所以这就是让我这一天的pom配置

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>4.3.1.Final</version>
 </dependency>
<dependency>
 <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
 <version>4.3.6.Final</version>
</dependency>

  <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
   <version>0.9.1.2</version>
</dependency>
像他解释的那样,仅向hibernate.cfg.xml添加一个属性就足够了  获得c3p0汇集

       <property name="hibernate.c3p0.min_size">10</property>

不再“通信链接错误”,“预计读取5个字节,读取0”之后。 这是链接:https://howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/