{"id":28566809,"url":"https://github.com/salsify/activerecord-forbid_implicit_connection_checkout","last_synced_at":"2025-08-03T21:37:34.977Z","repository":{"id":45988428,"uuid":"118165007","full_name":"salsify/activerecord-forbid_implicit_connection_checkout","owner":"salsify","description":"Optionally prevent threads from checking out out an ActiveRecord connection","archived":false,"fork":false,"pushed_at":"2025-03-11T13:09:40.000Z","size":40,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":36,"default_branch":"master","last_synced_at":"2025-07-27T09:38:42.409Z","etag":null,"topics":["gem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/salsify.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-01-19T18:52:05.000Z","updated_at":"2025-03-11T13:09:06.000Z","dependencies_parsed_at":"2023-02-09T13:15:17.830Z","dependency_job_id":"f3431a8e-b7a0-4564-a82c-1dd41ab6a230","html_url":"https://github.com/salsify/activerecord-forbid_implicit_connection_checkout","commit_stats":{"total_commits":18,"total_committers":5,"mean_commits":3.6,"dds":0.5555555555555556,"last_synced_commit":"db9d88304e6b2ab2aef39584c5cecd5ef8c370ac"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/salsify/activerecord-forbid_implicit_connection_checkout","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Factiverecord-forbid_implicit_connection_checkout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Factiverecord-forbid_implicit_connection_checkout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Factiverecord-forbid_implicit_connection_checkout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Factiverecord-forbid_implicit_connection_checkout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/salsify","download_url":"https://codeload.github.com/salsify/activerecord-forbid_implicit_connection_checkout/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Factiverecord-forbid_implicit_connection_checkout/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268197920,"owners_count":24211805,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem"],"created_at":"2025-06-10T15:39:08.763Z","updated_at":"2025-08-03T21:37:34.919Z","avatar_url":"https://github.com/salsify.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://circleci.com/gh/salsify/activerecord-forbid_implicit_connection_checkout.svg?style=svg)][circleci]\n\n# ActiveRecord-ForbidImplicitCheckout\n\nThis gem allows a `Thread` to prevent itself from checking out out an ActiveRecord connection. This can be useful\nin preventing your application from accidentally checking out more connections than the database can handle.\n\nInspired by this blog post: https://bibwild.wordpress.com/2014/07/17/activerecord-concurrency-in-rails4-avoid-leaked-connections/\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord-forbid_implicit_checkout'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install activerecord-forbid_implicit_checkout\n\n## Usage\n\n```ruby\nrequire 'active_record-forbid_implicit_connection_checkout'\n\nThread.new do\n  ActiveRecord::Base.forbid_implicit_connection_checkout_for_thread!\n  # Code that doesn't require ActiveRecord\nend\n```\n\nIf your thread needs a connection at some point\n```ruby\nThread.new do\n  ActiveRecord::Base.forbid_implicit_connection_checkout_for_thread!\n  # Code that doesn't require ActiveRecord\n\n  ActiveRecord::Base.connection_pool.with_connection do\n    # Allow the thread to take a connection within this block\n    ActiveRecord::Base.connection\n  end\n\n  # Code that doesn't require ActiveRecord\nend\n```\n\n## Why\nConsider the following initially:\n\n```ruby\nrequire 'active_record-forbid_implicit_connection_checkout'\n\nclass Foo\n  def self.download(id)\n    Net::HTTP.get(URI(\"http://example.com/products/#{URI.encode(id)}\"))\n  end\nend\n\nclass Downloader\n  def download_all(ids)\n    threads = ids.map do |id|\n      Thread.new do\n        ActiveRecord::Base.forbid_implicit_connection_checkout_for_thread!\n        Foo.download\n      end\n    end\n    threads.each(\u0026:join)\n  end\nend\n\nDownloader.download_all([1,2,3,4,5])\n```\n\nThe developer initially designed this parallel downloading to not be dependent on the database, so the developer feels confident in running\nmany parallel processes of `Downloader.download_all`.\n\nHowever, `Foo.download` might be subject to change.\n\n```ruby\nclass SomeModel \u003c ApplicationRecord\n  # ...\nend\n\nclass Foo\n  def self.download(id)\n    id = SomeModel.find(id).alias_id\n    Net::HTTP.get(URI(\"http://example.com/products/#{URI.encode(id)}\"))\n  end\nend\n```\n\nAfter this modification to `Foo.download`, each thread will checkout a new connection to the database. Which could\noverwhelm the database if there are many parallel processes executing `Downloader.download_all`. While this violates,\nthe initial assumption about `Downloader.download_all` using the database, it would have been useful to have a safeguard to prevent\nthis situation from occurring.\n\nThe error generated by setting `ActiveRecord::Base.forbid_implicit_connection_checkout_for_thread!` could have detected\n this situation during testing which should have failed with `ActiveRecord::ImplicitConnectionForbiddenError`. In the\n worst case, the production code running this would have failed with `ActiveRecord::ImplicitConnectionForbiddenError`,\n but it would have prevented the database from being overwhelmed and have protected the rest of the application.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then,\nrun `rake spec` to run the tests. You can also run `bin/console` for an\ninteractive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`.\n\nTo release a new version, update the version number in `version.rb`, and then\nrun `bundle exec rake release`, which will create a git tag for the version,\npush git commits and tags, and push the `.gem` file to\n[rubygems.org](https://rubygems.org)\n.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at\nhttps://github.com/salsify/activerecord-forbid_implicit_checkout.## License\n\nThe gem is available as open source under the terms of the\n[MIT License](http://opensource.org/licenses/MIT).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalsify%2Factiverecord-forbid_implicit_connection_checkout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsalsify%2Factiverecord-forbid_implicit_connection_checkout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalsify%2Factiverecord-forbid_implicit_connection_checkout/lists"}