Skip to content

Commit 2c18ea6

Browse files
author
Yana Hontyk
committed
Add Java+DB odo documentation
1 parent 5b3bfd3 commit 2c18ea6

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed

_topic_map.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ Topics:
489489
File: creating-a-multicomponent-application-with-odo
490490
- Name: Creating an application with a database
491491
File: creating-an-application-with-a-database
492+
- Name: Creating a Java application with a database
493+
File: creating-a-java-application-with-a-database
492494
- Name: Using devfiles in odo
493495
File: using-devfiles-in-odo
494496
- Name: Working with storage
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[id=creating-a-java-application-with-a-database]
2+
= Creating a Java application with a database
3+
include::modules/developer-cli-odo-attributes.adoc[]
4+
include::modules/common-attributes.adoc[]
5+
:context: creating-a-java-application-with-a-database
6+
toc::[]
7+
8+
This example describes how to deploy a Java application by using devfile and connect it to a database service.
9+
10+
.Prerequisites
11+
12+
* A running cluster.
13+
* `{odo-title}` is installed.
14+
* A Service Binding Operator is installed in your cluster. To learn how to install Operators, contact your cluster administrator or see xref:../../../operators/user/olm-installing-operators-in-namespace.adoc#olm-installing-operators-from-operatorhub_olm-installing-operators-in-namespace[Installing Operators from OperatorHub].
15+
* A Dev4Devs PostgreSQL Operator Operator is installed in your cluster. To learn how to install Operators, contact your cluster administrator or see xref:../../../operators/user/olm-installing-operators-in-namespace.adoc#olm-installing-operators-from-operatorhub_olm-installing-operators-in-namespace[Installing Operators from OperatorHub].
16+
17+
include::modules/developer-cli-odo-creating-a-project.adoc[leveloffset=+1]
18+
19+
include::modules/developer-cli-odo-creating-a-database-with-odo.adoc[leveloffset=+1]
20+
21+
include::modules/developer-cli-odo-creating-a-java-microservice-jpa-application.adoc[leveloffset=+1]
22+
23+
include::modules/developer-cli-odo-connecting-a-java-application-to-mysql-database.adoc[leveloffset=+1]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Module included in the following assemblies:
2+
//
3+
// * cli_reference/developer_cli_odo/creating-a-java-application-with-a-database
4+
5+
[id="connecting-a-java-application-to-a-database_{context}"]
6+
= Connecting a Java application to a database
7+
8+
To connect your Java application to the database, use the `odo link` command.
9+
10+
.Procedure
11+
12+
. Display the list of services:
13+
+
14+
[source,terminal]
15+
----
16+
$ odo service list
17+
----
18+
+
19+
.Example output
20+
[source,terminal]
21+
----
22+
NAME AGE
23+
Database/sampledatabase 6m31s
24+
----
25+
26+
. Connect the database to your application:
27+
+
28+
[source,terminal]
29+
----
30+
$ odo link Database/sampledatabase
31+
----
32+
33+
. Push the changes to your cluster:
34+
+
35+
[source,terminal]
36+
----
37+
$ odo push
38+
----
39+
+
40+
After the link has been created and pushed, a secret that contains the database connection data is created.
41+
42+
. Check the component for values injected from the database service:
43+
+
44+
[source,sh]
45+
----
46+
$ odo exec -- bash -c 'export | grep DATABASE'
47+
declare -x DATABASE_CLUSTERIP="10.106.182.173"
48+
declare -x DATABASE_DB_NAME="sampledb"
49+
declare -x DATABASE_DB_PASSWORD="samplepwd"
50+
declare -x DATABASE_DB_USER="sampleuser"
51+
----
52+
53+
. Open the URL of your Java application and navigate to the `CreatePerson.xhtml` data entry page. Enter a username and age by using the the form. Click *Save*.
54+
+
55+
Note that now you can see the data in the database by clicking the *View Persons Record List* link.
56+
+
57+
You can also use a CLI tool such as `psql` to manipulate the database.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Module included in the following assemblies:
2+
3+
[id="creating-a-database-with-odo_{context}"]
4+
5+
= Creating a database with `odo`
6+
7+
To create a database, you must have an access to the database Operator. For this example, Dev4Devs PostgreSQL Operator is used.
8+
9+
.Procedure
10+
11+
12+
. View the list of the services in your project:
13+
+
14+
[source,terminal]
15+
----
16+
$ odo catalog list services
17+
----
18+
+
19+
.Example output
20+
----
21+
Operators available in the cluster
22+
NAME CRDs
23+
postgresql-operator.v0.1.1 Backup, Database
24+
----
25+
26+
. Store the YAML of the service in a file:
27+
+
28+
[source,terminal]
29+
----
30+
$ odo service create postgresql-operator.v0.1.1/Database --dry-run > db.yaml
31+
----
32+
33+
. Add the following values under the `metadata:` section in the `db.yaml` file:
34+
+
35+
[source,yaml]
36+
----
37+
name: sampledatabase
38+
annotations:
39+
service.binding/db.name: 'path={.spec.databaseName}'
40+
service.binding/db.password: 'path={.spec.databasePassword}'
41+
service.binding/db.user: 'path={.spec.databaseUser}'
42+
----
43+
+
44+
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.
45+
46+
. Change the following values under the `spec:` section of the YAML file:
47+
+
48+
[source,yaml]
49+
----
50+
databaseName: "sampledb"
51+
databasePassword: "samplepwd"
52+
databaseUser: "sampleuser"
53+
----
54+
55+
. Create a database from the YAML file:
56+
+
57+
[source,terminal]
58+
----
59+
$ odo service create --from-file db.yaml
60+
----
61+
+
62+
A database instance is now present in your project.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
= Creating a Java MicroServices JPA application
2+
3+
With `odo`, you can create and manage a sample Java MicroServices JPA application.
4+
5+
.Procedure
6+
7+
. Clone the sample application:
8+
+
9+
[source,terminal]
10+
----
11+
$ git clone https://github.com/redhat-developer/application-stack-samples
12+
----
13+
14+
. Navigate to the application directory:
15+
+
16+
[source,terminal]
17+
----
18+
$ cd ./application-stack-samples/jpa
19+
----
20+
21+
. Initialize the project:
22+
+
23+
[source,terminal]
24+
----
25+
$ odo create java-openliberty java-application
26+
----
27+
28+
. Push the application to the cluster:
29+
+
30+
[source,terminal]
31+
----
32+
$ odo push
33+
----
34+
+
35+
The application is now deployed to the cluster.
36+
37+
. View the status of the cluster by streaming the OpenShift logs to the terminal:
38+
+
39+
[source,terminal]
40+
----
41+
$ odo log
42+
----
43+
+
44+
Notice the test failures and `UnknownDatabaseHostException` error. This is because your application does not have a database yet:
45+
+
46+
[source,terminal]
47+
----
48+
[INFO] [err] java.net.UnknownHostException: ${DATABASE_CLUSTERIP}
49+
[INFO] [err] at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
50+
[INFO] [err] at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
51+
[INFO] [err] at java.base/java.net.Socket.connect(Socket.java:609)
52+
[INFO] [err] at org.postgresql.core.PGStream.<init>(PGStream.java:68)
53+
[INFO] [err] at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
54+
[INFO] [err] ... 86 more
55+
[ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0, Time elapsed: 0.706 s <<< FAILURE! - in org.example.app.it.DatabaseIT
56+
[ERROR] testGetAllPeople Time elapsed: 0.33 s <<< FAILURE!
57+
org.opentest4j.AssertionFailedError: Expected at least 2 people to be registered, but there were only: [] ==> expected: <true> but was: <false>
58+
at org.example.app.it.DatabaseIT.testGetAllPeople(DatabaseIT.java:57)
59+
60+
[ERROR] testGetPerson Time elapsed: 0.047 s <<< ERROR!
61+
java.lang.NullPointerException
62+
at org.example.app.it.DatabaseIT.testGetPerson(DatabaseIT.java:41)
63+
64+
[INFO]
65+
[INFO] Results:
66+
[INFO]
67+
[ERROR] Failures:
68+
[ERROR] DatabaseIT.testGetAllPeople:57 Expected at least 2 people to be registered, but there were only: [] ==> expected: <true> but was: <false>
69+
[ERROR] Errors:
70+
[ERROR] DatabaseIT.testGetPerson:41 NullPointer
71+
[INFO]
72+
[ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0
73+
[INFO]
74+
[ERROR] Integration tests failed: There are test failures.
75+
----
76+
77+
. Create an ingress URL to access the application:
78+
+
79+
[source,terminal]
80+
----
81+
$ odo url create --port 8080
82+
----
83+
84+
. Push the changes to your cluster:
85+
+
86+
[source,terminal]
87+
----
88+
$ odo push
89+
----
90+
91+
. Display the created URL:
92+
+
93+
[source,terminal]
94+
----
95+
$ odo url list
96+
----
97+
+
98+
.Example output
99+
[source,terminal]
100+
----
101+
Found the following URLs for component mysboproj
102+
NAME STATE URL PORT SECURE KIND
103+
java-application-8080 Pushed http://java-application-8080.apps-crc.testing 8080 false ingress
104+
----
105+
+
106+
The application is now deployed to the cluster and you can access it by using the URL that is created.
107+
108+
. Use the URL to navigate to the `CreatePerson.xhtml` data entry page and enter a username and age by using the the form. Click *Save*.
109+
+
110+
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.

0 commit comments

Comments
 (0)