Skip to content

Ensure no permanent DB connections are used - #28

Merged
jujustayfly merged 15 commits into
mainfrom
dont-use-permanent-connections
Aug 7, 2025
Merged

Ensure no permanent DB connections are used#28
jujustayfly merged 15 commits into
mainfrom
dont-use-permanent-connections

Conversation

@jujustayfly

@jujustayfly jujustayfly commented Jul 31, 2025

Copy link
Copy Markdown
Contributor

Replace .connection with .connection_pool.with_connection for proper connection management

What

Refactored all direct .connection calls to use .with_connection blocks in adapters, tests, and documentation.

Why

  • Ensures proper connection pool cleanup
  • Prevents connection leaks in multi-threaded environments
  • Follows Rails best practices for ActiveRecord connection handling

# correctly, thus the clock_timestamp() value would be frozen between calls. We don't want that here.
# See https://stackoverflow.com/questions/73184531/why-would-postgres-clock-timestamp-freeze-inside-a-rails-unit-test
upserted = @model_class.connection.uncached { @model_class.connection.select_one(sql) }
upserted = Pecorino.with_connection_or_fallback(@model_class) { |connection| connection.uncached { connection.select_one(sql) } }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can do @model_class.connection_pool.with_connection for all those cases instead. ..._or_fallback is not really needed as you will always have a pool with ActiveRecord these days

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julik see https://github.com/cheddar-me/pecorino/actions/runs/16647409861/job/47111305387 the tests for rails 7 were failing because with_connection is not defined. I had initially built it using with_connection directly but it might be helpful to preserve backwards compatibility

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True - but that is on the ActiveRecord subclass, what I'm suggesting is using the pool instead - so @model_class.connection_pool.with_connection, not @model_class.with_connection

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done but in the block tests AR did not have a pool so had to build one in the setup but no big deal

@jujustayfly
jujustayfly force-pushed the dont-use-permanent-connections branch from aed26e1 to 93a662c Compare August 5, 2025 20:53
@jujustayfly
jujustayfly force-pushed the dont-use-permanent-connections branch from 624b17b to fb915fd Compare August 6, 2025 09:20

@svanhesteren svanhesteren left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks and good to go. Let's see how this improves things.

@jujustayfly
jujustayfly merged commit 9e33758 into main Aug 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants