kenn / standby

Read from standby databases for ActiveRecord
MIT License
87 stars 28 forks source link

Avoid new connections to the primary node. #35

Closed nicholasdower closed 11 months ago

nicholasdower commented 3 years ago

https://github.com/kenn/standby/issues/36

Standby::Base.inside_transaction? uses ActiveRecord::Base.connection on the primary to check whether access to the standby is occurring from within a transaction. ActiveRecord::Base.connection looks for a cached connection belonging to the current thread and, if it doesn't find one, attempts to checkout a connection from the connection pool. If the connection pool is exhausted, it blocks for up to ConnectionPool#checkout_timeout (default 5 seconds).

This change skips the transaction check if a connection has not been cached for the current thread. The assumption is there cannot be a current transaction without a current connection.

Also rename slavery_spec.rb to match renames done in d0e8c74019ef6278812dde662a8f27c897612c00.