Search code examples
javapostgresqltomcatjpapool

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


I have a Java Web project with Java8 using Postgresql9.3 on an Apache Tomcat7 Server. I am using Persistence for the connection to the database but I can not configure the Apache Tomcat Connection Pool to manage the traffic of the App with the Database.

So far I have searched in different forums and what I have found is in the context.xhtml file of Apache tomcat add these lines:

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

In the web.xml file of the web project add these lines:

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

My question is how to add this configuration to the persistence.xml file so that when using the JpaControllers generated by Persistence, use the Apache Tomcat Pool instead of a direct connection.

The current persistence.xml file looks like this:

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

Solution

  • Try:

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