Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Last active May 1, 2024 22:36
Show Gist options
  • Save jgaskins/9aa94c43748a90141363cedc2cced21b to your computer and use it in GitHub Desktop.
Save jgaskins/9aa94c43748a90141363cedc2cced21b to your computer and use it in GitHub Desktop.
ActiveSupport::Executor example
require "active_support/executor"
require "active_record"
require "pg"
ActiveRecord::Base.establish_connection(
uri: "postgres:///",
adapter: :postgresql,
pool: 5,
)
# Just happens to map to a table this DB
class Task < ActiveRecord::Base
end
# Executed on completion of any `Executor.wrap` block
ActiveSupport::Executor.to_complete do
# Release this thread's connection back to the pool
ActiveRecord::Base.connection_pool.release_connection
end
# Wrap your app code in this block, for example in Rack middleware
ActiveSupport::Executor.wrap { Task.first }
# The connection's owner should be `nil`
pp ActiveRecord::Base.connection_pool.connections.first.owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment