diff --git a/lib/ruby_lsp/ruby_lsp_rails/server.rb b/lib/ruby_lsp/ruby_lsp_rails/server.rb index 1fc3b9f4..9daa56a8 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/server.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/server.rb @@ -294,6 +294,7 @@ def start request = JSON.parse(json, symbolize_names: true) execute(request.fetch(:method), request[:params]) + disconnect_from_database end end @@ -493,6 +494,18 @@ def clear_file_system_resolver_hooks end end + # Keeping a connection to the database prevents it from being dropped in development. We don't actually need to + # to reuse database connections for the LSP server, the performance benefit of doing so only matters in production + # where there is latency, locally we're fine with the small overhead of establishing a new connection on each request. + #: -> void + def disconnect_from_database + return unless defined?(::ActiveRecord::Base) + + with_notification_error_handling("disconnect_from_database") do + ActiveRecord::Base.connection_handler.clear_all_connections!(:all) + end + end + #: (singleton(ActiveRecord::Base)) -> Array[String] def collect_model_foreign_keys(model) return [] unless model.connection.respond_to?(:supports_foreign_keys?) &&