karthikeya95
karthikeya95

Reputation: 33

Usage of Jooq DSLContext in spring boot

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);

Upvotes: 2

Views: 3735

Answers (2)

Lukas Eder
Lukas Eder

Reputation: 220842

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

Upvotes: 2

Simon Martinelli
Simon Martinelli

Reputation: 36123

The correct way is to use org.springframework.boot:spring-boot-starter-jooq and inject DslContext.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.sql.jooq

Upvotes: 3

Related Questions