How to setup MariaDB JNDI on Wildfly 10?

2019-05-14 10:04发布

问题:

I'm trying to move my Tomcat web service to Wildfly 10. I thought it should be drag and drop into Wildfly's deployment folder and everything should be fine, but it seems not like that.

This is my MariaDB datasource definition in standalone.xml:

<datasource jndi-name="java:jboss/MariaDBDS" pool-name="MariaDBDS">
    <connection-url>jdbc:mariadb://db_ip/db_name</connection-url>
    <driver>mariadb</driver>
    <security>
        <user-name>dbAcc</user-name>
        <password>dbPassword</password>
    </security>
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
        <validate-on-match>true</validate-on-match>
        <background-validation>false</background-validation>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
    </validation>
    </datasource>
    <drivers>
        <driver name="mariadb" module="org.mariadb">
            <driver-class>org.mariadb.jdbc.Driver</driver-class>
            <xa-datasource-class>org.mariadb.jdbc.MariaDbDataSource</xa-datasource-class>
        </driver>
    </drivers>

then I put mariadb-java-client-1.4.6.jar and my WAR file into wildfly/standalone/deployments, start the Wildfly, and error messages came into my eyes:

07:05:21,137 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 60) WFLYUT0021: Registered web context: /MySV
07:05:21,143 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([
("subsystem" => "datasources"),
("data-source" => "MariaDBDS")
]) - failure description: {
"WFLYCTL0412: Required services that are not installed:" => ["jboss.jdbc-driver.mariadb"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
    "jboss.driver-demander.java:jboss/MariaDBDS is missing [jboss.jdbc-driver.mariadb]",
    "org.wildfly.data-source.MariaDBDS is missing [jboss.jdbc-driver.mariadb]"
]
}
07:05:21,148 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([
("subsystem" => "datasources"),
("data-source" => "MariaDBDS")
]) - failure description: {
"WFLYCTL0412: Required services that are not installed:" => [
    "jboss.jdbc-driver.mariadb",
    "jboss.jdbc-driver.mariadb"
],
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
    "jboss.driver-demander.java:jboss/MariaDBDS is missing [jboss.jdbc-driver.mariadb]",
    "org.wildfly.data-source.MariaDBDS is missing [jboss.jdbc-driver.mariadb]",
    "org.wildfly.data-source.MariaDBDS is missing [jboss.jdbc-driver.mariadb]"
]
}

I cannot let my service connect to DB, do I missed any step?

回答1:

If I look at your configuration, you setup the driver to be a module. So you create it as a module in your server rather than deploying it as a separate jar in the deployments folder.

If you want to setup a datasource where the driver is in the deployments folder, you do something like this:

<datasource jndi-name="java:jboss/MariaDBDS" pool-name="MariaDBDS">
    <connection-url>jdbc:mariadb://db_ip/db_name</connection-url>
    <driver>mariadb-java-client-1.4.6.jar</driver>
    <driver-class>org.mariadb.jdbc.Driver</driver-class>
    <security>
        <user-name>dbAcc</user-name>
        <password>dbPassword</password>
    </security>
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
        <validate-on-match>true</validate-on-match>
        <background-validation>false</background-validation>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
    </validation>
</datasource>

No separate driver declaration needed, its all contained within the datasource configuration.