Commit cb44e74
committed
Honor intent.prepare in _exec_insert to restore pre-Rails-8 caching behavior
Background
----------
Before the Rails 8 adoption commit (baf9fd4 "Rails 8 support",
2025-02-23), `_exec_insert` decided whether to reuse a cached prepared
statement using `without_prepared_statement?(binds)`, which factored
in the `prepared_statements` config:
if without_prepared_statement?(binds)
cursor = _connection.prepare(sql) # fresh cursor
else
@Statements[sql] ||= _connection.prepare(sql) # cached cursor
cursor = @Statements[sql]
cursor.bind_params(...)
...
end
`baf9fd48` replaced that helper with the following predicate, in line
with the broader Rails 8 changes that retired `without_prepared_statement?`:
if binds.nil? || binds.empty?
cursor = _connection.prepare(sql) # fresh cursor
else
@Statements[sql] ||= _connection.prepare(sql) # cached cursor
...
end
After that change, the `prepared_statements` config is no longer part
of the cache decision in `_exec_insert`. Calls that arrive with
non-empty binds — including raw
`connection.insert("INSERT ... VALUES ('literal')", nil, "id")`, where
`sql_for_insert` appends a single `:returning_id` placeholder — go
through the cached-cursor branch on every invocation.
How it surfaced
---------------
Three things lined up:
1. A set of pre-2018 specs that issue the same raw-SQL INSERT with
`RETURNING ... INTO :returning_id` more than once on the same
adapter were removed in 2017 (commit ef321a1).
2. While those specs were absent, baf9fd4 (Rails 8 support, Feb
2025) widened `_exec_insert`'s cached-cursor branch as described
above.
3. The same spec patterns were restored in 2026 as part of porting
the trigger-based primary-key option (rsim#2615), and ran against the
post-baf9fd48 cache decision for the first time.
Under that combination, plus `cursor_sharing = FORCE` (the adapter
default), executing the same cached cursor a second time produces a
SQL*Net half-duplex deadlock between ruby-oci8 and the Oracle 23ai
server: the server waits in `SQL*Net more data from client` while
ruby-oci8 stays in `OCIStmtExecute -> nttfprd -> read()`. See rsim#2619
for the full diagnosis and a 5-line standalone repro.
Fix
---
Reintroduce the prepared-statements check using the Rails 8.2
QueryIntent successor to `without_prepared_statement?`: `intent.prepare`.
PostgreSQL gates on the same flag (rails-3d7458fecf49
postgresql/database_statements.rb:167).
For raw SQL paths, `intent.prepare` is left as its default `false`
(set by `connection.insert` in abstract/database_statements.rb), so
the fresh-cursor path runs and the cached-cursor combination above is
not reached.
Behavior after this change:
- `klass.create!(attrs)` (Arel insert, prepared_statements=true)
keeps the cached path.
- `connection.insert("INSERT ...", nil, "id")` (raw SQL) takes the
fresh-cursor path and never reuses a cursor with a stale OCI bind
state.
- Connections with `prepared_statements: false` always use fresh
cursors, matching the pre-`baf9fd48` behavior.
Tradeoff
--------
The change moves raw-SQL `connection.insert` paths to a fresh cursor
on every call, giving up the cached-cursor reuse benefit for those
calls. That tradeoff is acceptable because:
- Issuing the exact same raw SQL string through `connection.insert`
twice in a row is uncommon in Rails application code (model paths
go through Arel and parameterise their bind values).
- A SQL*Net deadlock that wedges the rest of the suite is a larger
problem than losing per-call cursor reuse on a rarely exercised
path.
The cached path is preserved unchanged for ORM (Arel-based) inserts,
which are the high-volume callers.
Regression spec
---------------
`spec/.../exec_insert_caching_spec.rb` issues the same
`INSERT ... NEXTVAL ... RETURNING "ID" INTO :returning_id` SQL twice
through `connection.insert`. Without the fix the second exec hits the
half-duplex deadlock and trips the spec's 5-second `Timeout`; with the
fix it completes well under 100ms. A short comment notes the
dependency on the default `cursor_sharing = force` so a future change
in defaults does not silently turn the regression test into a no-op.
Closes rsim#2619.1 parent 903067d commit cb44e74
2 files changed
Lines changed: 44 additions & 1 deletion
File tree
- lib/active_record/connection_adapters/oracle_enhanced
- spec/active_record/connection_adapters/oracle_enhanced
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
66 | 76 | | |
67 | 77 | | |
68 | 78 | | |
| |||
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
0 commit comments