In jOOQ, how does a bind value differ from an inlined value, and when is inlining the right choice?
answer
- placeholder versus literal
- default StatementType
- DSL.val and DSL.inline
- one SQL string per value
- getSQL rendering only
basics
~20 sjOOQ binds by default: a value passed to eq() or DSL.val renders as a ? placeholder sent separately. DSL.inline writes an escaped literal into the SQL text. Inline only true constants; varying inlined values defeat statement and plan caching.
solid answer
~40 sjOOQ's default `StatementType.PREPARED_STATEMENT` renders values as bind placeholders: `CUSTOMER.ID.eq(42)` wraps 42 in `DSL.val` and contributes `id = ?`, with 42 bound through a `PreparedStatement`. `DSL.inline(42)` renders the literal into the SQL text, escaping strings so `inline("abc'def")` becomes `'abc''def'`. Setting `StatementType.STATIC_STATEMENT` inlines every value and executes through a plain JDBC `Statement`, while `getSQL(ParamType.INLINED)` only changes the string you get back, for logging. The cost of inlining varying values is that each value produces distinct SQL text, so the database's plan cache and any prepared-statement cache stop reusing work. Inline when the value is a fixed constant of the query, such as a status the planner may benefit from seeing, and bind everything that comes from a request.
code
java · 11 linesimport org.jooq.conf.ParamType;
import org.jooq.impl.DSL;
import static com.example.db.Tables.ORDERS;
var query = ctx.selectFrom(ORDERS)
.where(ORDERS.CUSTOMER_ID.eq(customerId)) // bound: customer_id = ?
.and(ORDERS.STATUS.eq(DSL.inline("ACTIVE"))); // inlined: status = 'ACTIVE'
String forLog = query.getSQL(ParamType.INLINED); // string only
var rows = query.fetch(); // still one bind valuego deeper
Recall that jOOQ binds values by default as ? placeholders, and that DSL.inline is the explicit way to put a literal into the SQL text.
Explain StatementType versus ParamType, that getSQL(ParamType.INLINED) is rendering only, and why distinct SQL strings defeat statement and plan caches.
Diagnose cache churn from a global STATIC_STATEMENT setting or inlined request values, and know IN-list padding for variable-length lists.
Set a default that request data is always bound and literals are reserved for fixed constants, and decide where readable SQL logging may come from.
## Two ways a value reaches the database A query usually contains values: an ID, an email, a date. jOOQ can deliver a value in two ways. - A **bind value** is rendered as a placeholder (`?`) in the SQL text. The actual value travels separately and is set on a JDBC `PreparedStatement`. - An **inlined value** is rendered directly into the SQL text as a literal, such as `42` or `'ACTIVE'`. Both appear in jOOQ as a `Param`. `DSL.val(x)` creates a bind value, `DSL.inline(x)` an inlined one. When you pass a plain Java value to a field method, as in `CUSTOMER.ID.eq(42)`, jOOQ wraps it with `DSL.val` for you, typed after the field. ## The defaults and the switches | Mechanism | Effect | Scope | |---|---|---| | `StatementType.PREPARED_STATEMENT` | Values render as `?`, executed via `PreparedStatement` | Default `Settings` | | `DSL.inline(value)` | This one value renders as an escaped literal | One value | | `StatementType.STATIC_STATEMENT` | Every value inlined, executed via a plain `Statement` | Whole configuration | | `getSQL(ParamType.INLINED)` / `ctx.renderInlined(q)` | Returns a string with values inlined | Rendering only | The last row is a frequent confusion. `getSQL(ParamType.INLINED)` overrides the statement type only for the string it returns; it does not change how the query executes. It exists for log lines and debugging. `getBindValues()` returns the values that will actually be bound. ## Why binding is the default 1. **Statement and plan reuse.** With binds, `select ... where id = ?` is one SQL string no matter which ID you pass. Databases cache parsed statements and plans by text, and connection pools or drivers often cache prepared statements by text too. Inlining a varying value turns one string into thousands, each parsed and planned again. 2. **Type fidelity.** The JDBC driver sends the value with its type, so dates, decimals and binary data need no literal formatting. 3. **Safety by construction.** A bound value is never parsed as SQL. jOOQ does escape inlined values, so `DSL.inline("abc'def")` renders `'abc''def'`. Inlining through the DSL is therefore not an injection hole in the way that concatenating input into a plain SQL string is. The cost of inlining is performance and cache churn, not quoting. ## When inlining is right - **Fixed constants of the query.** `ORDER_STATUS.eq(inline("ACTIVE"))` never varies between calls, so it adds no new statement shapes, and the planner sees the literal. That can help on some databases, for example when a partial index is defined for that value. - **Debugging and logs.** Render with `ParamType.INLINED` to read a statement, without changing how it runs. - **Statements with very many values.** You rarely choose this one yourself: jOOQ may execute a statement without bind variables when the number of binds exceeds the dialect's maximum, and `getSQL()` does not reflect that switch. ## Reading what will actually execute When you debug a statement, look at both halves. `query.getSQL()` renders using the statement type from `Settings`, and `query.getBindValues()` returns only the values that will be sent as real bind values. Its sibling `getParams()` returns every `Param`, inlined ones included, keyed by name or index. You can also rebind a built query with `query.bind(index, value)` or `query.bind(name, value)` before running it again, which keeps one statement shape for repeated executions. | `ParamType` | Placeholder style when rendering | |---|---| | `INDEXED` (default) | `?` | | `NAMED` | named placeholders | | `NAMED_OR_INLINED` | named when a name is given, otherwise inlined | | `INLINED` | values as literals | | `FORCE_INDEXED` | `?`, even for values that were explicitly inlined or named | ## A related knob: IN-list padding An `IN (?, ?, ?)` with a different length on each call produces a different SQL string per length. `Settings.withInListPadding(true)` pads the list to powers of `inListPadBase` (default 2), so lengths 5 to 8 share one shape. It is off by default. ## Common mistakes - Turning on `STATIC_STATEMENT` globally "for readable logs" and then wondering why database CPU climbs. - Believing `getSQL(ParamType.INLINED)` changes execution. - Believing inlined values are unescaped. - Inlining request values such as a search term, creating one statement shape per user input.
- In jOOQ, how do you make every query of one configuration execute with inlined values?Set `new Settings().withStatementType(StatementType.STATIC_STATEMENT)` on that configuration. jOOQ then renders every value as a literal and executes through a plain JDBC `Statement` instead of a `PreparedStatement`. Scope it narrowly, for example a derived configuration for a tool, because it multiplies statement shapes.
- Why can a search endpoint with variable-length IN lists still churn the statement cache in jOOQ, even with bind values?Each list length renders a different number of placeholders, so each length is a new SQL string. `Settings.withInListPadding(true)` pads lists to powers of `inListPadBase` (default 2), repeating the last value, so many lengths share one statement shape. It is off by default.
A bind value is a printed form with a blank that the clerk fills in each time; an inlined value is a form with the answer pre-printed. Every different answer means printing and filing a brand-new form.
- jOOQ inlines values by default, so you must call DSL.val everywhere
- DSL.inline writes the raw string without escaping quotes
- getSQL(ParamType.INLINED) switches the query to execute with literals
- STATIC_STATEMENT is harmless because it only affects logging
- Inlining a request value is fine because jOOQ escapes it