對於JavaSE客戶端,如何處理DBCP?使用JDBC,我知道如何添加Apache DBCP。我可以將Apache DBCP添加到JPA嗎?如果是這樣,怎麼樣?來自JavaSE的DBCP(數據庫連接池)和JPA
persistence.xml
文件是否有這個選項?
代碼:
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>
我沒有使用Tomcat,還是其他容器。
看一下[HikariCP](https://github.com/brettwooldridge/HikariCP),然後查看各種配置選項 - 包括**示例屬性文件:**。 – 2014-08-31 23:44:34
我看到'maximumPoolSize',但沒有看到示例。 – Thufir 2014-08-31 23:49:01
提示,它有它自己的標題爲「示例屬性文件:」 – 2014-08-31 23:49:48