Add connection retry logic to connection pool

This commit is contained in:
Brian J. Cardiff 2016-08-31 17:32:01 -03:00
parent dbf7c94ef4
commit 47e7d826e8
8 changed files with 102 additions and 9 deletions

View file

@ -63,4 +63,28 @@ describe DB::Database do
end
stmt.closed?.should be_true
end
it "should not reconnect if connection is lost and retry_attempts=0" do
DummyDriver::DummyConnection.clear_connections
DB.open "dummy://localhost:1027?initial_pool_size=1&max_pool_size=1&retry_attempts=0" do |db|
db.exec("stmt1")
DummyDriver::DummyConnection.connections.size.should eq(1)
DummyDriver::DummyConnection.connections.first.disconnect!
expect_raises DB::PoolRetryAttemptsExceeded do
db.exec("stmt1")
end
DummyDriver::DummyConnection.connections.size.should eq(1)
end
end
it "should reconnect if connection is lost and executing same statement" do
DummyDriver::DummyConnection.clear_connections
DB.open "dummy://localhost:1027?initial_pool_size=1&max_pool_size=1&retry_attempts=1" do |db|
db.exec("stmt1")
DummyDriver::DummyConnection.connections.size.should eq(1)
DummyDriver::DummyConnection.connections.first.disconnect!
db.exec("stmt1")
DummyDriver::DummyConnection.connections.size.should eq(2)
end
end
end

View file

@ -9,6 +9,7 @@ class DummyDriver < DB::Driver
class DummyConnection < DB::Connection
def initialize(db)
super(db)
@connected = true
@@connections ||= [] of DummyConnection
@@connections.not_nil! << self
end
@ -29,6 +30,14 @@ class DummyDriver < DB::Driver
0
end
def check
raise DB::ConnectionLost.new(self) unless @connected
end
def disconnect!
@connected = false
end
protected def do_close
super
end
@ -43,11 +52,13 @@ class DummyDriver < DB::Driver
end
protected def perform_query(args : Enumerable)
@connection.as(DummyConnection).check
set_params args
DummyResultSet.new self, @query
end
protected def perform_exec(args : Enumerable)
@connection.as(DummyConnection).check
set_params args
raise "forced exception due to query" if @query == "raise"
DB::ExecResult.new 0i64, 0_i64