Method: Sequel::ShardedThreadedConnectionPool#hold

Defined in:
lib/sequel/connection_pool/sharded_threaded.rb

#hold(server = :default) ⇒ Object

Chooses the first available connection to the given server, or if none are available, creates a new connection. Passes the connection to the supplied block:

pool.hold(:server1) {|conn| conn.execute('DROP TABLE posts')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is immediately available and the pool is already using the maximum number of connections, Pool#hold will block until a connection is available or the timeout expires. If the timeout expires before a connection can be acquired, a Sequel::PoolTimeout is raised.

[View source]

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sequel/connection_pool/sharded_threaded.rb', line 124

def hold(server=:default)
  server = pick_server(server)
  t = Sequel.current
  if conn = owned_connection(t, server)
    return yield(conn)
  end
  begin
    conn = acquire(t, server)
    yield conn
  rescue Sequel::DatabaseDisconnectError, *@error_classes => e
    sync{@connections_to_remove << conn} if conn && disconnect_error?(e)
    raise
  ensure
    sync{release(t, conn, server)} if conn
    while dconn = sync{@connections_to_disconnect.shift}
      disconnect_connection(dconn)
    end
  end
end