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).
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.
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:
<datasource jta="true" jndi-name="java:jboss/datasources/MyApplicationDS" pool-name="MyApplicationDS" enabled="true" use-ccm="true" use-java-context="true">
<check-valid-connection-sql>/* ping */ SELECT 1 from dual</check-valid-connection-sql>
The interesting part is the driver tag:
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:
[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.