Search code examples
javamysqlwildflykeycloakwildfly-10

KeyCloak WildFly Standlone module MySQL


I am trying to deploy a WildFly KeyCloak Server in standalone Mode. This works fine when I just do nothing but now I want to add a Datasource of MySQL.
Therefore I have to add a driver i am using the: mysql-connector-java-8.0.20.jar.
To add the driver I put it inside /modules/com/mysql/main/. Then I add a module.xml file:

<module xmlns="urn:jboss:module:1.1" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-java-8.0.20.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api">
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

Then I add the follwing in the standalone.xml:

<driver name="mysql" module="com.mysql">
     <xa-datasource-class>com.mysql.cj.jdbc.MysqlXADataSource</xa-datasource-class>
</driver>

When I then deploy the Server with the standalone.bat I get following error:

16:57:43,962 ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 32) WFLYCTL0013: Operation ("add") failed - address: ([
    ("subsystem" => "datasources"),
    ("jdbc-driver" => "mysql")
]) - failure description: "WFLYJCA0041: Failed to load module for driver [com.mysql]"

Cheers !!


Solution

  • Simplify your life so that the next time you do this it's not manual. Create a file that contains something like:

    embed-server --server-config=standalone.xml --std-out=echo
        
    batch
    
    #
    # remove the default provided datasource
    #
    /subsystem=datasources/data-source=KeycloakDS/:remove
    
    #
    # add the module
    #
    module add --name=com.mysql --resources=${user.home}/Downloads/mysql-connector-java-8.0.20/mysql-connector-java-8.0.20.jar --dependencies=javax.api,javax.transaction.api
    
    #
    # create the driver
    #
    /subsystem=datasources/jdbc-driver=mysql:add(driver-name="mysql",driver-module-name="com.mysql",driver-class-name=com.mysql.cj.jdbc.MysqlXADataSource)
    
    #
    # create the datasource
    #
    /subsystem=datasources/data-source=KeycloakDS/:add(connection-url=jdbc:mysql://localhost:3306/keycloak,driver-name=mysql,jndi-name=java:jboss/datasources/KeycloakDS,initial-pool-size=4,max-pool-size=64,min-pool-size=4,password=keycloak,user-name=keycloak)
    
    run-batch
    

    Run this with $KEYCLOAK_HOME/bin/jboss-cli.sh --file=<the_file_name>. Keyclock cannot be running when you run this script. And the user and database must already exist before you restart Keycloak.

    What this does is

    1. Remove the "old" Keycloak Datasource
    2. Install the module. In the code above you will need to update the location of the MySQL driver.
    3. Re-install the Keycloak datasource with the correct parameters.

    The advantage is that this is repeatable if you need to install again or into a different server.

    And do you need the XA driver? If I was developing in Wildfly for myself I might need XA but I don't think Keycloak is going to take advantage of it.