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
63 def self.extended(pool)
64   pool.instance_exec do
65     sync do
66       @connection_timestamps ||= {}
67       @connection_validation_timeout ||= 3600
68     end
69   end
70 
71   # Make sure the valid connection SQL query is precached,
72   # otherwise it's possible it will happen at runtime. While
73   # it should work correctly at runtime, it's better to avoid
74   # the possibility of failure altogether.
75   pool.db.send(:valid_connection_sql)
76 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
 97 def acquire(*a)
 98   conn = nil
 99 
100   1.times do
101     if (conn = super) &&
102        (timer = sync{@connection_timestamps.delete(conn)}) &&
103        Sequel.elapsed_seconds_since(timer) > @connection_validation_timeout &&
104        !db.valid_connection?(conn)
105 
106       if pool_type == :sharded_threaded
107         sync{allocated(a.last).delete(Thread.current)}
108       else
109         sync{@allocated.delete(Thread.current)}
110       end
111 
112       disconnect_connection(conn)
113       redo
114     end
115   end
116 
117   conn
118 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
81 def checkin_connection(*)
82   conn = super
83   @connection_timestamps[conn] = Sequel.start_timer
84   conn
85 end
disconnect_connection(conn) click to toggle source

Clean up timestamps during disconnect.

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