Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JDBC connection pool best practices

I have a basic Spring JDBC application with a pretty basic configuration:

<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>    <property name="url" value="jdbc:oracle:thin:@1.1.1.1:1521:XXX"/>    <property name="username" value="username"/>    <property name="password" value="password"/> </bean>  <bean id="dbThing" class="com.DbThing">    <property name="dataSource" ref="myDataSource"/> </bean> 

I would like to introduce a connection pool, and after reading several threads on SO I am a bit confused about which pooling library to use.

The libraries that seem to have more credits on SO are C3P0 and DBCP. Since I'm using Oracle, I could also use the pooled data source offered by the driver.

I understand that there are more libraries available - for instance the new Apache Tomcat 7 pooling libraries.

Is there any library that I should really avoid?

Is there any recommended configuration I should use with a given library?

Any "war story" you care to share?

like image 367
Luciano Fiandesio Avatar asked Feb 25 '11 12:02

Luciano Fiandesio


People also ask

Which is the best connection pool in spring boot?

Spring Boot uses HikariCP as the default connection pool, due to its remarkable performance and enterprise-ready features.

What is the minimum pool size in JDBC connection?

The connection pool configuration settings are: Initial and Minimum Pool Size: Minimum and initial number of connections maintained in the pool (default is 8) Maximum Pool Size: Maximum number of connections that can be created to satisfy client requests (default is 32)

When should you not use connection pooling?

You reuse a prior database connection, in a new context to avoid the cost of setting up a new database connection for each request. The primary reason to avoid using database connections is that you're application's approach to solving problems isn't structured to accommodate a database connection pool.


2 Answers

C3PO and DBCP development have stalled mostly because they are mature. I have seen both of these drivers be able to support hundreds of transactions per second.

The Tomcat pool is a reworked & updated DBCP driver. MyBatis 3.0 also contains it's own pooling implementation which, based on code inspection, seems solid. Finally, there's BoneCP which claims to have the best performance. I haven't used any of these on a project yet.

Probably the best advice is to pick any of them test it. Spring makes it easy to swap out later.

like image 188
AngerClown Avatar answered Oct 03 '22 11:10

AngerClown


As an alternative to BoneCP, have you perhaps tried Oracle's own database connection pool?

I've had good experiences for the last couple of weeks, so it might be worth giving it a shot - also, I suppose Oracle would know a thing or two about making a connection pool especially when paired up with their own database.

<bean id="dataSource" class="oracle.jdbc.pool.OracleConnectionPoolDataSource">     <property name="URL" value="${jdbc.url}" />     <property name="user" value="${jdbc.username}" />     <property name="password" value="${jdbc.password}" /> </bean> 

UPDATE: Also, if you're using (one of) the latest Oracle JDBC drivers (11.2.0.1+), you may want to try out the new Universal Connection Pool. The OracleConnectionPoolDataSource seems to be officially deprecated in favour of this pool. However, some users have reported errors using it, so it may be too early. I am in a position to use Oracle's latest JDBC drivers, so I will give it a try and update here as soon as have any info on this.

More info on this SO thread: Oracle UCP

like image 38
quantum Avatar answered Oct 03 '22 11:10

quantum