From 189638e58faccbc9999f611fffe8c924cf5ae659 Mon Sep 17 00:00:00 2001 From: Petrik Date: Thu, 26 Mar 2026 18:31:03 +0100 Subject: [PATCH] Rails: Use a connection pool for the database connection --- frameworks/rails/app/controllers/benchmark_controller.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/frameworks/rails/app/controllers/benchmark_controller.rb b/frameworks/rails/app/controllers/benchmark_controller.rb index d7eee5f0..3b03a466 100644 --- a/frameworks/rails/app/controllers/benchmark_controller.rb +++ b/frameworks/rails/app/controllers/benchmark_controller.rb @@ -88,8 +88,9 @@ def db min_val = (params[:min] || 10).to_f max_val = (params[:max] || 50).to_f - conn = get_db - rows = conn.execute(DB_QUERY, [min_val, max_val]) + rows = connection_pool.with do |conn| + conn.execute(DB_QUERY, [min_val, max_val]) + end items = rows.map do |r| { 'id' => r['id'], 'name' => r['name'], 'category' => r['category'], @@ -114,8 +115,8 @@ def not_found private - def get_db - Thread.current[:rails_db] ||= begin + def connection_pool + $connection_pool ||= ConnectionPool.new(size: 4, timeout: 5) do db = SQLite3::Database.new('/data/benchmark.db', readonly: true) db.execute('PRAGMA mmap_size=268435456') db.results_as_hash = true