休眠 属性 以保持连接

Hibernate Property To Keep Connections Alive

有任何 Hibernate 属性 o 配置始终保持一定数量的连接 MySql?

提前致谢。

我想你问的是连接池。您可以配置它,例如使用 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

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>

没有更多 "communication link error" ,之后 "expected to read 5 bytes,read 0" 。 这是 link:https://howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/