trlogic Silver Logo

How to Add Persistence to Project via Openshift MySQL

trlogic Silver Logo

OpenShift container platform is a cloud application deployment and hosting platform. Most of the customers have been using OpenShift to run stateless applications. However the real challenge for OpenShift is applications that run stateful. To exceed that challenge, OpenShift provides storage solutions. One of these solutions includes a container image for running MySQL. This image can provide database services based on username, password and database name. These settings can be reachable via configuration.

In this article, creating a database and connecting that database with an application will be shown. First of all, to create a mysql database, click mysql on openshift web console.

Openshift MySQL
OpenShift MySQL

In information part, there is nothing to change or create. Therefore click next. On this page, the essential part is the database service name, mysql connection username, mysql connection password mysql root user password, mysql database name. These parts can be empty. If they are empty, the system is automatically generated them as environment values which can be seen at the pod terminal with shell command “printenv”.

Image for postAfter configuration
After configuration

To create a connection between the database and the application, there are some codes that have to be written in application properties file in the spring boot project. These are shown as follows.

Database connection
Database connection

In first line, spring boot choose a default value to based on whether that thinks the database is embedded. This defaults to create-drop if no schema manager has been detected or non in all other cases.

In second line, the URL has to be specified. Otherwise, spring boot tries to auto-configure an embedded database.

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

this code helps to run quiers for mysql.

To dump queries to standard out, add the following

spring.jpa.show-sql=true

To deploy the application on OpenShift, open Red Hat OpenJDK 8 and click next.

Image for post
Red Hat OpenJDK 8

Give name that project. Then give the address of git repository that have the application. After that, press create button and open the project overview.

Project overview
Project overview

As can bee seen from the figure, the application is added and the route is ready.

App is working
App is working