Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best configuration of c3p0 [closed]

I'm struggling with a problem facing c3p0 configuration. I posted a question last week

like image 339
Babak Behzadi Avatar asked Sep 20 '12 06:09

Babak Behzadi


People also ask

Which connection pool is best for hibernate?

C3P0 is an open source JDBC connection pool distributed along with Hibernate in the lib directory. Hibernate will use its org. hibernate.

How does c3p0 connection pool work?

c3p0 is a Java library that provides a convenient way for managing database connections. In short, it achieves this by creating a pool of connections. It also effectively handles the cleanup of Statements and ResultSets after use.

What is hibernate c3p0 timeout?

hibernate. c3p0. timeout – When an idle connection is removed from the pool (in second). Hibernate default: 0, never expire.

What is the use of c3p0 in hibernate?

c3p0. timeout You specify the timeout period (in this case, 300 seconds) after which an idle connection is removed from the pool).


1 Answers

This is a configuration I am using which keeps resources to a minimum. Of course you'll want to tailor your application to use the resources it needs...

Reference: http://www.mchange.com/projects/c3p0/index.html

  • testConnectionOnCheckin validates the connection when it is returned to the pool. testConnectionOnCheckOut, although would ensure active connections before use, would be too expensive to do.
  • idleConnectionTestPeriod sets a limit to how long a connection will stay idle before testing it. Without preferredTestQuery, the default is DatabaseMetaData.getTables() - which is database agnostic, and although a relatively expensive call, is probably fine for a relatively small database. If you're paranoid about performance use a query specific to your database (i.e. preferredTestQuery="SELECT 1")
  • maxIdleTimeExcessConnections will bring back the connectionCount back down to minPoolSize after a spike in activity.

Below configuration sets poolsize between 3-20. Idle connections are retested every 5 minutes to keep them active. Because of idleConnectionTestPeriod, this will only keep the the minumum number of connections alive. If there are more than 3 connections at the 4-minute mark, it kills those connections freeing resources back to the minimum.

Use of maxIdleTimeExcessConnections and idleConnectionTestPeriod negates the need for maxIdleTime

<Context docBase="myapp" path="/myapp" reloadable="true">     <Resource description="My DB Datasource" name="jdbc/mydb"         auth="Container" factory="org.apache.naming.factory.BeanFactory"         type="com.mchange.v2.c3p0.ComboPooledDataSource"          user="myuser" password="******"         minPoolSize="3"         maxPoolSize="20"         acquireIncrement="1"          driverClass="com.mysql.jdbc.Driver"          jdbcUrl="jdbc:mysql://localhost:3306/mydb"         testConnectionOnCheckin="true"          idleConnectionTestPeriod="300"         maxIdleTimeExcessConnections="240"     /> </Context> 
like image 113
Domenic D. Avatar answered Sep 20 '22 16:09

Domenic D.