module Sequel::ConnectionValidator

Attributes

connection_validation_timeout[RW]

The number of seconds that need to pass since connection checkin before attempting to validate the connection when checking it out from the pool. Defaults to 3600 seconds (1 hour).

Public Class Methods

extended(pool) click to toggle source

Initialize the data structures used by this extension.

   # File lib/sequel/extensions/connection_validator.rb
62 def self.extended(pool)
63   pool.instance_exec do
64     sync do
65       @connection_timestamps ||= {}
66       @connection_validation_timeout ||= 3600
67     end
68   end
69 
70   # Make sure the valid connection SQL query is precached,
71   # otherwise it's possible it will happen at runtime. While
72   # it should work correctly at runtime, it's better to avoid
73   # the possibility of failure altogether.
74   pool.db.send(:valid_connection_sql)
75 end

Private Instance Methods

acquire(*a) click to toggle source

When acquiring a connection, if it has been idle for longer than the connection validation timeout, test the connection for validity. If it is not valid, disconnect the connection, and retry with a new connection.

Calls superclass method
    # File lib/sequel/extensions/connection_validator.rb
 96 def acquire(*a)
 97   begin
 98     if (conn = super) &&
 99        (timer = sync{@connection_timestamps.delete(conn)}) &&
100        Sequel.elapsed_seconds_since(timer) > @connection_validation_timeout &&
101        !db.valid_connection?(conn)
102 
103       if pool_type == :sharded_threaded
104         sync{allocated(a.last).delete(Thread.current)}
105       else
106         sync{@allocated.delete(Thread.current)}
107       end
108 
109       disconnect_connection(conn)
110       raise Retry
111     end
112   rescue Retry
113     retry
114   end
115 
116   conn
117 end
checkin_connection(*) click to toggle source

Record the time the connection was checked back into the pool.

Calls superclass method
   # File lib/sequel/extensions/connection_validator.rb
80 def checkin_connection(*)
81   conn = super
82   @connection_timestamps[conn] = Sequel.start_timer
83   conn
84 end
disconnect_connection(conn) click to toggle source

Clean up timestamps during disconnect.

Calls superclass method
   # File lib/sequel/extensions/connection_validator.rb
87 def disconnect_connection(conn)
88   sync{@connection_timestamps.delete(conn)}
89   super
90 end