Home » Java » Add new Datasource (mysql) wildfly

Add new Datasource (mysql) wildfly

Posted by: admin December 28, 2021 Leave a comment


I’m trying to add new datasource mysql jdbc driver to my wildfly server

I created folder wildfly.x.x.x/modules/system/layers/base/com/mysql/main
I’ve got here jdbc jar file and module.xml

<module xmlns="urn:jboss:module:1.3" name="com.mysql">
         <resource-root path="mysql-connector-java-5.1.34-bin.jar"/>
      <module name="javax.api"/>

then added dataresource code into standalone-full.xml (under datareources tag)

 <datasource jndi-name="java:jboss/datasources/MySQLDS" pool-name="MySQLDS"     enabled="true" use-java-context="true">

but when i go to wildfly control panel http://localhost:9990/console/
dataresource doesnt appear , what did i missed?

also i’m trying to add it manually from interface i’v got this error

Unexpected HTTP response: 500

    "address" => [
        ("subsystem" => "datasources"),
        ("data-source" => "mysql")
    "operation" => "test-connection-in-pool"


Internal Server Error
    "outcome" => "failed",
    "failure-description" => "JBAS010440: failed to invoke operation: JBAS010447: Connection is not valid",
    "rolled-back" => true

Did you add a driver definition? Your datasources subsystem should look something like this:

    <subsystem xmlns="urn:jboss:domain:datasources:2.0">
            <datasource jndi-name="java:/jdbc/myds" pool-name="myds" enabled="true" use-java-context="true">
                <driver name="h2" module="com.h2database.h2">
                <driver name="mysql" module="com.mysql">

The driver element in the data source definition must reference a driver element by name. The module attribute must match the name of your MySQL driver module.


Actually I meet the same problem (I could add the datasources and test connection successfully before) So I’m just confused and I find a way works for me:)

See my services and I find it stopped,and I started it then tried again,It works well again! Even though your service does not stop, maybe just restart it.
I have to say it may not work for you if you never success to connect before,good luck~

enter image description here

enter image description here


There are three ways using which you can simply create datasource into wildfly

  1. Using admin console
  2. Manually adding into standalone.xml
  3. Creating datasource file that is xml file.

Go to WildFly directory/standalone/deployments
Simplest way to create datasource xml with following content

<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
      <datasource jndi-name="APP_DS" pool-name="APP_DS" enabled="true" use-ccm="false">

        <!-- sql to call when connection is created -->
        <new-connection-sql>SELECT 1</new-connection-sql>



        <!-- sql to call on an existing pooled connection when it is obtained from pool -->
        <check-valid-connection-sql>SELECT 1</check-valid-connection-sql>



Also create the database that you will use. Sometimes the reason for this error is that the database is missing.