Microservice: Creating JPA Application Using Jakarta Persistence API in Payara Micro
In this blog, we will discuss how your Java microservice application can connect to and interact with a relational database through the Jakarta Persistence API.
In this blog, we will discuss how your Java microservice application can connect to and interact with a relational database through the Jakarta Persistence API (the latest version at the time of writing is version 3.0). You can configure a data source and a JDBC driver so an application that is running on your Payara Microserver can connect with a relational database.
Before We Begin:
In this tutorial, we will configure this application with the following components in mind:
- The microservice application will be a Mavenized Java application.
- We will use MySQL DB Server as a relational database.
- The JDBC data source will be a transactional data source (
XADataSource).
JDBC Driver Library Configuration With Maven
You need a JDBC driver to connect your Java application with a relational database. The driver is usually provided by the database vendor. Fortunately, most database vendors have released their JDBC drivers to a Maven repository.
If you use Maven to build your application, you can add your JDBC driver by adding code that is similar to the following example to your pom.xml file.
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
With Maven, the JDBC driver file will be copied to the WEB-INF/lib folder of your application WAR file during the package build phase.
Configuring a Database Datasource
In the Payara Blog, a data source is configured inside the web.xml file. In this example, there is another alternative for creating a data source configuration. The example below demonstrates a basic data source configuration pattern for the payara-resource.xml file inside the src/main/java/webapp/WEB-INF folder:
<!DOCTYPE resources PUBLIC "-//Payara.fish//DTD Payara Server 4 Resource Definitions//EN" "https://raw.githubusercontent.com/payara/Payara-Community-Documentation/master/docs/modules/ROOT/pages/schemas/payara-resources_1_6.dtd">
<resources>
<jdbc-resource pool-name="MyAppDS"
jndi-name="java:app/jdbc/MyApp"
enable="true"/>
<jdbc-connection-pool name="MySQLPool"
res-type="javax.sql.XADataSource"
datasource-classname="com.mysql.cj.jdbc.MysqlXADataSource">
<property name="url" value="jdbc:h2:mem:hibernateExample"/>
<property name="User" value="testUser"></property>
<property name="Password" value="testPassword"></property>
<property name="DatabaseName" value="myapp_db"></property>
<property name="ServerName" value="localhost"></property>
<property name="PortNumber" value="3306"></property>
</jdbc-connection-pool>
</resources>
Application Configuration for Relational Database Connections
To use a data source that is configured in your payara-resource.xml file, you can either inject the data source or specify a lookup in your application code. The following examples assume that a jndi-name value of java:app/jdbc/MyApp is specified as the jdbc-resource element attribute in the payara-resource.xml file.
@Resource(name= "java:app/jdbc/MyApp")
DataSource myDB;
Injecting JPA EntityManager Into Your Application
Once your data source is configured in your payara-resource.xml file, we need to register your data source inside the persistence.xml in your src/main/resources/META-INF/persistence.xml file. The jta-data-source is and matches the jndi-name of the data source as specified in the payara-resource.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="MyAppPU" transaction-type="JTA">
<jta-data-source>java:app/jdbc/MyApp</jta-data-source>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<!-- JBoss Wildfly's Hibernate 4 specific JPA properties -->
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" /><!-- NEVER: update -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<!-- For Performance monitoring on Hibernate -->
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.cache.use_structured_entries" value="false"/>
<!-- TomEE PluME 1.7.2 and higher with EclipseLink -->
<property name="eclipselink.logging.logger" value="JavaLogger" />
</properties>
</persistence-unit>
</persistence>
You can inject your jakarta.persistence.EntityManager in your Java application code by specifying your persistence unit MyAppPU in your PersistenceContext annotation:
@PersistenceContext(unitName="MyAppPU")
private EntityManager entityManager;
With that, you can get your Java microservice application to connect and interact with a relational database through the Jakarta Persistence API!
Opinions expressed by DZone contributors are their own.