Configuring Mariadb datasources in wildfly

While i had been using glassfish/payara for most of my projects, i decided to switch to Wildfly for a project in which i preferred to have ActiveMQ als a JMS provider.

Using the JBOSS_CLI to create the JMS configurations was quite easy, but i did have a small challenge in figuring out how to create the mariadb driver/(xa)datasource.

This is what i ended up using:

1. Configure-buildTime.sh – a script that i run during docker build

# Download the maria-db driver
curl https://downloads.mariadb.com/Connectors/java/connector-java-2.7.2/mariadb-java-client-2.7.2.jar > /opt/jboss/mariadb.driver.jar
# Execute the client script that will register the driver & datasources
$JBOSS_CLI --file=/opt/jboss/wildfly/jdbc.command.cli

2. jdbc.command.cli – the JBOSS_CLI script that registers the driver and datasources.

connect
batch

module add \
 --name=org.mariadb \
 --dependencies=javax.api,javax.transaction.api \
 --resources=/opt/jboss/mariadb.driver.jar \

/subsystem=datasources/jdbc-driver=mariadb:add(driver-name="mariadb", driver-module-name="org.mariadb", driver-class-name="org.mariadb.jdbc.Driver", driver-xa-datasource-class-name="org.mariadb.jdbc.MariaDbDataSource")

data-source add \
 --name=MyDatasource \
 --jndi-name=java:/jdbc/MyDatasource \
 --driver-name=mariadb \
 --connection-url=jdbc:mysql://${env.RDS_HOSTNAME:${env.RDS_HOSTNAME}}:${env.RDS_PORT:${env.RDS_PORT}}/${env.RDS_DB_NAME:${env.RDS_DB_NAME}} \
 --user-name=${env.RDS_USERNAME:${env.RDS_USERNAME}} \
 --password=${env.RDS_PASSWORD:${env.RDS_PASSWORD}} \
 --check-valid-connection-sql="/* ping */ SELECT 1"

xa-data-source add --name=MyDatasourceXA \
 --driver-name=mariadb \
 --jndi-name=java:/jdbc/MyDatasourceXA \
 --user-name=${env.RDS_USERNAME:${env.RDS_USERNAME}} \
 --password=${env.RDS_PASSWORD:${env.RDS_PASSWORD}} \
 --recovery-username=${env.RDS_USERNAME:${env.RDS_USERNAME}} \
 --recovery-password=${env.RDS_PASSWORD:${env.RDS_PASSWORD}} \
 --xa-datasource-properties=Url=jdbc:mysql://${env.RDS_HOSTNAME:${env.RDS_HOSTNAME}}:${env.RDS_PORT:${env.RDS_PORT}}/${env.RDS_DB_NAME:${env.RDS_DB_NAME}} \ 
 --xa-datasource-class=org.mariadb.jdbc.MariaDbDataSource\
 --check-valid-connection-sql="/* ping */ SELECT 1"

run-batch

exit

Notice how i give my parameters a default value recursing to the parameter itself. This is done because the JBOSS_CLI apparantly tries to expand the values (although default configured not to do so version 21.0.1), and i could not find another way to prevent this – it feels like a bug. My goal was to have the parameters to be resolved at image-runtime – and that worked!

3. My war/META-INF/persistence.xml

..
<jta-data-source>jdbc/MyDatasourceXA</jta-data-source>
..

Leave a Reply

Your email address will not be published. Required fields are marked *