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
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
Configurationand 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
Configurationvia 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:
-
org.jooq.SQLDialect: The dialect of your database. This may be any of the currently supported database types (see SQL Dialect for more details) -
org.jooq.conf.Settings: An optional runtime configuration (see Custom Settings for more details) -
org.jooq.ExecuteListenerProvider: To provide execution lifecycle listeners (see ExecuteListeners for more details) -
org.jooq.RecordListenerProvider: To provide record listeners for your CRUD operations (see RecordListener SPI for more details) -
org.jooq.RecordMapperProvider: To provide an alternative default record mapper implementation (see RecordMapperProvider for more details) -
org.jooq.TransactionProvider: To provide custom transaction semantics (see Transaction management for more details) -
org.jooq.VisitListenerProvider: To provide listeners that can observe and transform the SQL rendering process (see Custom SQL transformation with VisitListener for more details) -
org.jooq.UnwrapperProvider: To override how JDBC types are unwrapped to native driver implementations (see Custom Unwrappers for more details) - Custom data : Arbitrary user-defined data that is made available to SPI implementations (see Custom data for more details)
- Any of these:
-
java.sql.Connection: An optional JDBC Connection that will be re-used for the whole lifecycle of your Configuration (see Connection vs. DataSource for more details). For simplicity, this is the use-case referenced from this manual, most of the time. -
java.sql.DataSource: An optional JDBC DataSource that will be re-used for the whole lifecycle of your Configuration. If you prefer using DataSources over Connections, jOOQ will internally fetch new Connections from your DataSource, conveniently closing them again after query execution. This is particularly useful in Java EE or Spring contexts (see Connection vs. DataSource for more details) -
org.jooq.ConnectionProvider: A custom abstraction that is used by jOOQ to "acquire" and "release" connections. jOOQ will internally "acquire" new Connections from your ConnectionProvider, conveniently "releasing" them again after query execution. (see Connection vs. DataSource for more details)
-
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.
References to this page
- The DSLContext API
- Attached and detached objects: The Attachable API
- The SQLDialect enum
- Using a JDBC Connection vs. a JDBC DataSource with jOOQ
- Custom data in a Configuration
- Custom ExecuteListeners in a Configuration
- Settings: auto-attach records
- Settings: listener invocation order
- Using jOOQ's parser as a SQL translator
- Serializability of jOOQ types
- Fetching into POJOs
- The RecordMapperProvider SPI
- Static statements vs. Prepared Statements
- Simple CRUD
- The RecordListener SPI
- ExecuteListeners
- Mocking JDBC Connections with jOOQ
- The Parsing Connection
- Performance considerations
- Migrating to jOOQ 3.0
- Don't do this with jOOQ: implementing the DSL types
Feedback
Do you have any feedback about this page? We'd love to hear it!