如何配置连接池ApacheTomcat->PostgreSQL->PersistenceJava?

How to configure a connection pool Apache Tomcat-> PostgreSQL-> Persistence Java?

我有一个 Java Web 项目 Java8 在 Apache Tomcat7 服务器上使用 Postgresql9.3。我正在使用 Persistence 连接到数据库,但我无法配置 Apache Tomcat 连接池来管理应用程序与数据库的流量。

到目前为止,我在不同的论坛中进行了搜索,我在 Apache context.xhtml 文件中找到了 tomcat 添加这些行:

<Resource name="jdbc/ead" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="postgres" password="postgres" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432/SIGENU_EaD"/>

在 web 项目的 web.xml 文件中添加这些行:

<resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/ead</res-ref-name>
        <res-type>org.postgresql.Driver</res-type>
        <res-auth>Container</res-auth>
</resource-ref>

我的问题是如何将此配置添加到 persistence.xml 文件中,以便在使用 Persistence 生成的 JpaControllers 时,使用 Apache Tomcat 池而不是直接连接。

当前 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="dist_educ_finalPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>entity.EstadoCivil</class>
        <class>entity.ProcedenciaEscolar</class>
        <class>entity.Disciplina</class>
        <class>entity.Planestudio</class>
        <class>entity.FuenteIngreso</class>
        <class>entity.TipoAsignatura</class>
        <class>entity.Especialidad</class>
        <class>entity.MatriculaEstudianteAsignatura</class>
        <class>entity.Organismo</class>
        <class>entity.Asignatura</class>
        <class>entity.Huerfano</class>
        <class>entity.Tutor</class>
        <class>entity.ColorPiel</class>
        <class>entity.GradoMilitar</class>
        <class>entity.EspecialidadMilitar</class>
        <class>entity.Authorities</class>
        <class>entity.Ocupacion</class>
        <class>entity.Carreranacional</class>
        <class>entity.Minusvalia</class>
        <class>entity.Estudiante</class>
        <class>entity.Sexo</class>
        <class>entity.NivelEscolar</class>
        <class>entity.Users</class>
        <class>entity.Universidad</class>
        <class>entity.OrganizacionPolitica</class>
        <class>entity.OrganizacionPopular</class>
        <class>entity.Municipio</class>
        <class>entity.TipoEvaluacion</class>
        <class>entity.Examen</class>
        <class>entity.Matricula</class>
        <class>entity.MatriculaEstudianteAsignaturaExamen</class>
        <class>entity.Pais</class>
        <class>entity.Centrotrabajo</class>
        <class>entity.EstadoEstudiante</class>
        <class>entity.Curso</class>
        <class>entity.Provincia</class>
        <class>entity.Ong</class>
        <class>entity.Sindicato</class>
        <class>entity.Area</class>
        <class>entity.Carrera</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/SIGENU_EaD"/>
            <property name="javax.persistence.jdbc.user" value="postgres"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.password" value="postgres"/>
        </properties>
    </persistence-unit>
</persistence>

尝试:

<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="dist_educ_finalPU" transaction-type="RESOURCE_LOCAL">   
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>java:comp/env/jdbc/ead</non-jta-data-source>
    ...
  </persistence-unit>
<persistence>