Defining a JavaEE Datasource in Wildfly
Just about every application built using JavaEE will need a datasource. The easiest and, in my opinion, the best way to do it is to create it through Wildfly’s administration UI.
Before we can do that, we first need to add an administrator user. If your Wildfly server isn’t started yet, go ahead and start it. Once that is done, run the following script:
WILDFLY_HOME/bin/add-user.bat
This will open a console window that should look similar to this:

There’s already an “admin” user in the system, but it’s disabled. If you choose to use “admin” go ahead and re-enable it then give it your own password. Follow the prompts to enable the user. The final output should look similar to the following:

Now that the administrator user is created, log in to Wildfly’s admin console by opening http://localhost:9990 in your browser then entering the credentials you just created. Once you’re logged in, you should see something like this:

To add a new datasource, click on the “Configuration” tab, then the “Subsystems” menu option, then “Datasources”, and finally “Non-XA”. You should now see this:

There are two types of datasources: XA and Non-XA. XA datasources enable a “global” transaction manager that can manage multiple data resources in a single transaction. For instance, if you have customer information in two databases and need to ensure they’re in sync, you could create an XA datasource that updates both databases in the same transaction. For now, we’re going to create a Non-XA datasource. Go ahead and click “Add”.
You’ll now see a list of database types. Choose H2 since that is what comes with Wildfly.

On the next screen, you want to give it a name and a JNDI name. For now, we’ll use “TodoDS” as the name and “java:/TodoDS” and the JNDI name.

The next screen is the JDBC driver configuration. We’ll skip it since it is fine the way it is. The third screen is where we’ll configure our connection settings. The only thing we want to change is the “Connection URL”. Change it to:
jdbc:h2:mem:todo;DB_CLOSE_DELAY=-1
What this will do is create an in-memory database called “todo” that will be destroyed when the server shuts down and will be recreated when it starts up.

The final screen is where we can test out our datasource to make sure it’s set up correctly. Go ahead and click the “Test connection” button. Everything should work and you should see a success message:

Assuming everything worked, go ahead and click “OK”, then “Next” and finally “Finish”. You’ll see a message about needing to reload the server. Go ahead and do that.
Now, there’s one final thing we need to do, and that’s wire the newly created datasource into our JavaEE application. We do this by creating a file called “persistence.xml”. Go ahead and create it and place it in src/main/resources/META-INF. Paste the following into it:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="TodoPU"> <jta-data-source>java:/TodoDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
Note that the “jta-data-source” must match the JNDI name used when the datasource was created in Wildfly. The “hibernate.hbm2ddl.auto” property will cause our data schema to be created for us automatically as the application starts up.
That’s all that you need to do to create a JavaEE datasource in Wildfly and wire it into your application.
In the next post we’ll dive into creating our entities and EJB classes that will tie into the datasource we just created.
Source code is available here:
Hi. I watched your video on YouTube and it’s been really helpful. I was hoping you could help with something. How can I connect to this H2 database with the H2 console so I can see the data in the database.