Hi, DJ Spiess here at DeegeU.com.
If you're writing a JEE application in Java, you're going to need a datasource.
There aren't too many applications I can think without a database of some type.
Configuring a datasource in Wildfly is very easy.
In fact there are several ways to do it.
Each have their pros and cons, and that's what we'll look at in this video.
3 ways to add a datasource to Wildfly coming up next!
So in this lesson, the goal is to go from a fresh install with a user created to a configured
datasource in Wildfly.
We'll look at adding it in the management console first.
Then we'll add a datasource to Wildfly by modifying the standalone.xml file.
We'll do the same thing again, but add it from the command line interface.
When we're done, we'll look at the benefits of each method.
If you don't have Wildfly installed on your machine, you might want to look at the "4
steps to get you started with Wildfly download lesson first.
In this video, I'm using WildFly 9.0.1 Final.
Startup Wildfly if you haven't already.
For this video, we're going to use Postgres, but the steps work for any database.
I'm also assuming your database is installed, up and running, and has a database created
in it.
I called mine "demo1".
In all three methods you're really doing the same two steps.
The first step is to load the JDBC driver into Wildfly.
The second step is to configure your datasource.
Usually if you Google the name of your database and add the words "JDBC driver", that will
take you to the download page.
You'll also need the JDBC connection string for your database server.
That looks something like this.
OK, let's dive in!
First lets do this from the UI.
The first thing you need to do is open the console app in your browser.
Here's the address if you forgot how to get there.
If you're connecting to a server that isn't local, you'll need change the URI to match
your server.
You'll come to a page that looks like this.
Click on "> Create a deployment".
To the left, you'll have all the current deployments.
We're starting from a clean Wildfly installation here, so mine is empty.
Click "Add".
This brings up a wizard to walk through.
Select "Upload a new deployment", then "Next".
Click "Choose File" and navigate to the location on your computer where you stored your JDBC
driver.
Once you've selected your JDBC driver, click "Next".
On the final page, you can update the name if you like.
I usually change it to be the database name and version.
In this case, "Postgresql".
I'll leave the runtime name as is.
Basically the name is how the deployment is known to your users.
It must be unique.
The runtime name is how it's known to the server.
This way you can have multiple names, but they can point to the same runtime name.
Or not.
Depends on how you want your environment set up.
Make sure you check "Enable" and then press finish.
Your JDBC driver is now available.
Click back on "Home".
The next task is to create the datasource, so click on "> Datasources".
This will bring up another wizard.
Select the datasource type.
In this case, I'm selecting "PostgreSQL Datasource".
Click "Next".
On the Datasource Attributes page, give the datasource a name and JNDI name.
This is how your application is going to see your datasource.
So the name and JNDI name must be unique.
Make sure you keep the java:/ part in your JNDI name.
Click "Next".
When it asks you to select a driver, click the "Detected Driver" tab.
That will give you a list of JDBC drivers installed in Wildfly.
You should see the driver you just installed.
Click that one, and then click "Next".
Update the connection URI to match your server.
You'll also want to update the username and password to match you credentials.
Click "Test Connection".
If it fails, the error will have a "Details" button to tell you why it failed.
Otherwise it will say success, and you have a added a datasource to Wildfly.
Behind the scenes the management console is updating the standalone.xml file.
If you're not familiar with Wildfly, the standalone.xml is the configuration script for your server.
It contains configuration for everything you want to run in your server.
Let's manually do everything we did in the first segment of this video.
So we need to add the JDBC driver first.
Navigate to the /modules/system/layers/base directory in your Wildfly directory.
Create subfolders /org/postgresql/main The subfolders need to match the package structure
of your JDBC driver.
So if you're using an Oracle database, you'd want to create /com/oracle/main.
Copy your JDBC driver into the directory you created.
Next you need to create a XML file in the directory called module.xml.
The XML file looks like this.
Make sure the resource-root element path matches the exact name of your JDBC driver.
Next we need to edit the standalone.xml file in the /wildfly-9.0.0.CR2/standalone directory.
Look for the drivers element.
It should be under the datasources element.
We need to add the driver here with the following XML snippet.
This adds the driver to Wildfly.
Now we need to configure the datasource.
Just above in the datasources element, add this XML snippet.
Restart Wildfly, and you should be good to go.
You can verify your connections in the management console.
The final way to show you is to add your datasource via the command line interface.
It's often abbreviated as the CLI.
The CLI can be a whole series of videos on it's own, but basically the CLI is a special
command line interface to communicate with the Wildfly application server.
It has two advantages.
You can automate your interaction with the Wildfly server through a script, and you can
run this on a running Wildfly server.
Again the process is broken into two steps.
We first start the command line interface with the following command.
Then we install the JDBC jar.
We do this by loading the jar by adding a module.
This is the command to do that.
Then we add the driver to the datasources subsystem.
We do that with this command.
Finally we create the datasource that uses the JDBC driver we just installed.
And that should be it!
Again we can test it by going into the management console for Wildfly.
So how do these three methods compare?
Well the first method is the easiest.
There's no coding, and it just requires a few clicks.
The downside is you need access to the management console.
Depending on how dev-ops operates at your company, this might be a non-starter.
You don't really want people logging into your system to make changes.
It's a security issue.
The second method is good, because you can provide the XML as part of your deployment.
If you have multiple applications running on the server, that might not be the best
idea.
Different groups might write over each other's changes in the standalone.xml.
So it creates a versioning issue for the operations team.
The other problem is you must reboot the server to add the datasource.
The third option really is the best from a dev-ops perspective.
You can provide a script that is run when you install your application.
This script can be part of your standard deployment, and you can create a script that starts from
a fresh installation.
This plays well with technologies like Docker.
So that's the three ways you can install a datasource in Wildfly.
You can add it through the Wildfly management console, add it to the standalone.xml file,
or you can add it through the CLI.
You should now have a datasource you can use from your JEE applications!
See you in the next video!
Hey!
Thanks for watching the video.
There is a quick quiz for this on DeegeU.com if you’d like to gauge how much you learned.
If you like the videos you are seeing please let me know by liking the video and hitting
the subscribe button for the DeegeU channel on YouTube.
I’d really appreciate that.
If you have concerns or questions please leave them in the comments below or on DeegeU.com.
There’s a poll on the front page of DeegeU.com so you can let me know what topic is covered
next.
Thanks for watching and see you in the next video!