Skip to content

Instantly share code, notes, and snippets.

@ioquatix
Last active October 2, 2025 01:35
Show Gist options
  • Select an option

  • Save ioquatix/f419bf18d7af8ba5cda81fdadee458f6 to your computer and use it in GitHub Desktop.

Select an option

Save ioquatix/f419bf18d7af8ba5cda81fdadee458f6 to your computer and use it in GitHub Desktop.

Revisions

  1. ioquatix revised this gist Sep 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion connections.md
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,7 @@ Highscore.connection_pool.active_connection?

    ```ruby
    Highscore.with_connection do
    Highscore.lease_connection
    Highscore.lease_connection
    end

    Highscore.connection_pool.active_connection?
  2. ioquatix created this gist Sep 26, 2024.
    60 changes: 60 additions & 0 deletions connections.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    ## Permanent Connection Checkout

    Rails 7.2 defaults:

    ```ruby
    Highscore.connection
    # => #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x000000000080e8 env_name="development" role=:writing>
    ```

    With `config.active_record.permanent_connection_checkout = :disallowed`:

    ```ruby
    Highscore.connection
    # Called deprecated `ActiveRecord::Base.connection` method. (ActiveRecord::ActiveRecordError)
    # Either use `with_connection` or `lease_connection`.

    Highscore.lease_connection
    # => #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x000000000103b0 env_name="development" role=:writing>

    Highscore.connection_pool.active_connection?
    # => #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x000000000103b0 env_name="development" role=:writing>

    Highscore.release_connection
    # => true

    Highscore.connection_pool.active_connection?
    # => nil
    ```

    ## With Connection

    ```ruby
    Highscore.connection_pool.active_connection?
    # => nil

    Highscore.first
    # => #<Highscore:0x00007d3751d853c8 id: 1, name: "Anonymous", score: 1, created_at: "2024-04-24 06:12:16.208562000 +0000", updated_at: "2024-04-24 06:12:16.208562000 +0000">

    Highscore.connection_pool.active_connection?
    # => nil

    Highscore.with_connection do
    Highscore.connection_pool.active_connection?
    end
    # => #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x0000000002a7d8 env_name="development" role=:writing>

    Highscore.connection_pool.active_connection?
    # => nil
    ```

    ### Leased Connection within With Connection

    ```ruby
    Highscore.with_connection do
    Highscore.lease_connection
    end

    Highscore.connection_pool.active_connection?
    # => #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x00000000038270 env_name="development" role=:writing>
    ```