Usage of Jooq DSLContext in spring boot

Viewed 850

What is the proper way to use DslContext. Does having bean autoconfiguration, or calling DSL.using() method directly before execution; Do they vary in performance ?

@Autowired DataSource dataSource;

@PostConstruct
@Bean(name = "ExecutorDslContext")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DSLContext executorDslContext() {
  return DSL.using(dataSource, SQLDialect.MEMSQL);
}

Should the Scope be Prototype or Singleton ?

What's the impact of using above mentioned bean for execution vs

Result<Record> result = DSL.using(...).select().from(TABLE);
2 Answers

Regarding:

calling DSL.using() method directly before execution; Do they vary in performance ?

Calling DSL.using() will create a new DefaultConfiguration every time. This isn't a big overhead per se, but you won't profit from reflection caching and some other caches that can be shared among sessions that re-use the same configuration instance. In general, there's no need to create a new Configuration instance for every single query, so ideally just inject the shared and pre-configured DSLContext as suggested by Simon

Additional information can be found in the manual's sections

Related