Search code examples
quarkusjooq

Using jOOQ in Quarkus - SQLDialect needs to be defined manually


When using jOOQ together with the Quarkus Framework, i need to pick the SQL-Dialect manually, because it is not picked-up / resolved from the typical application.conf setting. For example: quarkus.datasource.db-kind=mariadb for mariadb.

I can resolve it manually, by defining an additional property in the application.conf and resolve it manually.

See following example:

...
final Optional<@NotNull SQLDialect> maybeSqlDialect =
        Arrays.stream(SQLDialect.families()).filter(x -> x.name().equalsIgnoreCase(jooqDialect)).findFirst();
if (maybeSqlDialect.isEmpty()) {
    throw new RuntimeException("sql-dialect not found! " + jooqDialect);
}
final SQLDialect sqlDialect = maybeSqlDialect.get();

Configuration configuration = new DefaultConfiguration()
        .set(dataSource)
        .set(sqlDialect)
        .set(new Settings()
          ...
        );
...

is it possible to let jOOQ automatically resolve the SQLDialect from the javax.sql.Datasource, or are there other alternative i may not know of, to make jOOQ correctly select the SQLDialect for my quarkus datasource configuration, that i setup in the application.conf file?

Used Versions:

  • Quarkus 3.8.2
  • jOOQ 3.19.6

Solution

  • jOOQ itself won't resolve the SQLDialect from the javax.sql.DataSource alone, because to do so, it would have to call DataSource.getConnection(), which is potentially a costly operation. The SQLDialect is already necessary before the Connection is fetched, and not all interactions with jOOQ will require a Connection. E.g. when logging formatted SQL, the generated SQL requires only a SQLDialect, not a Connection, as it isn't executed.

    If you can provide jOOQ with a JDBC Connection instead, then it can find the SQLDialect from the meta data, however:

    // This dialect is inferred:
    DSL.using(connection).configuration().dialect();
    

    Alternatively, if you know how your DataSource is set up (e.g. from a properties file), then you can also use JDBCUtils.dialect(String) to have jOOQ look at the JDBC URL to derive the SQLDialect family at least. The dialect version can't be detected this way either, for that, a JDBC Connection is also necessary (and its DatabaseMetaData API)