Skip to content

Commit 50562cf

Browse files
committed
add mysql datasource
1 parent 70cfe84 commit 50562cf

File tree

3 files changed

+169
-85
lines changed

3 files changed

+169
-85
lines changed

articles/app-service/configure-language-java-data-sources.md

Lines changed: 5 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -386,98 +386,18 @@ There are three core steps when [registering a data source with JBoss EAP](https
386386
1. Add the JDBC driver as a module.
387387
1. Add a data source with the module.
388388

389-
App Service is a stateless hosting service, so you must put these steps into a startup script and run it each time the JBoss container starts. Using PostgreSQL as an example:
389+
App Service is a stateless hosting service, so you must put these steps into a startup script and run it each time the JBoss container starts. Using PostgreSQL and MySQL as an examples:
390390

391-
1. Create an XML module definition file for the JDBC driver. The following example shows a module definition for PostgreSQL called *postgresql-module.xml*.
391+
# [PostgreSQL](#tab/jboss_postgresql)
392392

393-
```xml
394-
<?xml version="1.0" ?>
395-
<module xmlns="urn:jboss:module:1.1" name="org.postgresql">
396-
<resources>
397-
<resource-root path="postgresql-42.7.1.jar" />
398-
</resources>
399-
<dependencies>
400-
<module name="javax.api" />
401-
<module name="javax.transaction.api" />
402-
</dependencies>
403-
</module>
404-
```
405-
406-
1. 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`.
407-
408-
```bash
409-
#!/bin/bash
410-
# These commands are executed by the JBoss CLI.
411-
module add --name=org.postgresql --resources=/home/site/libs/postgresql-42.7.1.jar --module-xml=/home/site/scripts/postgresql-module.xml
412-
/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")
413-
data-source add --name="PostgresqlDS" --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=true
414-
```
415-
416-
Note that the `module add` command uses three environment variables (`DB_HOST`, `DB_USERNAME`, and `DB_PASSWORD`), which you must add in App Service as app settings.
417-
418-
1. Create a startup script, *startup.sh*, that calls the JBoss CLI commands. The following example shows how to call your `jboss-cli-commands.cli`. Later, you'll configure App Service to run this script when the container starts.
419-
420-
```bash
421-
$JBOSS_HOME/bin/jboss-cli.sh --connect --file=/home/site/scripts/jboss_cli_commands.cli --resolve-parameter-values
422-
```
393+
[!INCLUDE [configure-jboss-postgresql](includes/configure-language-java-data-sources/configure-jboss-postgresql.md)]
423394

424-
1. Using a deployment option of your choice, upload your JDBC driver, *postgresql-module.xml*, *jboss-cli-commands.cli*, and *startup.sh* to the paths specified in the respective scripts. Especially, upload *startup.sh* as a startup file. For example:
395+
# [MySQL](#tab/jboss_mysql)
425396

426-
# [Azure CLI](#tab/cli)
427-
428-
```azurecli-interactive
429-
export RESOURCE_GROUP_NAME=<resource-group-name>
430-
export APP_NAME=<app-name>
431-
432-
# The lib type uploads to /home/site/libs by default
433-
az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path postgresql-42.7.1.jar --target-path postgresql-42.7.1.jar --type lib
434-
az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path postgresql-module.xml --target-path /home/site/scripts/postgresql-module.xml --type static
435-
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
436-
# The startup type uploads to /home/site/scripts by default and sets it up to run on startup
437-
az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path startup.sh --type startup
438-
```
439-
440-
For more information, see [Deploy files to App Service](deploy-zip.md).
441-
442-
# [Azure Maven Plugin](#tab/maven)
443-
444-
```xml
445-
<deployment>
446-
<resources>
447-
<resource>
448-
<!-- The lib type uploads to /home/site/libs by default -->
449-
<type>lib</type>
450-
<directory>${project.basedir}/src/main/jboss/config</directory>
451-
<includes>
452-
<include>postgresql-42.7.1.jar</include>
453-
</includes>
454-
</resource>
455-
<resource>
456-
<!-- The script type uploads to /home/site/scripts by default -->
457-
<type>script</type>
458-
<directory>${project.basedir}/src/main/jboss/config</directory>
459-
<includes>
460-
<include>postgresql-module.xml</include>
461-
<include>jboss_cli_commands.cli</include>
462-
</includes>
463-
</resource>
464-
<resource>
465-
<!-- The startup type uploads to /home/site/scripts by default and sets it up to run on startup -->
466-
<type>startup</type>
467-
<directory>${project.basedir}/src/main/jboss/config</directory>
468-
<includes>
469-
<include>startup.sh</include>
470-
</includes>
471-
</resource>
472-
...
473-
</resources>
474-
</deployment>
475-
```
397+
[!INCLUDE [configure-jboss-mysql](includes/configure-language-java-data-sources/configure-jboss-mysql.md)]
476398

477399
---
478400

479-
To confirm that the datasource was added to the JBoss server, SSH into your webapp and run `$JBOSS_HOME/bin/jboss-cli.sh --connect`. Once you're connected to JBoss, run the `/subsystem=datasources:read-resource` to print a list of the data sources.
480-
481401
::: zone-end
482402

483403
## Next steps
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
author: cephalin
3+
ms.service: azure-app-service
4+
ms.devlang: java
5+
ms.topic: include
6+
ms.date: 11/05/2024
7+
ms.author: cephalin
8+
---
9+
10+
1. 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 MySQL data source with the JNDI name `java:jboss/datasources/mysqlDS`.
11+
12+
```bash
13+
#!/bin/bash
14+
module add --name=com.mysql.mysql-connector-j --resources=/home/site/libs/mysql-connector-j-9.1.0.jar
15+
/subsystem=datasources/jdbc-driver=mysql:add(driver-name="mysql",driver-module-name="com.mysql.mysql-connector-j",driver-class-name="com.mysql.cj.jdbc.Driver",driver-xa-datasource-class-name="com.mysql.cj.jdbc.MysqlXADataSource")
16+
data-source add --name=mysql --driver-name="mysql" --jndi-name="java:jboss/datasources/mysqlDS" --connection-url="jdbc:mysql://\${env.DB_HOST}:5432/mysql?serverTimezone=UTC" --user-name="\${env.DB_USERNAME}" --password="\${env.DB_PASSWORD}" --enabled=true --use-java-context=true
17+
```
18+
19+
Note that the `module add` command uses three environment variables (`DB_HOST`, `DB_USERNAME`, and `DB_PASSWORD`), which you must add in App Service as app settings. The script adds them without the `--resolve-parameter-values` flag so that JBoss doesn't save their values in plaintext.
20+
21+
1. Create a startup script, *startup.sh*, that calls the JBoss CLI commands. The following example shows how to call your `jboss-cli-commands.cli`. Later, you'll configure App Service to run this script when the container starts.
22+
23+
```bash
24+
$JBOSS_HOME/bin/jboss-cli.sh --connect --file=/home/site/scripts/jboss_cli_commands.cli
25+
```
26+
27+
1. Using 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. Especially, upload *startup.sh* as a startup file. For example:
28+
29+
# [Azure CLI](#tab/cli)
30+
31+
```azurecli-interactive
32+
export RESOURCE_GROUP_NAME=<resource-group-name>
33+
export APP_NAME=<app-name>
34+
35+
# The lib type uploads to /home/site/libs by default
36+
az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path mysql-connector-j-9.1.0.jar --target-path mysql-connector-j-9.1.0.jar --type lib
37+
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
38+
# The startup type uploads to /home/site/scripts by default and sets it up to run on startup
39+
az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path startup.sh --type startup
40+
```
41+
42+
For more information, see [Deploy files to App Service](deploy-zip.md).
43+
44+
# [Azure Maven Plugin](#tab/maven)
45+
46+
```xml
47+
<deployment>
48+
<resources>
49+
<resource>
50+
<!-- The lib type uploads to /home/site/libs by default. -->
51+
<type>lib</type>
52+
<directory>${project.build.directory}/${project.artifactId}/META-INF/lib</directory> <!-- Assume driver is part of POM dependencies. -->
53+
<includes>
54+
<include>mysql-connector-j-9.1.0.jar</include>
55+
</includes>
56+
</resource>
57+
<resource>
58+
<!-- The script type uploads to /home/site/scripts by default. -->
59+
<type>script</type>
60+
<directory>${project.scriptSourceDirectory}</directory> <!-- scriptSourceDirectory is src/main/scripts by default. -->
61+
<includes>
62+
<include>jboss_cli_commands.cli</include>
63+
</includes>
64+
</resource>
65+
<resource>
66+
<!-- The startup type uploads to /home/site/scripts/startup.sh by default -->
67+
<type>startup</type>
68+
<directory>${project.scriptSourceDirectory}</directory> <!-- scriptSourceDirectory is src/main/scripts by default. -->
69+
<includes>
70+
<include>startup.sh</include>
71+
</includes>
72+
</resource>
73+
...
74+
</resources>
75+
</deployment>
76+
```
77+
78+
---
79+
80+
To confirm that the datasource was added to the JBoss server, SSH into your webapp and run `$JBOSS_HOME/bin/jboss-cli.sh --connect`. Once you're connected to JBoss, run the `/subsystem=datasources:read-resource` to print a list of the data sources.
81+
82+
As defined by *jboss-cli-commands.cli* previously, you can access the MySQL connection using the JNDI name `java:jboss/datasources/mysqlDS`.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
author: cephalin
3+
ms.service: azure-app-service
4+
ms.devlang: java
5+
ms.topic: include
6+
ms.date: 11/05/2024
7+
ms.author: cephalin
8+
---
9+
10+
1. 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`.
11+
12+
```bash
13+
#!/bin/bash
14+
module add --name=org.postgresql --resources=/home/site/libs/postgresql-42.7.1.jar
15+
/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")
16+
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=true
17+
```
18+
19+
Note that the `module add` command uses three environment variables (`DB_HOST`, `DB_USERNAME`, and `DB_PASSWORD`), which you must add in App Service as app settings. The script adds them without the `--resolve-parameter-values` flag so that JBoss doesn't save their values in plaintext.
20+
21+
1. Create a startup script, *startup.sh*, that calls the JBoss CLI commands. The following example shows how to call your `jboss-cli-commands.cli`. Later, you'll configure App Service to run this script when the container starts.
22+
23+
```bash
24+
$JBOSS_HOME/bin/jboss-cli.sh --connect --file=/home/site/scripts/jboss_cli_commands.cli
25+
```
26+
27+
1. Using 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. Especially, upload *startup.sh* as a startup file. For example:
28+
29+
# [Azure CLI](#tab/cli)
30+
31+
```azurecli-interactive
32+
export RESOURCE_GROUP_NAME=<resource-group-name>
33+
export APP_NAME=<app-name>
34+
35+
# The lib type uploads to /home/site/libs by default
36+
az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path postgresql-42.7.1.jar --target-path postgresql-42.7.1.jar --type lib
37+
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
38+
# The startup type uploads to /home/site/scripts by default and sets it up to run on startup
39+
az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path startup.sh --type startup
40+
```
41+
42+
For more information, see [Deploy files to App Service](deploy-zip.md).
43+
44+
# [Azure Maven Plugin](#tab/maven)
45+
46+
```xml
47+
<deployment>
48+
<resources>
49+
<resource>
50+
<!-- The lib type uploads to /home/site/libs by default. -->
51+
<type>lib</type>
52+
<directory>${project.build.directory}/${project.artifactId}/META-INF/lib</directory> <!-- Assume driver is part of POM dependencies. -->
53+
<includes>
54+
<include>postgresql-42.7.1.jar</include>
55+
</includes>
56+
</resource>
57+
<resource>
58+
<!-- The script type uploads to /home/site/scripts by default. -->
59+
<type>script</type>
60+
<directory>${project.scriptSourceDirectory}</directory> <!-- scriptSourceDirectory is src/main/scripts by default. -->
61+
<includes>
62+
<include>jboss_cli_commands.cli</include>
63+
</includes>
64+
</resource>
65+
<resource>
66+
<!-- The startup type uploads to /home/site/scripts by default. -->
67+
<type>startup</type>
68+
<directory>${project.scriptSourceDirectory}</directory> <!-- scriptSourceDirectory is src/main/scripts by default. -->
69+
<includes>
70+
<include>startup.sh</include>
71+
</includes>
72+
</resource>
73+
...
74+
</resources>
75+
</deployment>
76+
```
77+
78+
---
79+
80+
To confirm that the datasource was added to the JBoss server, SSH into your webapp and run `$JBOSS_HOME/bin/jboss-cli.sh --connect`. Once you're connected to JBoss, run the `/subsystem=datasources:read-resource` to print a list of the data sources.
81+
82+
As defined by *jboss-cli-commands.cli* previously, you can access the PostgreSQL connection using the JNDI name `java:jboss/datasources/postgresDS`.

0 commit comments

Comments
 (0)