1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# frozen-string-literal: true
# This is the fastest connection pool, since it isn't a connection pool at all.
# It is just a wrapper around a single connection that uses the connection pool
# API.
class Sequel::SingleConnectionPool < Sequel::ConnectionPool
def initialize(db, opts=OPTS)
super
@conn = []
end
# Yield the connection if one has been made.
def all_connections
yield @conn.first unless @conn.empty?
end
# Disconnect the connection from the database.
def disconnect(opts=nil)
return unless c = @conn.first
disconnect_connection(c)
@conn.clear
nil
end
# Yield the connection to the block.
def hold(server=nil)
unless c = @conn.first
@conn.replace([c = make_new(:default)])
end
yield c
rescue Sequel::DatabaseDisconnectError, *@error_classes => e
disconnect if disconnect_error?(e)
raise
end
# The SingleConnectionPool always has a maximum size of 1.
def max_size
1
end
def pool_type
:single
end
# The SingleConnectionPool always has a size of 1 if connected
# and 0 if not.
def size
@conn.empty? ? 0 : 1
end
private
# Make sure there is a valid connection.
def preconnect(concurrent = nil)
hold{}
end
end
|