Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

PostgreSQL on Open Liberty

DZone's Guide to

PostgreSQL on Open Liberty

Learn how to configure Open Liberty, an OSS Java EE application server that originated from WebSphere Liberty, to use PostgreSQL as its default data source.

· Database Zone ·
Free Resource

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

Open Liberty is an interesting new OSS Java EE application server that originated from WebSphere Liberty. You can configure Open Liberty to use PostgreSQL as its default data source as follows.

Add directives for <datasource><jdbcDriver>, and library to the server.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<server description="OpenLiberty Java EE 8 Server">

    <featureManager>
        <feature>javaee-7.0</feature>
    </featureManager>

    <httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9080" httpsPort="9443"/>
    <keyStore id="defaultKeyStore" password="Liberty"/>

    <!-- Postgres coffee-shop-db definition -->
    <dataSource id="DefaultDataSource"
                jndiName="jdbc/sample"
                jdbcDriverRef="postgresql-driver"
                type="javax.sql.ConnectionPoolDataSource"
                transactional="true">

        <properties serverName="coffee-shop-db"
                    portNumber="5432"
                    databaseName="postgres"
                    user="postgres"
                    password="postgres"/>
    </dataSource>

    <jdbcDriver id="postgresql-driver"
                javax.sql.XADataSource="org.postgresql.xa.PGXADataSource"
                javax.sql.ConnectionPoolDataSource="org.postgresql.ds.PGConnectionPoolDataSource"
                libraryRef="postgresql-library"/>

    <library id="postgresql-library">
        <fileset id="PostgreSQLFileset" dir="/opt/ol/wlp/lib"
                includes="postgresql-9.4-1201.jar"/>
    </library>

</server>

The database available via host coffee-shop-db will be accessed through the default Postgres port 5432 with postgres as database name, username, and password.

The driver library, here postgresql-9.4-1201.jar, needs to reside in the lib/ directory of the server installation.

The default data source can be used transparently from a Java EE application. Only a single persistence unit needs to be specified in the persistence.xml file, similar to the following snippet:

<?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="prod" transaction-type="JTA"/>

</persistence>

If the application comprises multiple databases, the persistence units are required to specify the JNDI names of their corresponding data source.

Tested with the official Docker image open-liberty:javaee7.

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

Topics:
database ,postgresql ,open liberty ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}