Friday, February 27, 2009

Database Connection Pooling in Hibernate

I was looking for a way to control connection pooling in Hibernate inside a Spring environment. God bless Apache foundation for their amazing code helps, as I found the answers in DBCP. Which-ever hibernate session factory you use, use the following data source bean in your configuration xml to control connection pooling:

<bean id="internalDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://127.0.0.1/myDatabase"/>
<property name="username" value="root"/>
<property name="password" value="abc123"/>
<property name="maxIdle" value="5"/>
<property name="maxActive" value="25"/>
<property name="minIdle" value="5"/>
<property name="initialSize" value="5"/>
</bean>


For all options and details, refer the API for BasicDataSource. And, if you are a maven user, don't forget to include the dependency:

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>



 

No comments: