Skip to content

Pin a DB connection for the Session's lifetime (with_connection) to survive implicit check-ins #43

Description

@sncalvo

Problem

Rails 7.2+ changed connection checkout semantics (per-request check-in via the executor). A Session that doesn't pin a connection can have its connection returned to the pool mid-block, which invalidates temp tables on Postgres.

Proposal

Wrap the session body in ActiveRecord::Base.connection_pool.with_connection { ... } (or the source_model's pool), pinning the connection for the duration of the block.

StagingTable.stage(User) do |staging|  # automatic with_connection
  staging.insert(rows)
end

Opt-out for advanced users who manage their own connection lifecycle:

StagingTable.stage(User, pin_connection: false) { ... }

Acceptance criteria

  • Default behavior pins the connection for the stage { } block
  • Manual-control API gets a Session#with_pinned_connection equivalent
  • Test: under a forced pool-check-in mid-block (simulate via executor), temp table survives
  • README note on Rails 7.2+ behavior change and why pinning matters

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions