如何配置连接池Apache Tomcat-> PostgreSQL-> Persistence Java?

时间:2019-02-05 23:41:42

标签: java postgresql tomcat jpa pool

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

到目前为止,我在不同的论坛中进行了搜索,发现的内容是在Apache tomcat的context.xhtml文件中添加以下行:

<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 Pool而不是直接连接。

当前的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>

1 个答案:

答案 0 :(得分:1)

尝试:

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