Payara Cloud

Data Sources

Apps deployed to Payara Qube, being Jakarta EE (Java EE) compatible, can connect to any database using the Jakarta EE datasource mechanism.

For Jakarta EE/MicroProfile applications, Payara Qube looks for a persistence.xml file that uses the Jakarta EE default datasource java:comp/DefaultDataSource.

Spring Boot applications can connect to databases using Spring’s standard datasource configuration mechanisms.

If detected, it makes the necessary minimal configuration parameters for the default datasource available on the UI.

application 18 default datasource
Figure 1. Default Datasource Configuration

Configuring Datasources for Jakarta EE Applications

For Jakarta EE/MicroProfile applications, you can configure datasources using either the @DataSourceDefinition or through the <data-source> element in the web.xml file.

Using Annotation

You can configure database connection through the @DataSourceDefinition.

@DataSourceDefinition(name="java:global/jdbc/cloud-postgres",
        className="org.postgresql.ds.PGSimpleDataSource",
        serverName = "${MPCONFIG=ds_servername}",
        portNumber = 5432,
        databaseName = "${MPCONFIG=ds_databasename}",
        user="${MPCONFIG=ds_username}",
        password="${MPCONFIG=ds_password}"
)

Using the MicroProfile Config property MPCONFIG, you can externalise the various properties needed for the connection.

This allows you to set different values for different environments and namespaces in Payara Qube.

You can also use the @DataSourceDefinitions() to define different datasources to connect to different databases.

@DataSourceDefinitions(
      value = {
        @DataSourceDefinition(name="java:app/jdbc/cloud-postgres",
        className="org.postgresql.ds.PGSimpleDataSource",
        serverName = "${MPCONFIG=ds_servername}",
        portNumber = 5432,
        databaseName = "${MPCONFIG=ds_databasename}",
        user="${MPCONFIG=ds_username}",
        password="${MPCONFIG=ds_password}"
),
        @DataSourceDefinition(name = "java:comp/env/DS2",
           minPoolSize = 0,
           initialPoolSize = 0,
           className = "org.apache.derby.jdbc.ClientDataSource",
           portNumber = 1527,
           serverName = "localhost",
           user = "examples",
           password = "examples",
           databaseName = "examplesDB",
           properties={"create=true", "weblogic.TestTableName=SQL SELECT 1 FROM SYS.SYSTABLES"}
        )
      }
   )

Using Web Descriptor

You can also configure datasources in the web.xml descriptor.

<data-source>
    <name>java:global/jdbc/cloud-mysql</name>
    <class-name>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</class-name>
    <server-name>${MPCONFIG=ds_servername}</server-name>
    <port-number>3306</port-number>
    <database-name>${MPCONFIG=ds_databasename}</database-name>
    <user>${MPCONFIG=ds_username}</user>
    <password>${MPCONFIG=ds_password}</password>
</data-source>

Similar to the annotation, you can externalize connection properties through the MicroProfile Config property MPCONFIG property.

You must define the MicroProfile configuration keys passed to MPCONFIG manually in Payara Qube. You can then set values for those keys to be passed to the datasource definition parameters.

Spring Boot Database Configuration

For Spring Boot applications deployed to Payara Qube, database connections are configured using standard Spring Boot properties in your application.properties or application.yml file.

Basic Configuration

# Database connection properties
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.Driver

# Connection pool settings
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=20000

Using Environment Variables

You can externalize database configuration using environment variables:

spring.datasource.url=${DATABASE_URL:jdbc:h2:mem:testdb}
spring.datasource.username=${DATABASE_USERNAME:sa}
spring.datasource.password=${DATABASE_PASSWORD:}

Multiple Datasources

For applications requiring multiple database connections:

# Primary datasource
spring.datasource.primary.url=jdbc:postgresql://localhost:5432/primary_db
spring.datasource.primary.username=primary_user
spring.datasource.primary.password=primary_password

# Secondary datasource
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/secondary_db
spring.datasource.secondary.username=secondary_user
spring.datasource.secondary.password=secondary_password

These properties can be configured through the Payara Qube UI when deploying your Spring Boot application.

Back to Top