Creating a Java application with a database

This example describes how to deploy a Java application by using devfile and connect it to a database service.

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 Installing Operators from OperatorHub.

Creating a project

Create a project to keep your source code, tests, and libraries organized in a separate single unit.

Procedure

  1. Log in to an OKD cluster:

    1. $ odo login -u developer -p developer
  2. Create a project:

    1. $ odo project create myproject

    Example output

    1. Project 'myproject' is ready for use
    2. New project created and now using project : myproject

Creating a Java MicroServices JPA application

With odo, you can create and manage a sample Java MicroServices JPA application.

Procedure

  1. Clone the sample application:

    1. $ git clone -b jpa-sample https://github.com/redhat-developer/application-stack-samples.git
  2. Navigate to the application directory:

    1. $ cd ./application-stack-samples/jpa
  3. Initialize the project:

    1. $ odo create java-openliberty java-application
  4. Push the application to the cluster:

    1. $ odo push

    The application is now deployed to the cluster.

  5. View the status of the cluster by streaming the OpenShift logs to the terminal:

    1. $ odo log

    Notice the test failures and UnknownDatabaseHostException error. This is because your application does not have a database yet:

    1. [INFO] [err] java.net.UnknownHostException: ${DATABASE_CLUSTERIP}
    2. [INFO] [err] at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
    3. [INFO] [err] at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    4. [INFO] [err] at java.base/java.net.Socket.connect(Socket.java:609)
    5. [INFO] [err] at org.postgresql.core.PGStream.<init>(PGStream.java:68)
    6. [INFO] [err] at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
    7. [INFO] [err] ... 86 more
    8. [ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0, Time elapsed: 0.706 s <<< FAILURE! - in org.example.app.it.DatabaseIT
    9. [ERROR] testGetAllPeople Time elapsed: 0.33 s <<< FAILURE!
    10. org.opentest4j.AssertionFailedError: Expected at least 2 people to be registered, but there were only: [] ==> expected: <true> but was: <false>
    11. at org.example.app.it.DatabaseIT.testGetAllPeople(DatabaseIT.java:57)
    12. [ERROR] testGetPerson Time elapsed: 0.047 s <<< ERROR!
    13. java.lang.NullPointerException
    14. at org.example.app.it.DatabaseIT.testGetPerson(DatabaseIT.java:41)
    15. [INFO]
    16. [INFO] Results:
    17. [INFO]
    18. [ERROR] Failures:
    19. [ERROR] DatabaseIT.testGetAllPeople:57 Expected at least 2 people to be registered, but there were only: [] ==> expected: <true> but was: <false>
    20. [ERROR] Errors:
    21. [ERROR] DatabaseIT.testGetPerson:41 NullPointer
    22. [INFO]
    23. [ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0
    24. [INFO]
    25. [ERROR] Integration tests failed: There are test failures.
  6. Create an ingress URL to access the application:

    1. $ odo url create --port 8080
  7. Push the changes to your cluster:

    1. $ odo push
  8. Display the created URL:

    1. $ odo url list

    Example output

    1. Found the following URLs for component mysboproj
    2. NAME STATE URL PORT SECURE KIND
    3. 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.

  9. 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.

Creating a database with odo

To create a database, you must have an access to the database Operator. For this example, Dev4Devs PostgreSQL Operator is used.

Procedure

  1. View the list of the services in your project:

    1. $ odo catalog list services

    Example output

    1. Operators available in the cluster
    2. NAME CRDs
    3. postgresql-operator.v0.1.1 Backup, Database
  2. Store the YAML of the service in a file:

    1. $ odo service create postgresql-operator.v0.1.1/Database --dry-run > db.yaml
  3. Add the following values under the metadata: section in the db.yaml file:

    1. name: sampledatabase
    2. annotations:
    3. service.binding/db.name: 'path={.spec.databaseName}'
    4. service.binding/db.password: 'path={.spec.databasePassword}'
    5. service.binding/db.user: 'path={.spec.databaseUser}'

    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, and databaseUser into the application.

  4. Change the following values under the spec: section of the YAML file:

    1. databaseName: "sampledb"
    2. databasePassword: "samplepwd"
    3. databaseUser: "sampleuser"
  5. Create a database from the YAML file:

    1. $ odo service create --from-file db.yaml

    A database instance is now present in your project.

Connecting a Java application to a database

To connect your Java application to the database, use the odo link command.

Procedure

  1. Display the list of services:

    1. $ odo service list

    Example output

    1. NAME AGE
    2. Database/sampledatabase 6m31s
  2. Connect the database to your application:

    1. $ odo link Database/sampledatabase
  3. Push the changes to your cluster:

    1. $ odo push

    After the link has been created and pushed, a secret that contains the database connection data is created.

  4. Check the component for values injected from the database service:

    1. $ odo exec -- bash -c 'env | grep DATABASE'
    2. declare -x DATABASE_CLUSTERIP="10.106.182.173"
    3. declare -x DATABASE_DB_NAME="sampledb"
    4. declare -x DATABASE_DB_PASSWORD="samplepwd"
    5. declare -x DATABASE_DB_USER="sampleuser"
  5. 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.