I recently had deadlock issues with c3p0 and statement caching. Long story short, after investigating c3p0 code, I decided to switch to DBCP (maybe I’ll write a post with the long story).

I am not a big fan of Spring (here again, maybe I’ll write a post about that). If you are like me, here is how to programmatically configure Hibernate (JPA) to use DBCP, without Spring and without JNDI.

package com.opcodesolutions.demo;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.cfg.AvailableSettings;

import com.opcodesolutions.generic.jpa.entity.User;

public class HibernateJPAWithDBCP {

    public static void main(String[] args) {

        // Create and configure DBCP DataSource
        BasicDataSource dbcpDataSource = new BasicDataSource();

        // Enable statement caching (Optional)

        // JPA properties
        Map<String, Object> properties = new HashMap<>();

        // This line will tell hibernate (JPA) to use DBCP
        properties.put(AvailableSettings.DATASOURCE, dbcpDataSource);

        // Create the EntityManagerFactory with JPA properties
        EntityManagerFactory factory =
            Persistence.createEntityManagerFactory("User", properties);

        // Test with simple query
        EntityManager entityManager = factory.createEntityManager();
        TypedQuery<User> query =
            entityManager.createQuery("select u from User u", User.class);
        for (User user : query.getResultList()) {


With DBCP, all my deadlock issues disappeared. Thank you ASF.

Author: Jonathan Demers