Data Sources
Apps deployed to Payara Cloud, being Jakarta EE (Java EE) compatible, can connect to any database using the Jakarta EE datasource mechanism.
Payara Cloud looks for a persistence.xml file that uses the Jakarta EE default datasource java:comp/DefaultDataSource
.
If detected, it makes the necessary minimal configuration parameters for the default datasource available on the UI.
Configuring Datasources
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.
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 Cloud. You can then set values for those keys to be passed to the datasource definition parameters. |