Skip to content

Ensure Jobs do not hold database connections outside of queries - #22

Closed
jujustayfly wants to merge 1 commit into
mainfrom
dont-hold-database-connection
Closed

Ensure Jobs do not hold database connections outside of queries#22
jujustayfly wants to merge 1 commit into
mainfrom
dont-hold-database-connection

Conversation

@jujustayfly

@jujustayfly jujustayfly commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

Add configurable connection management to prevent database connection hoarding

Problem

Gouda jobs hold database connections for their entire execution duration, reducing available connections in the pool and potentially causing exhaustion under load.

Root cause: ActiveRecord operations in jobs call ActiveRecord::Base.connection, which checks out a permanent connection that remains held until the Rails executor completes (i.e., when the entire job finishes).

class MyJob < ActiveJob::Base
  def perform
    User.find(123)           # Checks out connection
    sleep(30)                # Connection remains held during sleep
    user.update(seen: true)  # Still using same held connection
  end
end

Solution

Added prevent_connection_hoarding configuration (default: false) that wraps Gouda's executor to implement Rails 7.2+ style per-query connection management.

How it works: Temporarily overrides ActiveRecord::Base.connection during job execution to use connection_pool.with_connection, ensuring connections are released immediately after each database operation.

Configuration

# Enable connection management (opt-in)
Rails.application.configure do
  config.gouda.prevent_connection_hoarding = true
end

Benefits

  • Reduced connection pool pressure: Connections released between queries
  • Better resource utilization: Long-running jobs don't monopolize connections
  • Backwards compatible: Disabled by default, no behavior changes for existing users
  • Zero application changes: Works with existing job code patterns

Testing

  • Verifies connections are released during job sleep periods when enabled
  • Confirms plain executor behavior when disabled (default)
  • All existing tests pass

@jujustayfly

Copy link
Copy Markdown
Contributor Author

The issue is actually coming from our application and we will probably want to address the issue there instead of stubbing ActiveRecord::Base.connection

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.

1 participant