Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows how to configure data sources in a Java SE, Tomcat, or JBoss app in App Service.
Azure App Service runs Java web applications in three types on a fully managed service:
- Java Standard Edition (SE). Java SE can run an app deployed as a Java archive (JAR) package that contains an embedded server, such as Spring Boot, Quarkus, Dropwizard, or an app with an embedded Tomcat or Jetty server.
- Tomcat. The built-in Tomcat server can run an app deployed as a web application archive (WAR) package.
- JBoss Enterprise Application Platform (EAP): The built-in JBoss EAP server can run an app deployed as a WAR or enterprise archive (EAR) package. This option is supported for Linux apps in a set of pricing tiers that include Free, Premium v3, and Isolated v2.
Note
JBoss EAP on App Service now supports Bring Your Own License (BYOL) billing. BYOL enables customers who have existing Red Hat subscriptions to apply those licenses directly to their JBoss EAP deployments on Azure App Service. For more information, see BYOL Support for JBoss EAP on App Service.
Configure the data source
To connect to data sources in Spring Boot applications, we suggest creating connection strings and injecting them into your application.properties file.
In the left pane of the App Service page, select Settings > Environment variables. On the Connection strings tab, select Add. Set a Name for the string, paste your JDBC connection string into the Value field, and set the Type to Custom. You can optionally set the connection string as a slot setting.
The connection string is accessible to your application as an environment variable named
CUSTOMCONNSTR_<your-string-name>. For example,CUSTOMCONNSTR_exampledb.In your application.properties file, reference the connection string with the environment variable name. For the preceding example, you would use this code:
app.datasource.url=${CUSTOMCONNSTR_exampledb}
For more information, see the Spring Boot documentation on data access and externalized configuration.
Tip
Linux Tomcat containers can automatically configure shared data sources in the Tomcat server if you set the environment variable WEBSITE_AUTOCONFIGURE_DATABASE to true. The only thing for you to do is add an app setting that contains a valid JDBC connection string to an Oracle, SQL Server, PostgreSQL, or MySQL database (including the connection credentials). App Service automatically adds the corresponding shared database to /usr/local/tomcat/conf/context.xml, using an appropriate driver that's available in the container. For an end-to-end scenario that uses this approach, see Tutorial: Build a Tomcat web app with Azure App Service on Linux and MySQL.
These instructions apply to all database connections. You need to replace placeholders with your chosen database's driver class name and JAR file. The following table provides class names and driver downloads for common databases.
| Database | Driver class name | JDBC driver |
|---|---|---|
| PostgreSQL | org.postgresql.Driver |
Download |
| MySQL | com.mysql.jdbc.Driver |
Download (Select Platform Independent.) |
| SQL Server | com.microsoft.sqlserver.jdbc.SQLServerDriver |
Download |
To configure Tomcat to use Java Database Connectivity (JDBC) or the Java Persistence API (JPA), first customize the CATALINA_OPTS environment variable that's read in by Tomcat at startup. Set this value by using an app setting in the App Service Maven plugin:
<appSettings>
<property>
<name>CATALINA_OPTS</name>
<value>"$CATALINA_OPTS -Ddbuser=${DBUSER} -Ddbpassword=${DBPASSWORD} -DconnURL=${CONNURL}"</value>
</property>
</appSettings>
Or set the environment variable on the App settings tab of the Settings > Environment variables page in the Azure portal.
Next, determine whether the data source should be available to one application or to all applications running on the Tomcat servlet.
Application-level data sources
To configure an application-level data source:
Create a context.xml file in the META-INF/ directory of your project. Create the META-INF/ directory if it doesn't exist.
In context.xml, add a
Contextelement to link the data source to a JNDI address. Replace thedriverClassNameplaceholder with your driver's class name from the table that appears earlier in this article.<Context> <Resource name="jdbc/dbconnection" type="javax.sql.DataSource" url="${connURL}" driverClassName="<insert your driver class name>" username="${dbuser}" password="${dbpassword}" /> </Context>Update your application's web.xml to use the data source in your application.
<resource-env-ref> <resource-env-ref-name>jdbc/dbconnection</resource-env-ref-name> <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type> </resource-env-ref>
Shared server-level resources
Tip
Linux Tomcat containers can automatically apply XSLT files by using the following convention for files copied to /home/site/wwwroot: If server.xml.xsl or server.xml.xslt is present, the files are applied to Tomcat's server.xml. If context.xml.xsl or context.xml.xslt is present, the files are applied to Tomcat's context.xml.
Adding a shared, server-level data source requires you to edit Tomcat's server.xml. Because file changes outside of the /home directory are ephemeral, changes to Tomcat's configuration files need to be applied programatically, as follows:
- Upload a startup script and set the path to the script in Settings > Configuration. On the Stack settings tab, add the path in the Startup command box. You can upload the startup script by using FTP.
Your startup script makes an XSL transform to the server.xml file and outputs the resulting XML file to /usr/local/tomcat/conf/server.xml. The startup script should install libxslt or xlstproc, depending on the distribution of the version of Tomcat of your web app, as noted in the comment in the following example script. You can use FTP to upload your XSL file and startup script.
# Install the libxslt package on Alpine-based images:
apk add --update libxslt
# Install the xsltproc package on Debian or Ubuntu-based images:
apt install xsltproc
# Also copy the transform file to /home/tomcat/conf/
# Usage: xsltproc --output output.xml style.xsl input.xml
xsltproc --output /home/tomcat/conf/server.xml /home/tomcat/conf/transform.xsl /usr/local/tomcat/conf/server.xml
The following example XSL file adds a new connector node to the Tomcat server.xml.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()" name="Copy">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@* | node()" mode="insertConnector">
<xsl:call-template name="Copy" />
</xsl:template>
<xsl:template match="comment()[not(../Connector[@scheme = 'https']) and
contains(., '<Connector') and
(contains(., 'scheme="https"') or
contains(., "scheme='https'"))]">
<xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>
<xsl:template match="Service[not(Connector[@scheme = 'https'] or
comment()[contains(., '<Connector') and
(contains(., 'scheme="https"') or
contains(., "scheme='https'"))]
)]
">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="insertConnector" />
</xsl:copy>
</xsl:template>
<!-- Add the new connector after the last existing connector if there is one -->
<xsl:template match="Connector[last()]" mode="insertConnector">
<xsl:call-template name="Copy" />
<xsl:call-template name="AddConnector" />
</xsl:template>
<!-- ... or before the first engine if there's no existing connector -->
<xsl:template match="Engine[1][not(preceding-sibling::Connector)]"
mode="insertConnector">
<xsl:call-template name="AddConnector" />
<xsl:call-template name="Copy" />
</xsl:template>
<xsl:template name="AddConnector">
<!-- Add new line -->
<xsl:text>
</xsl:text>
<!-- This is the new connector -->
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="${{user.home}}/.keystore" keystorePass="changeit"
clientAuth="false" sslProtocol="TLS" />
</xsl:template>
</xsl:stylesheet>
Finalize configuration
Finally, place the driver JARs in the Tomcat classpath and restart your App Service app.
- Ensure that the JDBC driver files are available to the Tomcat classloader by placing them in the /home/site/lib directory. In the Cloud Shell, run
az webapp deploy --type=libfor each driver JAR:
az webapp deploy --resource-group <group-name> --name <app-name> --src-path <jar-name>.jar --type=lib --path <jar-name>.jar
If you created a server-level data source, restart the App Service Linux application. Tomcat resets CATALINA_BASE to /home/tomcat and uses the updated configuration.
Tip
By default, Linux JBoss containers can automatically configure shared data sources in the JBoss server. The only thing you need to do is add an app setting that contains a valid JDBC connection string to an Oracle, SQL Server, PostgreSQL, or MySQL database (including the connection credentials), and add the app setting / environment variable WEBSITE_AUTOCONFIGURE_DATABASE with the value true. JDBC connections created with service connector are also supported. App Service automatically adds the corresponding shared data source (based on the app setting name and the suffix _DS), using an appropriate driver available in the container. For an end-to-end scenario that uses this approach, see Tutorial: Build a JBoss web app with Azure App Service on Linux and MySQL.
There are three main steps to register a data source with JBoss EAP:
- Upload the JDBC driver.
- Add the JDBC driver as a module.
- Add a data source with the module.
App Service is a stateless hosting service, so you need to put these steps into a startup script and run it each time the JBoss container starts. Here are PostgreSQL, MySQL, and Azure SQL Database examples:
Note
JBoss EAP on App Service supports Bring Your Own License (BYOL) billing. BYOL enables customers who have existing Red Hat subscriptions to apply those licenses directly to their JBoss EAP deployments on Azure App Service. For more information, see BYOL Support for JBoss EAP.
Put your JBoss CLI commands into a file named jboss-cli-commands.cli. The JBoss commands must add the module and register it as a data source. The following example shows the JBoss CLI commands for creating a PostgreSQL data source with the JNDI name
java:jboss/datasources/postgresDS.module add --name=org.postgresql --resources=/home/site/libs/postgresql-42.7.4.jar /subsystem=datasources/jdbc-driver=postgresql:add(driver-name="postgresql",driver-module-name="org.postgresql",driver-class-name="org.postgresql.Driver",driver-xa-datasource-class-name="org.postgresql.xa.PGXADataSource") data-source add --name=postgresql --driver-name="postgresql" --jndi-name="java:jboss/datasources/postgresDS" --connection-url="jdbc:postgresql://\${env.DB_HOST}:5432/postgres" --user-name="\${env.DB_USERNAME}" --password="\${env.DB_PASSWORD}" --enabled=true --use-java-context=trueNote that the
module addcommand uses three environment variables (DB_HOST,DB_USERNAME, andDB_PASSWORD), which you must add in App Service as app settings. The script adds them without the--resolve-parameter-valuesflag so that JBoss doesn't save their values in plaintext.Create a startup script, startup.sh, that calls the JBoss CLI commands. The following example shows how to call your jboss-cli-commands.cli file. Later, you configure App Service to run this script when the container starts.
$JBOSS_HOME/bin/jboss-cli.sh --connect --file=/home/site/scripts/jboss-cli-commands.cliUsing a deployment option of your choice, upload your JDBC driver, jboss-cli-commands.cli, and startup.sh to the paths specified in the respective scripts. Upload startup.sh as a startup file. For example:
export RESOURCE_GROUP_NAME=<resource-group-name> export APP_NAME=<app-name> # The lib type uploads to /home/site/libs by default. az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path postgresql-42.7.4.jar --target-path postgresql-42.7.4.jar --type lib az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path jboss-cli-commands.cli --target-path /home/site/scripts/jboss-cli-commands.cli --type static # The startup type uploads to /home/site/scripts/startup.sh by default. az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path startup.sh --type startupFor more information, see Deploy files to App Service.
To confirm that the data source is added to the JBoss server, SSH into your web app and run $JBOSS_HOME/bin/jboss-cli.sh --connect. After you're connected to JBoss, run /subsystem=datasources:read-resource to print a list of the data sources.
Per the definition in jboss-cli-commands.cli, you can access the PostgreSQL connection using the JNDI name java:jboss/datasources/postgresDS.