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.

Attachable

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

Some jOOQ objects can hold an internal reference to a Configuration. These objects are said to be "attached" to that Configuration, and they use it whenever they need to interact with your database, for example to execute a query or to store a record. Objects that don't have such a reference are said to be "detached" or "unattached".

All of these objects implement the org.jooq.Attachable API:

public interface Attachable extends Serializable {

    // Attach this object to a new Configuration, or detach it by passing null
    void attach(Configuration configuration);

    // Detach this object from its current Configuration. This is the same as attach(null)
    void detach();

    // Get the currently attached Configuration, or null, if this object is detached
    Configuration configuration();
}

Any operation that requires a database connection will throw a org.jooq.exception.DetachedException, when called on a detached object.

Implementations of Attachable

The most important Attachable types are:

Most other QueryParts, such as column expressions, conditions, or table expressions are not attachable. They are mere building blocks of a query's expression tree, which do not interact with the database on their own.

How objects get attached

In most cases, you don't have to think about attaching objects, as jOOQ does it for you:

  • Queries constructed from a DSLContext are attached to the DSLContext's Configuration, whereas queries constructed from the static DSL are not.
  • Records and results fetched from an attached query are attached to the same Configuration (unless this is turned off using the attachRecords setting).
  • Records and results created using the DSLContext, e.g. using DSLContext.newRecord() or DSLContext.newResult(), are attached (see also creating results in memory).
  • Records created using a constructor, e.g. new BookRecord(), are not attached.

If you have a detached object, there are various ways to attach it, or to execute it in the context of a Configuration:

// A query constructed from the static DSL is not attached
Select<?> select = DSL.select(BOOK.TITLE).from(BOOK);

// This would throw a DetachedException:
// select.fetch();

// Attach it explicitly, either directly, or via a DSLContext:
select.attach(configuration);
ctx.attach(select);
Result<?> result = select.fetch();

// Alternatively, don't (permanently) attach it, but execute it in the context of a DSLContext:
Result<?> result = ctx.fetch(select);

// Records created with a constructor are not attached either
BookRecord book = new BookRecord();
book.setTitle("1984");

// Attach them before storing them, or create them attached using ctx.newRecord(BOOK)
ctx.attach(book);
book.store();

// You can also detach objects, e.g. to prevent any further interaction with the database
book.detach();

Methods like DSLContext.fetch(ResultQuery) or DSLContext.execute(Query) temporarily attach the argument query to the DSLContext's Configuration for the duration of the execution, and then restore the previous attachment.

Attachment and connection lifecycle

Attaching or detaching an object has no effect on the underlying connection. In particular, detaching an object does not close any JDBC java.sql.Connection. Connections are always acquired and released through the Configuration's org.jooq.ConnectionProvider, as described in the section about Connection vs. DataSource.

All Attachable types are java.io.Serializable, but the underlying connection is transient. Deserialised objects may thus need to be re-attached to a Configuration before they can interact with the database again. 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