get schemata.output from configuration at runtime

24 views
Skip to first unread message

Jim McGlaughlin

unread,
Mar 17, 2021, 3:31:25 PM3/17/21
to jOOQ User Group
Hello,

I am using the same schemata for several databases using 'outputSchemaToDefault'.

At runtime I would like to get the specific database the DSLContext is connected to.

In a debugger I can see that info is stored in

configuration.settings.renderMapping.schemata.0.output

Is the a method of the DSLConext to retrieve this?  I would like to avoid having to pass this value through layers of code which are already carrying the DSLContext.

Thanks,
Jim

Lukas Eder

unread,
Mar 18, 2021, 4:54:16 AM3/18/21
to jOOQ User Group
Hi Jim,

Please use a different DSLContext (and backing Configuration) with a different Settings instance for each database as documented here:

You can create derived configurations using Configuration.derive(Settings) for that purpose, but do note that each time you derive a Configuration, the internal caches (e.g. used for reflection of into(Class) calls) may be flushed. Chances are, you'll have separate JDBC connections, though, one per database, and with separate connections, you might have a separate beans (e.g. if you're using Spring), and you can inject the appropriate Settings once for each bean.

I hope this helps,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/2ba8d594-e59d-4e11-b130-177a41479b8fn%40googlegroups.com.

Jim McGlaughlin

unread,
Mar 19, 2021, 10:27:17 AM3/19/21
to jOOQ User Group
WONDERFUL Lukas, Your are the greatest !!

I have been using settings and that works well for my case.  In case this  helps someone else I am documenting what I did.

I have many identical database schemas created using settings in the below helper method

        public static Settings getSettings( String specificDatabase ) {
            String schemaInput = "dashboard2";
            Settings settings = new Settings()
                .withRenderMapping(new RenderMapping()
                .withSchemata(
                    new MappedSchema().withInput(schemaInput)
                    .withOutput(specificDatabase)
                )
            );
            return settings;
        }

Based on your help the following retrieves the specific database being used

        public static String getDatabaseNameFromDsl(DSLContext dsl) {
            Settings settings = dsl.settings();
            return settings.getRenderMapping().getSchemata().get(0).getOutput();
        }       

Thanks again,
Jim
Reply all
Reply to author
Forward
0 new messages