Creating a Java application with a database
Prerequisites
A running cluster.
odo
is installed.A Service Binding Operator is installed in your cluster. To learn how to install Operators, contact your cluster administrator or see Installing Operators from OperatorHub.
A Dev4Devs PostgreSQL Operator Operator is installed in your cluster. To learn how to install Operators, contact your cluster administrator or see .
Create a project to keep your source code, tests, and libraries organized in a separate single unit.
Procedure
Log in to an OKD cluster:
Create a project:
$ odo project create myproject
Example output
✓ Project 'myproject' is ready for use
✓ New project created and now using project : myproject
With odo
, you can create and manage a sample Java MicroServices JPA application.
Procedure
Clone the sample application:
$ git clone -b jpa-sample https://github.com/redhat-developer/application-stack-samples.git
Navigate to the application directory:
$ cd ./application-stack-samples/jpa
Initialize the project:
$ odo create java-openliberty java-application
-
The application is now deployed to the cluster.
View the status of the cluster by streaming the OpenShift logs to the terminal:
$ odo log
Notice the test failures and
UnknownDatabaseHostException
error. This is because your application does not have a database yet:Create an ingress URL to access the application:
$ odo url create --port 8080
Push the changes to your cluster:
$ odo push
Display the created URL:
$ odo url list
Example output
Found the following URLs for component mysboproj
java-application-8080 Pushed http://java-application-8080.apps-crc.testing 8080 false ingress
The application is now deployed to the cluster and you can access it by using the URL that is created.
Use the URL to navigate to the
CreatePerson.xhtml
data entry page and enter a username and age by using the form. Click Save.Note that you cannot see the data by clicking the View Persons Record List link since your application does not have a database connected yet.
To create a database, you must have an access to the database Operator. For this example, Dev4Devs PostgreSQL Operator is used.
Procedure
View the list of the services in your project:
$ odo catalog list services
Example output
Operators available in the cluster
NAME CRDs
postgresql-operator.v0.1.1 Backup, Database
Store the YAML of the service in a file:
$ odo service create postgresql-operator.v0.1.1/Database --dry-run > db.yaml
-
This configuration ensures that when a database service is started, appropriate annotations are added to it. Annotations help the Service Binding Operator in injecting the values for
databaseName
,databasePassword
, anddatabaseUser
into the application. Create a database from the YAML file:
$ odo service create --from-file db.yaml
A database instance is now present in your project.
To connect your Java application to the database, use the odo link
command.
Procedure
Display the list of services:
$ odo service list
Example output
NAME AGE
Database/sampledatabase 6m31s
Connect the database to your application:
$ odo link Database/sampledatabase
Push the changes to your cluster:
$ odo push
After the link has been created and pushed, a secret that contains the database connection data is created.
Check the component for values injected from the database service:
$ odo exec -- bash -c 'env | grep DATABASE'
declare -x DATABASE_CLUSTERIP="10.106.182.173"
declare -x DATABASE_DB_NAME="sampledb"
declare -x DATABASE_DB_PASSWORD="samplepwd"
declare -x DATABASE_DB_USER="sampleuser"
Open the URL of your Java application and navigate to the
CreatePerson.xhtml
data entry page. Enter a username and age by using the form. Click Save.Note that now you can see the data in the database by clicking the View Persons Record List link.
You can also use a CLI tool such as
psql
to manipulate the database.