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 app in App Service.
Azure App Service runs Java web applications in two 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.
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.
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.