Skip to content

Commit 5e5afe9

Browse files
authored
Close any database connections at the end of request (#661)
Close any database connections at the end of each request There is no need to reuse database connections for the LSP server, as the overhead of establishing new connections locally is negligible. Therefore, we can close any active database connections as soon as we're done with the request.
1 parent 9a52310 commit 5e5afe9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/ruby_lsp/ruby_lsp_rails/server.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def start
294294

295295
request = JSON.parse(json, symbolize_names: true)
296296
execute(request.fetch(:method), request[:params])
297+
disconnect_from_database
297298
end
298299
end
299300

@@ -493,6 +494,18 @@ def clear_file_system_resolver_hooks
493494
end
494495
end
495496

497+
# Keeping a connection to the database prevents it from being dropped in development. We don't actually need to
498+
# to reuse database connections for the LSP server, the performance benefit of doing so only matters in production
499+
# where there is latency, locally we're fine with the small overhead of establishing a new connection on each request.
500+
#: -> void
501+
def disconnect_from_database
502+
return unless defined?(::ActiveRecord::Base)
503+
504+
with_notification_error_handling("disconnect_from_database") do
505+
ActiveRecord::Base.connection_handler.clear_all_connections!(:all)
506+
end
507+
end
508+
496509
#: (singleton(ActiveRecord::Base)) -> Array[String]
497510
def collect_model_foreign_keys(model)
498511
return [] unless model.connection.respond_to?(:supports_foreign_keys?) &&

0 commit comments

Comments
 (0)