Containerize Jakarta EE Application with Red Hat EAP in Openshift #1

Li Khia
5 min readSep 24, 2021

Database is commonly required for storing data for applications. Datasource is used to configure the connection pool of the database that applications need to use on the Jakarta EE Application server.

This blog shares how to install MySQL drivers and configure data sources when deploying the application in Red Hat EAP Operator [1].

Please note that this blog is not meant to share components in Openshift so there is no explanation on the Openshift Components used here. Please refer to the links provided below for more information.

Environment and Application Set up

I already have an instance of MySQL database [5] installed in the common project in the same Openshift cluster. Please refer to the links provided below for more information.

The application is using both Java API and Hibernate referring to the data source that is configured in Red Hat EAP.

Just to share some code snippets using datasource JNDI.

  • Below screenshot is the code snippet of the servlet that creates a record into the database and retrieves a list of records from the database.
  • @Resource(lookup=”java:jboss/datasources/workshop-mysql”) is using the Java API to get datasource.
  • RegistrationService is the class that uses Hibernate to integrate with the database.
Servlet that used the datasource
  • Below screenshot showed the persistence.xml to configure the datasource in hibernate. The same datasource is configured with <jta-data-source> tag.
persistence.xml
  • Below screenshot showed that RegistrationService used the configuration (i.e. PersistenceContext) in persistence.xml to connect with the datasource to create new record (e.g. Member)
The class that uses the persistenceContext in persistence.xml

Add MySQL Driver as Module in EAP

The following files are required to be created under the root directory of the application project so that MySQL driver will be added as a module when using S2I [2] to build the application container image,

  1. install.sh script is responsible for customizing the base image using APIs provided by install-common.sh. install-common.sh contains functions that are used by the install.sh script to install and configure the modules, drivers, and generic deployments. It will execute during the s2i process.

2. module.xml contains the configuration of the driver jar to use and its dependencies.

3. driver.env contains the details of the database driver.

  • The value of MYSQL_DRIVER_MODULE is the same as the name in ‘module.xml’ mentioned earlier.
  • The value of MYSQL_DRIVER_NAME will be used to configure the datasource later.

Build Application Container Image

Execute the following in the root directory of the application project to build the application container image. Add CUSTOM_INSTALL_DIRECTORIES as environment variable and value as the current directory (.) so that install.sh will be executed during the S2I process.

oc login <URL> -u <username> -p <password>

oc project <project>

oc new-build — name=eap-db — binary — image-stream=jboss-eap73-openjdk11-openshift:7.3 -e CUSTOM_INSTALL_DIRECTORIES=.

oc start-build eap-db — from-dir=. — follow

After the build is successful, a container image will be created in the current Openshift project. Please take note of the URL under image repository as it is required to deploy the application later.

The password of the database should not show in clear text in yaml so execute the command below to create a secret [3].

oc create secret generic db-secret — from-literal=password=openshift

Deploy Application in Red Hat EAP Operator

In the Openshift console, navigate to the Red Hat EAP operator. Click on the WildFlyServer tab. Click on [Create WildFlyServer].

Click on Yaml View. Copy and paste below and click on [Create]. This will use the earlier generated image with the environment variables below to run this application in a standalone server. With these environment variables, the datasource with JNDI = java:jboss/datasources/workshop-mysql will be created. The secret created earlier is used for the password of the database.

YAML to create EAP servers

Below explains the usage of these environment variables.

  • DB_SERVICE_PREFIX_MAPPING=POOLNAME-DATABASETYPE=PREFIX
  • POOLNAME_DATABASETYPE_SERVICE_HOST=The database server’s host name or IP address to be used in the datasource’s connection-url property. POOLNAME and DATABASETYPE must be in upper case.
  • POOLNAME_DATABASETYPE_SERVICE_PORT=The database server’s port for the datasource. POOLNAME and DATABASETYPE must be in upper case.
  • PREFIX_JNDI=The JNDI name for the datasource
  • PREFIX_URL=The connection URL for the datasource.
  • PREFIX_DATABASE=The database name for the datasource.
  • PREFIX_DRIVER=Java database driver for the datasource that is configured in driver.env.
  • PREFIX_USERNAME=The username for the datasource.
  • PREFIX_PASSWORD=The password for the datasource.

After the pod is running, you can access the application using the Location of the corresponding route [4].

The source codes are available @ ​​https://github.com/likhia/eap73-ocp-db.git.

Please refer to the links below for more information on the corresponding topic.

[1] Operators : https://docs.openshift.com/container-platform/4.8/operators/understanding/olm-what-operators-are.html

[2] Source2Image : https://docs.openshift.com/container-platform/4.8/openshift_images/using_images/using-s21-images.html

[3] Secret : https://docs.openshift.com/container-platform/4.8/nodes/pods/nodes-pods-secrets.html

[4] Route : https://docs.openshift.com/container-platform/4.8/networking/routes/route-configuration.html

[5] Install MySQL in Openshift: https://medium.com/fnplus/creating-deploying-and-exposing-a-mysql-database-container-on-redhat-openshift-8fda5a81e1a2

--

--