diff --git a/spec/database_spec.cr b/spec/database_spec.cr index 74373af..38f963b 100644 --- a/spec/database_spec.cr +++ b/spec/database_spec.cr @@ -179,6 +179,25 @@ describe DB::Database do end end + it "should not checkout multiple connections if there is a statement error" do + with_dummy "dummy://localhost:1027?initial_pool_size=1&max_pool_size=10&retry_attempts=10" do |db| + expect_raises DB::Error do + db.exec("syntax error") + end + DummyDriver::DummyConnection.connections.size.should eq(1) + end + end + + it "should attempt all retries if connection is lost" do + with_dummy "dummy://localhost:1027?initial_pool_size=1&max_pool_size=1&retry_attempts=10" do |db| + expect_raises DB::PoolRetryAttemptsExceeded do + db.exec("raise ConnectionLost") + end + # 1 initial + 10 retries + DummyDriver::DummyConnection.connections.size.should eq(11) + end + end + describe "prepared_statements connection option" do it "defaults to true" do with_dummy "dummy://localhost:1027" do |db| diff --git a/spec/dummy_driver.cr b/spec/dummy_driver.cr index 8d0f7e5..84ab5b7 100644 --- a/spec/dummy_driver.cr +++ b/spec/dummy_driver.cr @@ -142,6 +142,7 @@ class DummyDriver < DB::Driver super(connection, command) @@statements_count.add(1) raise DB::Error.new(command) if command == "syntax error" + raise DB::ConnectionLost.new(connection) if command == "raise ConnectionLost" end def self.statements_count diff --git a/src/db/pool.cr b/src/db/pool.cr index aeef2d6..378e303 100644 --- a/src/db/pool.cr +++ b/src/db/pool.cr @@ -206,8 +206,6 @@ module DB # if the connection is lost it will be closed by # the exception to release resources # we still need to remove it from the known pool. - # Closed connection will be evicted from statement cache - # in PoolPreparedStatement#clean_connections sync { delete(e.resource) } rescue e : PoolResourceRefused # a ConnectionRefused means a new connection