Java Connection Pooling

arjunarajan used Ask the Experts™
Hi All,

I am creating a web portal with online transaction facility. I am not familiar with database connection pooling. Also, I don't have idea with EJB.

I want to know how I can implement connection pooling without EJB and want a simple code which details me about the connection pooling and its usage in application.

I have heard that we can use JNDI resources for connection pooling but not much idea about that.

Please give me a clear and simple example.

Also, I read that using the default connection pooling mechanism provided by hibernate is not advisable in production and they recommend some third party connection pooling mechanism like c3p0.

I need to know how to use c3p0 connection pooling in hibernate.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here is an example of Hibernate configuration file with c3p0 connection pool.

Try using Spring with Hibernate. It will make your life easier.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <!-- Database connection settings -->
        <property name="connection.driver_class">[driver]</property>
        <property name="connection.url">[url]</property>
        <property name="connection.username">[username]</property>
        <property name="connection.password">[password]</property>
        <!-- SQL dialect -->
        <property name="dialect">[dialect]</property>
        <!-- Use the C3P0 connection pool provider -->
		<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<property name="hibernate.c3p0.min_size">5</property>
		<property name="hibernate.c3p0.max_size">20</property>
		<property name="hibernate.c3p0.timeout">1800</property>
		<property name="hibernate.c3p0.max_statements">50</property>
		<property name="hibernate.c3p0.idle_test_period">1200</property>
		<!-- Show and print nice SQL on stdout -->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<!-- Bind the getCurrentSession() method to the thread. -->
                <property name="current_session_context_class">thread</property>
		<!-- List of XML mapping files -->

Open in new window


Hi Tuno85,

Thank you for your response and that was really an useful one. But, my question has one more query on connection pooling with JNDI without EJB.

Hence, I am not in a position to give full points to you at this moment. If anyone add the comments for the remaining part, I will share the points among both.

You should configure your connection pool on the server and use the JNDI look up in the Hibernate configuration file to get a connection. In the previous example you have your connection pool managed by c3p0 and you don't need the JNDI look up.

Combining Spring and Hibernate can replace your EJB.

<property name="hibernate.connection.datasource">[jnid]</property>

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.


should be

Open in new window


Hi Tuno85,

I hope you misunderstood my question. I am asking the JNDI connection pooling without Hibernate. Not with Hibernate.

Please dont combine both Hibernate and JNDI

Is your connection pool configured at the server. If so the server manages the connection pool to the database and you can request it through the exposed JNDI by doing the look up:

But why doing this programmatic while you can use a framework like Hibernate.
It will make you life easier while you can focus on more important stuff like business logic.


Thank you for your suggestion. The thing is I am not familiar with connection pooling with JNDI. I dont know how to create a connection using datasource.

I just wanted to know the technique of connection pooling with JNDI. Also, I read that using the framework will slowdown the speed of the application. Is it right?

Can you please tell me how and where to configure the datasource file for JNDI connection pooling with a simple example.

Anyhow, I am satisfied with your response and I am awarding full points to you.


Which server do you use to make your web portal?

Your server should manage you connection pool to the database. It exposes the JNDI name for the connection pool.
Your application does the JNDI look up through example Hibernate to get an connection from the pool. Retrieves data from the database and returns the connection to the pool.

Other way is to use the connection management through your application using the c3p0 library.

Frameworks, especially Spring and Hibernate (both proven technologies), provide very little overhead that it can be neglected. In return you get commonly used code so you don't have to re-invent the wheel and focus on your business logic.


Im using JBoss as my application server.

I am not sure how it works with JBoss, but try these links and/or open a new question here at EE:


Thank you. That link was useful.

Whether hibernate handles database transactions itself?

Nope, you should use Spring transaction proxy for this. Is has a nice Hibernate template.

For more information check Spring Framework website.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial