Adding SQL Server’s JDBC Driver in to Wildfly
Update: 7/26/2018 – Corrected path for JDBC driver.
In a previous blog post, I walked you through installing MySQL’s JDBC driver into Wildfly. This post will show you how to do the same thing for Microsoft SQL Server.
The first thing you’ll need is the jar file containing the JDBC driver. You can fetch it here. I would recommend downloading the tar.gz file. Once that’s done downloading, you’ll need to create the following folder:
Extract the appropriate JDBC jar file from within the archive and place it in that folder then create a module.xml file with the following content:
<module xmlns="urn:jboss:module:1.3" name="com.microsoft.sqlserver"> <resources> <resource-root path="mssql-jdbc-6.4.0.jre9.jar" /> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>
The path attribute for the resource-root node should contain the name of the JAR file you extracted. My system is using JRE 9, yours might be using something different.
Next, we need to tell Wildfly about the new JDBC driver. Open the configuration file you’re using (e.g. WILDFLY_HOME/standalone/configuration/standalone.xml), and navigate to the “<drivers>” section. You should see a reference to the H2 JDBC driver here. Add the following block after the “<driver>” entry for H2:
<driver name="sqlserver" module="com.microsoft.sqlserver"> <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class> <xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</xa-datasource-class> </driver>
After you’ve added that, bounce your server and you should see the following messages in the log file to let you know that the driver was loaded:
[org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 33) WFLYJCA0004: Deploying JDBC-compliant driver class com.microsoft.sqlserver.jdbc.SQLServerDriver (version 6.4) [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0018: Started Driver service with driver-name = sqlserver
That’s it! You should now be able to create a SQL Server datasource.
For convenience, if you don’t remember the JDBC url format for SQL Server, you can use this one:
Note, that if you want to use an XA datasource, there are a few more steps that you will need to do. The first step is to run the “xa_install.sql” script found within the JDBC driver download. This will have to be done on each instance of SQL Server that is part of your cluster. The final step is to enable XA transactions on each server. On Windows 10, you can do this by opening Component Services under Administrative Tools. Once that’s open, navigate to “Computers > My Computer > Distributed Transaction Coordinator”, right click on “Local DTC” and choose Properties. On the following dialog, click on the “Security” tab and ensure the “Enable XA Transactions” is selected.