This article describes how to bundle a database driver and the datasource configuration into an enterprise archive (EAR). In my case I used WildFly 15 and MariaDB JDBC Client as the database driver for MySQL. The Java MySQL JDBC driver is under the GPL license and cannot be bundled into the application (at least for me).
Disclaimer
Since I am working in a container environment, I specifically do NOT want to deploy the database driver by using the WildFly management console or by putting files under modules/system/layers etc.. I want to use a vanilla WildFly instance without having to copy files into it and without the need to configure the datasource in the standalone.xml.
If this is what you want, please look somewhere else, there are a lot of other tutorials for this.
Adding the Database Driver to the EAR file
First you simply have to add the appropriate database driver to your ear file, by putting it into your pom.xml.
1 2 3 4 5 |
<dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> |
Since this jar file contains a file under /META-INF/services/java.sql.Driver it is automatically recognized and registered by WildFly as a database driver.
The tricky part was how to specify this driver within the datasource.
Adding a Datasource File
A new datasource can be added by placing a file ending on “-ds.xml” on the classpath. There are sooo many tutorials out there that tell you how you should specify your datasource, but most of them do no work at all. Some of them also mention that these “-ds.xml” files are deprecated, but these posts are also years old….
The way I am describing here, is the only one I could get to work after hours of trying different tutorials.
I just put a myapplication-ds.xml on the classpath with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<datasources> <datasource jta="true" jndi-name="java:jboss/datasources/MyApplicationDS" pool-name="MyApplicationDS" enabled="true" use-ccm="true" use-java-context="true"> <connection-url>jdbc:mariadb://localhost/mydatabase?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8&useSSL=false&connectTimeout=1000&allowMasterDownConnections=true&maxReconnects=2</connection-url> <driver> MyApplication.ear_org.mariadb.jdbc.Driver_2_3 </driver> <transaction-isolation>TRANSACTION_REPEATABLE_READ</transaction-isolation> <pool> <min-pool-size>10</min-pool-size> <max-pool-size>100</max-pool-size> <prefill>true</prefill> </pool> <security> <user-name>username</user-name> <password>password</password> </security> <validation> <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/> <check-valid-connection-sql>/* ping */ SELECT 1 from dual</check-valid-connection-sql> <background-validation>true</background-validation> <use-fast-fail>true</use-fast-fail> <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/> </validation> <statement> <prepared-statement-cache-size>64</prepared-statement-cache-size> <share-prepared-statements>true</share-prepared-statements> </statement> </datasource> </datasources> |
The interesting part is the driver tag:
1 2 3 |
<driver> MyApplication.ear_org.mariadb.jdbc.Driver_2_3 </driver> |
The driver name has to be exactly specified this way. The “name of your ear file”_org.mariadb.jdbc.Driver_2_3. Obviously 2.3 is the version number of the MariaDB driver.
I figured this out by taking a close look at the WildFly console output while starting the application. After you put the database driver into the ear file, you should see some output like this:
1 |
[org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) WFLYJCA0018: Started Driver service with driver-name = MyApplication.ear_org.mariadb.jdbc.Driver_2_3 |
This is where you get this from…
After doing so, you should be able to use your datasource as expected.
Hope this little tutorial saves you some time. All the outdated tutorials out there almost drove me crazy 🙂
If you have any questions or comments, please leave a message below.
Thank you. Thank you. THANK YOU.
THANK YOU.
I name thee the Data Source Saviour.
Thank you.
Thanks