Adding MySQL’s JDBC Driver To Our WAR File for Wildfly – JavaEE Series 2 Part 1
A while ago, I created a series of articles that created a simple “todo” JavaEE service. I’d like to revisit and expand upon that. To kick things off, we’re going to follow a previous article I wrote and add our JDBC drivers and data source configuration to our war file that we’ll deploy to Wildfly. This will allow us to seamlessly upgrade versions of Wildfly without having to install JDBC drivers every time we upgrade.
If you don’t have it already, clone the original JavaEE series GitHub project.
git clone email@example.com:synaptik-labs/javaee-series.git
Next, we need to add MySQL’s JDBC driver to our list of dependencies. In your pom.xml file add the following:
<dependencies> ... <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> ... </dependencies>
We can also eliminate the entire “plugins” block within the “build” node, as it isn’t needed anymore. The “build” block should now look like this:
<build> <finalName>todo</finalName> </build>
Just below that, let’s add some new properties:
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <failOnMissingWebXml>false</failOnMissingWebXml> </properties>
Another change I’d like to make is to the existing persistence.xml file. Inside of it there is a “properties” node with one entry. Remove that entry so the properties node has nothing inside:
I’d rather not have Hibernate try to update or create any tables for us. We’ll revisit the idea of schema management in a future article soon.
Now for the last code change, let’s add a new folder structure under “main”, “webapp/WEB-INF”. So your folder structure should now look like:
/src/main /java ... /resources /META-INF /webapp /WEB-INF
We now need to create our data source configuration file inside of the newly created WEB-INF folder. Create a new file that ends with “-ds.xml” (example: “my-ds.xml”). Wildfly will look for filenames with this format and process them. Inside this file, you’ll need to add the following:
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"> <datasource jndi-name="java:/TodoDS" pool-name="todoDS"> <driver>todo.war_com.mysql.cj.jdbc.Driver_8_0</driver> <connection-url>jdbc:mysql://10.0.0.128:3306/todo?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC</connection-url> <security> <user-name>todoadmin</user-name> <password>todopassword</password> </security> </datasource> </datasources>
You will want to update the connection-url, user-name, and password fields to match your local installation. If you’re curious about anything else, please refer to my previous article about this data source file which goes into more detail.
Build it and test it out
At this point we should be able to package this project and deploy it to Wildfly:
mvn clean package copy /Y target\todo.war
Now we can test it out using our AngularJS project we wrote a while ago.
NOTE: I also had to change Todo.java to have the following line in place of the @GeneratedValue annotation:
@GeneratedValue(strategy = GenerationType.IDENTITY)
Not every project will want to define their data source or deploy their own JDBC drivers, but in some cases, it might make sense. This feature has been marked as deprecated within JBoss / Wildfly for quite a while now, but it still functions as of today with Wildfly 15.0.1.
If you run into any problems or have any comments, please leave them in the comment section below.
Source code to the entire series is available here: