I am new to hibernate and am running into an issue with open connections to the application database (Oracle 10g). It appears that when a new connection is needed, Hibernate is not obtaining a connection from the existing connection pool, but instead opening new connections. Also, once these connections are open, they are remaining open in the DB until the timeout is reached. For instance, when the FetchRespondentForLoginId method in the UserManager.java file below is called, the connection in Oracle is not being closed when I would expect on .close() in the application. Instead Oracle keeps this connection open until the timeout is reached. Config files and example call below, I really appreciate any insight:
hibernate.cfg.xml
Code:
<?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 name="myapplication">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@SERVER:PORT:SID</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.release_mode">on_close</property>
<property name="hibernate.default_schema">schema</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">60</property> <!-- seconds -->
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">300</property> <!-- seconds -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
HibernateUtil.java
Code:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
UserManager.java
Code:
public class RespondentManager extends HibernateDaoSupport {
public static Respondent FetchRespondentForLoginId(String userId)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria userCriteria = session.createCriteria(Respondent.class);
Criterion filterUserId = Restrictions.eq("userId", userId);
Criterion filterUserIdCustom = Restrictions.eq("userIdCustom", userId);
LogicalExpression orExp = Restrictions.or(filterUserId, filterUserIdCustom);
userCriteria.add(orExp);
List users = userCriteria.list();
session.getTransaction().commit();
session.close();
if (users != null && users.size() > 0)
return (Respondent)users.get(0);
else
return null;
}
public static List FetchRespondentsForHouseholdInformationId(Integer householdInformationId)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
List respondents = session.createCriteria(Respondent.class)
.add(Restrictions.eq("householdInformation.householdInformationId", householdInformationId))
.list();
session.getTransaction().commit();
session.close();
return respondents;
}
}