Hibernate Property To Keep Connections Alive

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

Thank you in advance.

Upvotes: 1

Views: 4400

Answers (2)

luisacevedo
luisacevedo

Reputation: 242

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/

Upvotes: 0

StanislavL
StanislavL

Reputation: 57391

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

Upvotes: 1

Related Questions