Home » Java » Add new Datasource (mysql) wildfly

Add new Datasource (mysql) wildfly

Posted by: admin December 28, 2021 Leave a comment

Questions:

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">
        <resources>
         <resource-root path="mysql-connector-java-5.1.34-bin.jar"/>
     </resources>
     <dependencies>
      <module name="javax.api"/>
     </dependencies>
    </module>

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">
 <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
 <driver>MySQLDriver</driver>
<security>
 <user-name>root</user-name>
 <password></password>
</security>
</datasource>

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

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

Response

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

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

    <subsystem xmlns="urn:jboss:domain:datasources:2.0">
        <datasources>
            <datasource jndi-name="java:/jdbc/myds" pool-name="myds" enabled="true" use-java-context="true">
                <connection-url>jdbc:mysql://localhost/mydb</connection-url>
                <driver>mysql</driver>
                <security>
                    <user-name>foo</user-name>
                    <password>bar</password>
                </security>
            </datasource>
            <drivers>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
                <driver name="mysql" module="com.mysql">
                    <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                </driver>
            </drivers>
        </datasources>
    </subsystem>

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">
        <connection-url>jdbc:mysql://localhost:3306/DB_NAME</connection-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <driver>mysql</driver>

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

        <pool>
            <min-pool-size>5</min-pool-size>
            <max-pool-size>50</max-pool-size>
        </pool>

        <security>
          <user-name>username</user-name>
          <password>password</password>
        </security>

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

        <timeout>
        <blocking-timeout-millis>300000</blocking-timeout-millis>
        <idle-timeout-minutes>5</idle-timeout-minutes>
        </timeout>
        <statement>
        <track-statements>true</track-statements>
        </statement>      
      </datasource>
    </datasources>

###

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