Hibernate Property To Keep Connections Alive

牧云@^-^@ 提交于 2019-12-11 16:32:30

问题


There is any Hibernate property o configuration to keep alive always certain number of connection with MySql?

Thank you in advance.


回答1:


I think you asking about connection pool. You can configure it e.g. using c3p0 Like this

<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>

Read more here




回答2:


c3p0 never worked for me until I came across the blog above that explained that the org.hibernate version must be equal to the c3p0 version, so this is the pom configuration that made my day

<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>

like he explains, it was enough to add only one property to hibernate.cfg.xml to get c3p0 pooling

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

no more "communication link error" , "expected to read 5 bytes,read 0" after that. here is the link:https://howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/



来源:https://stackoverflow.com/questions/32622233/hibernate-property-to-keep-connections-alive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!