Add c3p0 to previous example..

.. and refactor it a little to harness Dependency Injection!

All started with this example, but things changed a little.

Directory Layout

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>jpa-examples</artifactId>
    <packaging>jar</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>jpa-examples</name>
    <url>http://www.tugay.biz</url>
 
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>5.2.10.Final</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.192</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>jpa-examples</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.jpaExamples.App</mainClass>
                    <commandlineArgs>-Dfile.encoding=UTF-8</commandlineArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
             version="2.0">
 
    <persistence-unit name="jpaExamples" transaction-type="RESOURCE_LOCAL">
        <class>biz.tugay.jpaExamples.model.Person</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/sampleh2db;TRACE_LEVEL_FILE=0;AUTO_SERVER=TRUE"/>
            <property name="javax.persistence.jdbc.user" value="test"/>
            <property name="javax.persistence.jdbc.password" value="test"/>
        </properties>
    </persistence-unit>
</persistence>

hibernate.properties
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=10
hibernate.c3p0.timeout=300
hibernate.c3p0.max_statements=5
hibernate.c3p0.unreturnedConnectionTimeout=2

EntityManagerService.java
package biz.tugay.jpaExamples.service;
 
import javax.persistence.EntityManager;
 
public interface EntityManagerService {
    EntityManager entityManager();
    void shutDown();
}

EntityManagerServiceByPersistenceUnitNameImpl.java
package biz.tugay.jpaExamples.service;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
public final class EntityManagerServiceByPersistenceUnitNameImpl implements EntityManagerService {
 
    private final EntityManagerFactory entityManagerFactory;
 
    public EntityManagerServiceByPersistenceUnitNameImpl(final String persistenceUnitName) {
        this.entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
    }
 
    @Override
    public EntityManager entityManager() {
        final EntityManager entityManager = entityManagerFactory.createEntityManager();
        return entityManager;
    }
 
    @Override
    public void shutDown() {
        entityManagerFactory.close();
    }
}

PersonDaoImpl.java
package biz.tugay.jpaExamples.dao;
 
import biz.tugay.jpaExamples.model.Person;
import biz.tugay.jpaExamples.service.EntityManagerService;
 
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.List;
 
public final class PersonDaoImpl {
 
    private EntityManagerService entityManagerService;
 
    public void setEntityManagerService(final EntityManagerService entityManagerService) {
        this.entityManagerService = entityManagerService;
    }
 
    public List<Person> all() {
        final EntityManager entityManager = entityManagerService.entityManager();
        final TypedQuery<Person> selectAllPersons = entityManager.createQuery("SELECT p FROM Person p", Person.class);
        final List<Person> persons = selectAllPersons.getResultList();
        entityManager.close();
        return persons;
    }
}

App.java
package biz.tugay.jpaExamples;
 
import biz.tugay.jpaExamples.model.Person;
import biz.tugay.jpaExamples.service.EntityManagerService;
import biz.tugay.jpaExamples.service.EntityManagerServiceByPersistenceUnitNameImpl;
import biz.tugay.jpaExamples.dao.PersonDaoImpl;
 
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class App {
 
    static {
        final Logger globalLogger = Logger.getLogger("");
        globalLogger.setLevel(Level.OFF);
    }
 
    public static void main(final String[] args) throws {
        final EntityManagerService entityManagerService = new EntityManagerServiceByPersistenceUnitNameImpl("jpaExamples");
        final PersonDaoImpl personDao = new PersonDaoImpl();
        personDao.setEntityManagerService(entityManagerService);
 
        final List<Person> persons = personDao.all();
 
        for (Person person : persons) {
            System.out.println(person);
        }
 
        entityManagerService.shutDown();
    }
}

Person.java and Build & Run is exactly same as seen in here. Our project is already getting quite big, no?