Hibernate C3P0 pooling integration

Reading Time: 1 minute

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- Hibernate Settings -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://10.10.100.106:3306/nuspa?autoReconnect=true</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">xxxx</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.current_session_context_class">thread</property>
		<property name="hibernate.hbm2ddl.auto">validate</property>
		<property name="show_sql">true</property>

		<!-- C3P0 Pooling settings -->
		<property name="hibernate.c3p0.min_size">7</property>
		<property name="hibernate.c3p0.max_size">53</property>
		<property name="hibernate.c3p0.timeout">100</property>
		<property name="hibernate.c3p0.max_statements">50</property>
		<property name="hibernate.c3p0.idle_test_period">1000</property>
		<property name="hibernate.c3p0.validate">true</property>

		<!-- Mapping Entity classes -->
		<mapping class="com.tr.xxx.entity.Customer" />
		<mapping class="com.xxx.entity.Club" />
		<mapping class="com.tr.xxx.entity.TransactionHistory" />
	</session-factory>
</hibernate-configuration>

HibernateUtil.java

package com.xxx.util;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	private final static Logger logger = Logger.getLogger(HibernateUtil.class);

	static {
		try {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			logger.error("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

}

pom.xml

<properties>
		<hibernate-core.version>4.3.5.Final</hibernate-core.version>

	</properties>

<dependencies>
<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate-core.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>${hibernate-core.version}</version>
		</dependency>
</dependencies>

DAO.java

private final static Logger logger = Logger.getLogger(AppDAOImpl.class);
	final StringBuilder query = new StringBuilder();

	@Override
	public List<Club> getAllClubs() {
		logger.info("fetching all clubs from database");
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		List<Club> clubs = new ArrayList<>();
		try {
			clubs = session.createQuery("from Club").list();
			transaction.commit();
		} catch (HibernateException e) {
			logger.error("Exception in getting all clubs: " + e);
			transaction.rollback();
		}
		return clubs;
	}

Leave a Reply

Your email address will not be published. Required fields are marked *