Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12

This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.

Configuration

Supported by ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The org.jooq.Configuration is the central object that configures jOOQ's behaviour when rendering SQL, binding bind values, executing queries, as well as when fetching, mapping, and storing data. It holds the state that defines how jOOQ behaves (such as the SQLDialect and Settings), the means to connect to your database (such as a JDBC javax.sql.DataSource), and a variety of SPIs to extend or override jOOQ's default behaviour.

The Configuration is rarely used directly to construct queries. Instead:

  • A DSLContext wraps a Configuration and exposes the API to construct and execute queries in its context.
  • Objects such as queries, records, results, or routines are attached to a Configuration, which they use internally whenever they need to interact with the database.
  • SPI implementations, such as an ExecuteListener, typically get access to the current Configuration via their context argument, e.g. ExecuteContext.configuration().

Creating a Configuration

The default implementation is the org.jooq.impl.DefaultConfiguration, which can be set up fluently using its various set() methods, and then be wrapped in a DSLContext:

// Create a Configuration explicitly
Configuration configuration = new DefaultConfiguration()
    .set(dataSource)
    .set(SQLDialect.POSTGRES)
    .set(new Settings().withRenderFormatted(true));

// Wrap it in a DSLContext to construct and execute queries
DSLContext ctx = DSL.using(configuration);

// ... which is the same as
DSLContext ctx = configuration.dsl();

// Conversely, you can always access the Configuration from the DSLContext
Configuration c = ctx.configuration();

For convenience, the various overloaded DSL.using() methods create a DefaultConfiguration for you from ad-hoc arguments, such as a java.sql.Connection and a org.jooq.SQLDialect. When using third party integrations, such as Spring Boot's jOOQ starter, the Configuration is usually created and configured by the framework, and you can inject it (or the DSLContext) into your code.

Contents of a Configuration object

A Configuration can be supplied with these objects:

Deriving Configurations and thread safety

A Configuration is designed to be long-lived, e.g. a singleton in your application, shared among many threads. In that case, thread safety is delegated to its components, such as the org.jooq.ConnectionProvider, which must be thread safe themselves (e.g. a javax.sql.DataSource backed by a connection pool).

For this to work, a shared Configuration must not be modified after its initialisation. Avoid calling any of the mutating set() methods, and do not modify its org.jooq.conf.Settings anymore. If you need a slightly different Configuration for a local use-case, use one of the derive() methods, instead, which create a new Configuration from the existing one, with some parts replaced:

// Create a new, derived Configuration with different Settings, leaving the original one untouched
Configuration derived = configuration.derive(new Settings().withRenderFormatted(false));

// Use the derived Configuration locally
DSL.using(derived).select(BOOK.TITLE).from(BOOK).fetch();

jOOQ itself derives Configuration objects internally in a few places, for example when running transactions, where your transactional code receives a new, transaction-scoped Configuration that should be used instead of the global one.

Serializability

A Configuration is java.io.Serializable, but its underlying connection (such as the JDBC java.sql.Connection or javax.sql.DataSource) and possibly some SPIs are not. This is why attached objects may need to be re-attached to a Configuration after deserialisation. See the manual's section about serializability for more details.

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo