We are developing a Java SE application and I am using Hibernate to communicate with the Database. My database is Oracle 11g Express Edition. Up till now I have not used a connection pool in the application. But I have used c3p0 connection pool before but I never really had a good understanding of it.
What are the Pros and Cons of using c3p0 in a Java SE application? I can understand it’s usefulness when it comes to a Java EE application but Java SE?
Here is my Configuration for Hibernate.
<?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>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.connection.username">EP</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">EP</property>
<property name="show_sql">true</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<mapping class="app.model.User"></mapping>
</session-factory>
</hibernate-configuration>
The reason for using a connection pool in Java SE is fundamentally the same as in the Java EE case. Openning and closing JDBC connections is relatively expensive, and connection pools allow you to reuse connections that the application openned previously.
(Obviously, if the Java SE application only ever needs to use one (or a small number) of JDBC connections, there is no pay-off in using a connection pool. But the same would apply to a Java EE application with the same characteristics.)
1) That entirely depends on the application, and what it does.
2) A connection pool is also useful for an application that requires multiple connections serially … but only ever one connection at a time. Like for example if the connections are being created and closed in a 3rd-party library.