Is there a better method for switching database in rails without exhausting the connection pool?

They’re all on the same server but switching is more nuanced. Firstly it helps to have a dummy database that’s empty but has an identical structure to use as the boot up database. Then there are a few things that are necessary to do when switching. This is my current code:

module ActiveRecord
  module ConnectionAdapters
    class Mysql2Adapter < AbstractMysqlAdapter
      # Allows us to connect to a new database in a clean way
      def change_database!(db)
        if @config[:database] != db
          @config.merge!(database: db)
          clear_query_cache
          reconnect!

          Current.reset
        end
      end

      def restore_default_database!
        database = YAML.unsafe_load_file("config/database.yml")[Rails.env]["primary"]["database"]
        change_database!(database)
      end
    end
  end
end

Works well, but it would be cool to have something more built-in one day.