简体   繁体   中英

Hibernate C3P0 pool mechanism

I am using Hibernate with my web application.

And used C3P0 connection pooling mechanism for it.

My configuration is as bellow, To check if it's working properly or not, I put max_size and min_size to 0. I think ideally if it's 0 then it should not give me any connection and should throw error/exception. But it doesn't do any thing like this. It's working as normal it should.

So, how can I be sure that the given C3P0 related configuration is perfect one?

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/reg</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="connection.autocommit">false</property> 

<!-- Connection pooling configuration -->
<property name="c3p0.acquire_increment">1</property> 
<property name="c3p0.idle_test_period">1</property> <!-- seconds --> 
<property name="c3p0.max_size">0</property> 
<property name="c3p0.max_statements">0</property> 
<property name="c3p0.min_size">0</property> 
<property name="c3p0.timeout">200</property> <!-- seconds --> 


<property name="hibernate.show_sql">false</property>

It depends on the app you are building.

Database connection pools are used because of following reasons:

  • Acquiring DB connection is costly operation.
  • You have limited resources and hence at a time can have only finite number of DB connections open.
  • Not all the user requests being processed by your server are doing DB operations, so you can reuse DB connections between requests.

Since acquiring new connections are costly, you should keep min_size to be non-zero. Based on what is load during light usage of your app, you can use a good guess here. Typically, 5 is specified in most examples.

acquire_increment depends on how the fast the number of users that are using your app increases. So, imagine if you ask 1 new connection every time you needed an extra connection, your app may perform badly. So, anticipating user burst, you may want to increment in larger chunks, say 5 or 10 or more.

Typically, max. number of DB connections you have can be lesser than the number of concurrent users that are using your app. However, if you are application is database-heavy, then, you may have to configure max_size to match the number of concurrent users you have.

There will be a point when you may not be able to deal with so many users even after configuring max_size to be very high. That's when you will have to think about re-designing your app to avoid load on database. This is typically done by offloading read operations to alternate DB instance that serves only read operations. Also, one employs caching of such data which do not change often but are read very often.

Similar justification can be applied for other fields as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM