Configure a second datasource

Hello,

I’m currently upgrading Keycloak from 16.1.0 to 18.0.2. So I have to change from Wildfly to Quarkus. I’m now struggling with the configuration of a second database pool for my SPIs. In the keycloak.conf you can only configure one datasource. With Wildfly I used the following configuartion:

<subsystem xmlns="urn:jboss:domain:datasources:6.0">
    <datasources>
        <xa-datasource jndi-name="java:jboss/datasources/keycloakDatabase" pool-name="keycloakDatabase" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
            ...Configuration like username and password...
        </xa-datasource>
        <xa-datasource jndi-name="java:jboss/datasources/secondDatabase" pool-name="secondDatabase" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
            ...Configuration like username and password...
            <driver>oracle</driver>
            <xa-pool>
                <min-pool-size>0</min-pool-size>
                <max-pool-size>100</max-pool-size>
                <prefill>true</prefill>
                <allow-multiple-users>false</allow-multiple-users>
                <is-same-rm-override>false</is-same-rm-override>
            </xa-pool>
            <validation>
                <validate-on-match>false</validate-on-match>
                <background-validation>false</background-validation>
                <use-fast-fail>true</use-fast-fail>
                <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
                <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
            </validation>
            <timeout>
                <blocking-timeout-millis>15000</blocking-timeout-millis>
                <idle-timeout-minutes>1</idle-timeout-minutes>
                <query-timeout>30</query-timeout>
            </timeout>
        </xa-datasource>
        <drivers>
            <driver name="oracle" module="oracle.jdbc">
                <xa-datasource-class>oracle.ucp.jdbc.PoolXADataSourceImpl</xa-datasource-class>
            </driver>
        </drivers>
    </datasources>
</subsystem>

So I could link my SPIs with the secondDatabase:

<spi name="storage">
    <provider name="my-spi-with-different-database" enabled="true">
        <properties>
          <property name="dataSource" value="java:jboss/datasources/secondDatabase"/>
        </properties>
    </provider>
</spi>

This way, there is only one connection to the database from keycloak and not every SPI - that uses the secondDatabase - has his own connection. So the SPI’s do not get in each others way.

I can’t find a similar solution for Quarkus. Any one else stepped into this problem?

See this example: keycloak-quickstarts/user-storage-jpa at main · keycloak/keycloak-quickstarts · GitHub
And the config file in config/quarkus.properties

3 Likes

Thanks for the example.
Basicially I add the persitance.xml to every SPI and Keycloak gets the database configuration from the quarkus.properties, so the SPIs wont interfere with each other?!
This means a major restructuring of my code, but i will try it.

Hello,this URL doesn’t work now. Do you know more documentation? Do we have to configure the datasource inside each SPI?
Thanks.

One could click around in the given repository and see, if the example has moved…