Search code examples
javaglassfishjndipayara

correct way to include a database connection to POSTGRES in payara micro war file


I managed to deploy and use a JPA connection using the below files. This is currently working, but it's not ideal.

The connection should be using the postgres pool driver, and a JNDI data source rather than a data source in web.xml.

What is the best practice way of deploying a database connection in a JEE container.

Here is my current setup, using a global connection that is not using the pooled driver.

persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.1"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="pcc" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>java:global/pccData</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.deploy-on-startup" value="true"/>
            <property name="eclipselink.logging.level" value="INFO"/>
            <property name="eclipselink.logging.level.sql" value="CONFIG"/>
            <property name="eclipselink.jdbc.fetch-size" value="1000"/>
            <property name="eclipselink.jdbc.cache-statements" value="true"/>
            <property name="eclipselink.persistence-context.flush-mode" value="commit"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        </properties>
    </persistence-unit>

</persistence>

web.xml
I am sure PGSimpleDataSource is not the recommended approach

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>PCC Web Application</display-name>
  <data-source>
    <name>java:global/pccData</name>
    <class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
    <server-name>localhost</server-name>
    <port-number>5432</port-number>
    <database-name>pcc</database-name>
    <user>postgres</user>
    <password>postgres</password>
    <property>
      <name>fish.payara.slow-query-threshold-in-seconds</name>
      <value>5</value>
    </property>
  </data-source>
</web-app>

Solution

  • To answer your question:

    What is the best practice way of deploying a database connection in a JEE container?

    This is pretty much it.

    It doesn't need to be any more complex than the set up you currently have. I assume you have already seen the official Payara-Examples repository but, if not, there is a JPA example there which is configured exactly as you have in your question:
    https://github.com/payara/Payara-Examples/blob/master/Payara-Micro/jpa-datasource-example/ReadMe.md

    As a general point about configuring Payara Micro, you can think of it as a Payara Server in a different package, so if you're still unsure about something and want to do things in the same way as in the more traditional server then you can do.

    There are --postbootcommandfile and --postdeploycommandfile options which will allow you to run asadmin commands against Payara Micro and configure it just like Payara Server.

    For your connection pool example, if you really wanted to define it in the server (really, defining it in the web.xml as you already have done is more portable and would be my preferred option), then you could start a normal Payara Server, click the button to enable asadmin command recording in the admin console, and then make the changes using the GUI. The necessary commands will then by dumped to a file for you to apply to Payara Micro later.

    Payara Micro also dumps out a temporary directory (controllable with --rootDir, by default has the same value as java.io.tmpdir) so you can always see how a configuration change has affected it by inspecting the domain.xml in that directory structure. This gives you some form of manual verification.