gpt4 book ai didi

java - 来自 JavaSE 和 JPA 的 DBCP(数据库连接池)

转载 作者:行者123 更新时间:2023-12-02 05:30:28 26 4
gpt4 key购买 nike

对于 JavaSE 客户端,DBCP 是如何处理的?有了 JDBC,我知道如何添加 Apache DBCP。我可以将 Apache 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;
}

}

持久性.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,也没有使用其他容器。

最佳答案

根据您的 persistence.xml,您使用连接 URL、驱动程序名称和要使用的用户名/密码创建 EntityManagerFactory,这不一定会池化连接。因此,如果不使用 Spring 或,则无法将 Apache DBCP 添加到 JPA其他平台如 DataNucleus 。如果你使用hibernate,你可以像这样配置hibernate-annotation.cfg.xml,

<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>

关于java - 来自 JavaSE 和 JPA 的 DBCP(数据库连接池),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com