Java Connection Pooling

arjunarajan
arjunarajan used Ask the Experts™
on
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.

Thanks,
Rameshbabu
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
    <session-factory>
 
        <!-- 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 -->
                [mapping_files]
 
    </session-factory>
 
</hibernate-configuration>

Open in new window

Author

Commented:
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.

Thanks
Commented:
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.

Commented:

jnid
 
should be
 
jndi

Open in new window

Author

Commented:
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

Thanks
Commented:
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:

http://www.javapractices.com/topic/TopicAction.do?Id=127

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.

Author

Commented:
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.

Thanks.

Commented:
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.

Author

Commented:
Im using JBoss as my application server.

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

http://www.coderanch.com/t/89511/JBoss/Connection-Pooling

http://onjava.com/pub/a/onjava/2004/02/25/jbossjdbc.html

Author

Commented:
Thank you. That link was useful.

Whether hibernate handles database transactions itself?

Commented:
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