What is the recommeded way to connect to an external database in a custom SPI?

I’ve implemented a custom UserStorageProvider SPI and can successfully get a connection to an external database by defining the necessary parameters in keycloak/conf/quarkus.properties and then using the following code in the SPI to get access to an EntityManager.

keycloakSession.getProvider(JpaConnectionProvider.class, "my-datasource").getEntityManager();

This works great. However, I’d rather have the connection details be provided as configuration properties in the SPI. As in when the User Federation SPI is installed the user will type in the database connection details. I also have this working and can access the entered properties in my custom SPI.

What is the recommeded approach to creating an EntityManager in a SPI? I’ve seen an example here:

Is this the recommended approach or is there a better way?

For your usecase, I don‘t know of any „recommended way“. If you don‘t want to do it through config settings, use whatever suits your requirements and what works (at the time being and expect/ be prepared for possible changes in behavior in the future)

Hi,

I wrote a User Federation SPI. This isn’t a 100% match for what you’re asking but it does interact with the UI to parameterize the SPI. One key difference is that what you’ve done appears to be a singleton (one DB per Keycloak). The GH link I’m provided installs the SPI at the component level meaning that there each Realm can have multiple SPIs that stack.

I don’t attempt to inject a datasource. My SPI is dynamic and needs to build the datasource and connection pool on-the-fly.

https://github.com/bekwam/bekwam-jdbc-user-storage-spi