0

For a JavaSE client, how is DBCP handled? With JDBC, I know how to add Apache DBCP. Can I add Apache DBCP to JPA? If so, how?

Does the persistence.xml file have an option for this?

code:

package legacy.database;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

public class MyQueries {

    private static final Logger log = Logger.getLogger(MyQueries.class.getName());
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("LegacyDatabasePU");
    private EntityManager em = emf.createEntityManager();

    public MyQueries() {
    }

    private List<Clients> findAll() {
        Query q = em.createQuery("select c from Clients c");
        List<Clients> clients = q.getResultList();
        em.close();
        return clients;
    }

    public List<Clients> selectByCriteria(Criteria criteria) {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Clients> clientCriteriaQuery = criteriaBuilder.createQuery(Clients.class);
        Root<Clients> clientRoot = clientCriteriaQuery.from(Clients.class);
        clientCriteriaQuery.select(clientRoot);
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(criteriaBuilder.like(clientRoot.get(Clients_.phone1), "%" + criteria.getPhone1() + "%"));
        if (!criteria.getStatus().equalsIgnoreCase("all")) {
            predicates.add(criteriaBuilder.like(clientRoot.get(Clients_.status), "%" + criteria.getStatus() + "%"));
        }
        clientCriteriaQuery.where(predicates.toArray(new Predicate[0]));
        List<Clients> clients = em.createQuery(clientCriteriaQuery).getResultList();
        em.close();
        return clients;
    }

    public Clients findById(int id) {
        Clients client = em.find(Clients.class, id);
        em.close();
        return client;
    }

}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="LegacyDatabasePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>legacy.database.Clients</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/legacy?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.password" value="gjkgjtd"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="jdbc"/>
    </properties>
  </persistence-unit>
</persistence>

I'm not using Tomcat, nor other container.

Thufir
  • 8,216
  • 28
  • 125
  • 273

2 Answers2

1

According to your persistence.xml,you create an EntityManagerFactory using the connection URL, driver name and the username/password to use, this doesn't necessarily pool the connections.So you cannot add Apache DBCP to JPA without using Spring or other platform like DataNucleus. If you using with hibernate, you can configure hibernate-annotation.cfg.xml likes this,

<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myschema</property>
  <property name="hibernate.connection.username">user</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>

  <property name="hibernate.dbcp.initialSize">8</property>
  <property name="hibernate.dbcp.maxActive">20</property>
  <property name="hibernate.dbcp.maxIdle">20</property>
  <property name="hibernate.dbcp.minIdle">0</property>
</session-factory>
</hibernate-configuration>
Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
  • Ok...so, if I switch to java. SE and hibernate, is there some form of DBCP. available? – Thufir Sep 01 '14 at 16:06
  • except that is Hibernate-specific and he said JPA. So you could easily change your code example to be for persistence.xml (i.e the same persistence properties but in a persistence.xml file) –  Sep 16 '14 at 07:58
1

Yes, it's possible. You can pass a data source from your code, as this blog post shows.

llogiq
  • 13,815
  • 8
  • 40
  • 72
  • Datasource is in toplink? It allows. Connection. Pooling? – Thufir Sep 02 '14 at 09:28
  • You just need to edit your code to pass an additional Map with one entry with key PersistenceUnitProperties.NON_JTA_DATASOURCE and your instantiated DataSource as value. The latter in your case should be a DBCPDataSource. Note that this is the only entry you want to put in the map. – llogiq Sep 02 '14 at 10:12
  • the blog post is about eclipselink. do I need to switch from toplink to eclipselink to do this? – Thufir Sep 02 '14 at 10:15
  • I haven't used TopLink yet, but as the API in question is standard JPA, it should work with just about every JPA implementation I can think of. – llogiq Sep 02 '14 at 11:48
  • ok. it looks like `ClientDataSource` is just for derby, I'm using MySQL, I'll have to google a bit tomorrow. looks interesting :) – Thufir Sep 02 '14 at 11:54