Connecting to a separate database per role/login via a connection switch
Another quick solution for connecting to a separate database per role/login consists of switching to the proper connection at runtime. In order to accomplish this task, we have to suppress the jOOQ default behavior of rendering the schema/catalog name. This way, we don't risk connecting to database A
but get database B
rendered in front of our tables, and so on. In other words, we need unqualified names.
jOOQ allows us to turn off rendering the schema/catalog name via the withRenderSchema(false)
and withRenderCatalog(false)
settings. The following example connects to the database having the same name as the role of the logged in user and suppresses rendering the schema/catalog names:
Authentication auth = SecurityContextHolder .getContext().getAuthentication(); if (auth != null && auth.isAuthenticated()) { String authority = auth.getAuthorities...