Home > Java, Programming > Using C3P0 and Hibernate 3

Using C3P0 and Hibernate 3

February 5th, 2009 Leave a comment Go to comments

C3P0 is a database connection pooling software that works very well with Hibernate. Unfortunately the documentation to get it working correctly is sparse.

What is a connection pool?

Ripped from Wikipedia:

In software engineering, a connection pool is a cache of database connections maintained by the database so that the connections can be reused when the database receives future requests for data. Connection pools are used to enhance the performance of executing commands on a database. Opening and maintaining a database connection for each user, especially requests made to a dynamic database-driven website application, is costly and wastes resources. In connection pooling, after a connection is created, it is placed in the pool and it is used over again so that a new connection does not have to be established. If all the connections are being used, a new connection is made and is added to the pool. Connection pooling also cuts down on the amount of time a user must wait to establish a connection to the database.

I will be explaining how to do this using JPA and the persistence.xml file which uses a slightly different syntax than the hibernate.cfg.xml file.

The following is a persistence.xml file that is setup to use MySQL as its database:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0">
    <persistence-unit name="mypersistenceunitname">
      <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
 
        <!-- Important -->
        <property name="hibernate.connection.provider_class"
          value="org.hibernate.connection.C3P0ConnectionProvider" />
 
        <property name="hibernate.c3p0.max_size" value="100" />
        <property name="hibernate.c3p0.min_size" value="0" />
        <property name="hibernate.c3p0.acquire_increment" value="1" />
        <property name="hibernate.c3p0.idle_test_period" value="300" />
        <property name="hibernate.c3p0.max_statements" value="0" />
        <property name="hibernate.c3p0.timeout" value="100" />
      </properties>
    </persistence-unit>
</persistence>

As you can see there is a lot of references to C3P0 in there. The most important property is the hibernate.connection.provider_class property. This property tells Hibernate that it wants to use C3P0 as its connection provider. The other references to C3P0 allow for fine tuning of how the connection pool behaves.

Other connection pools can be used in place of C3P0. Another common connection pool is DBCP.

For more information on how to configure a database connection pool for Hibernate check out:

http://www.hibernate.org/214.html

Although most of the information is regarding Hibernate 2. The comments are helpful for Hibernate 3.


  1. Ben
    March 18th, 2010 at 16:54 | #1

    I know this is old, but I’m starting a new seam project with c3p0 and JPA persistence… the persistence.xml that I have defines a JTA JNID data source, but c3p0 seems to want to have its own url/username/password in persistence.xml? How do I get it to look at my mysql-ds.xml file?

  2. Tomaž
    January 5th, 2011 at 07:28 | #2

    Thx for your post, JBOSS people they are not serious, in distribution package there is no mentioned that hey change c3p0 configuration.

    I was wondering what was going wrong with c3p0 pooling, just stupidity of JBOSS people.

    Regards, Tomaž

  3. Tomaž
    January 5th, 2011 at 07:29 | #3

    Thx for your post, JBOSS people they are not serious, in distribution package there is no mentioned that hey change c3p0 configuration.

    Important
    property name=”hibernate.connection.provider_class” value=”org.hibernate.connection.C3P0ConnectionProvider”

    I was wondering what was going wrong with c3p0 pooling, just stupidity of JBOSS people.

    Regards, Tomaž

  4. Sumit
    February 7th, 2011 at 02:34 | #4

    Even within the persistent file, persistence provider needs to be tweaked as a lot of options such as auto-increment, lazy-init, etc can be changed, which in turn would influence the application functionality.

  5. June 1st, 2011 at 02:59 | #5

    So what’s the best setting for a busy website with many reads and not many writes? (InnoDB)

  6. February 3rd, 2014 at 12:41 | #6

    This is not to say that you would spam the forums and blogs in question but rather offer your expertise
    and design an effective way to leverage these sites.
    An Internet marketing company will provide detailed monthly reports on the performances of
    your various campaigns which will help you track your
    business’s performance and improve your ROI. You may not be aware of this,
    but there are a few websites on the Internet today that will allow you to build
    a free web page.

  7. February 8th, 2014 at 03:59 | #7

    This is not to say that you would spam the forums and blogs in question but rather offer your expertise and design an effective way to leverage these sites.
    Spill Under is when the people under you sponsor
    people making your business grow. Search engine optimisation is the
    approach of employing methods that will have an influence of your natural and organic
    lookup engine rankings.

  8. February 10th, 2014 at 02:16 | #8

    Most Nigerians want to make money quickly without any work to
    be done and this is why so many are being scammed and duped.
    In publishing your ebook, make sure that people will not have a hard time to download
    your ebook internet marketing strategy and it is easy to locate.
    Most Hosting companies will also give you a discount if you pay in full, by the year.

  9. February 17th, 2014 at 02:09 | #9

    He was a house painter a few years ago and began
    his internet marketing career as an article writer.
    The above is a good example of fast strategies to
    grow a business on the Internet. Direct mailings over a
    period of time, or many successful marketers set the individual segments up into an email
    marketing auto responder program that sends the messages
    out to a predetermined list of clients and customers over a prearranged rate.

  1. No trackbacks yet.