DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Persistence for Java Microservices in Kubernetes Via JPA

Persistence for Java Microservices in Kubernetes Via JPA

We continue building out a microservice application by using JPA to add data persistence.

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
May. 23, 19 · Tutorial
Like (4)
Save
Tweet
Share
9.96K Views

Join the DZone community and get the full member experience.

Join For Free

over the past few weeks i've worked on an example application that demonstrates how java ee developers can get started with microservices. the application is a full end-to-end sample which includes a web application, business logic, authentication, and now also persistence. it runs on kubernetes and istio and there are scripts to easily deploy it.

get the cloud-native-starter code from github.

java persistence api

in the example i use a full open source java stack with openj9, openjdk, open liberty, and microprofile. in order to deploy the microservices to kubernetes, i've created an image. read my article dockerizing java microprofile applications for details.

open liberty provides some pretty good guides. one guide is specifically about jpa: accessing and persisting data in microservices . i don't want to repeat everything here, but only highlight the changes i had to do to run this functionality in a container, rather than via a local open liberty installation.

here is a short description of jpa:

jpa is a java ee specification for representing relational database table data as plain old java objects (pojo). jpa simplifies object-relational mapping (orm) by using annotations to map java objects to tables in a relational database. in addition to providing an efficient api for performing crud operations, jpa also reduces the burden of having to write jdbc and sql code when performing database operations and takes care of database vendor-specific differences.

configuration of the sample microservices application

the following diagram shows the simplied architecture of the cloud-native-starter example. a web application invokes through ingress, the web-api service that implements a backend-for-frontend pattern. the web-api service invokes the articles service which stores data in a sql database on the ibm cloud . obviously, you can use any other sql database instead.

in order to access the db2 on the ibm cloud, first the driver needs to be downloaded via maven . note that the driver does not go into the war files together with the business logic of the microservices, but it needs to be copied in a certain open liberty directory : /opt/ol/wlp/usr/shared/resources/jcc-11.1.4.4.jar

next you need to define in server.xml information about the driver and the data source.

<server description="openliberty server">
    <featuremanager>
        <feature>webprofile-8.0</feature>
        <feature>microprofile-2.1</feature>
    </featuremanager>

    <httpendpoint id="defaulthttpendpoint" host="*" httpport="8080" httpsport="9443"/>

    <library id="db2jcclib">
        <fileset dir="${shared.resource.dir}" includes="jcc*.jar"/>
    </library>

    <datasource id="articlejpadatasource"
              jndiname="jdbc/articlejpadatasource">
        <jdbcdriver libraryref="db2jcclib" />
        <properties.db2.jcc databasename="bludb"
            portnumber="50000"
            servername="db2-server"         
            user="db2-user" 
            password="db2-password" />
  </datasource>
</server>

next the persistence unit needs to be define in persistence.xml .

the tricky part was for me to figure out the right location for this file. in order for all maven versions to build it correctly i put it in 'src/main/resources/meta-inf/persistence.xml'. this produces an articles.war file with the internal structure 'classes/meta-inf/persistence.xml'.

<persistence version="2.2"
    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_2.xsd">
    <persistence-unit name="jpa-unit" transaction-type="jta">
        <jta-data-source>jdbc/articlejpadatasource</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="both" />
        </properties>
    </persistence-unit>
</persistence>

usage of jpa in java

once all the configuration has been done, writing the java code is simple.

first you need to define the java class which represents the entries in a table. check out the code of articleentity.java with the five columns id, title, url, author and creation date. as defined in persistence.xml this table is created automatically.

the crud operations for articles are defined in articledao.java . the code is pretty straight forward. the only thing that confused me, was that i have to begin and commit transactions manually for the create operation. in the open liberty sample this was not necessary. i'm trying to find out what the difference is.

in jpadataaccess.java the logic is implemented to add and read articles. the articledao is injected. again, the code looks simple. the lesson that i learned here is, that dependency injection only seems to work when the upper layers that invoke this code use dependency injection and @applicationscoped as well.

how to run the example

i've written scripts to create the sql database and create the articles service. check out the documentation to run the sample yourself on minikube or the ibm cloud kubernetes service .

once installed the openapi api explorer can be used to create a new article.

the table is displayed in the db2 console.

the data in the table can be displayed in the console as well.

to learn more about microservices built with java and microprofile, check out the cloud-native-starter repo.

Java (programming language) Database microservice Kubernetes Relational database Persistence (computer science) application

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Multi-Cloud Database Deep Dive
  • Unit of Work With Generic Repository Implementation Using .NET Core 6 Web API
  • What Should You Know About Graph Database’s Scalability?
  • 9 Ways You Can Improve Security Posture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: