Skip to content

Commit

Permalink
Add support for an :after_connect option when connection, called with…
Browse files Browse the repository at this point in the history
… each new connection made

This is useful for customizations you want set on every connection
that Sequel doesn't already support.  For example, on PostgreSQL
if you wanted to set the schema_search_path on every connection:

  DB = Sequel.postgres('dbname', :after_connect=>(proc do |conn|
     conn.execute('SET search_path TO schema1,schema2')
   end))
  • Loading branch information
jeremyevans committed Apr 8, 2010
1 parent 789b707 commit df6f285
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
@@ -1,6 +1,8 @@
=== HEAD

* Add support for :test option to Database to be automatically test the connection (jeremyevans)
* Add support for an :after_connect option when connection, called with each new connection made (jeremyevans)

* Add support for a :test option when connecting to be automatically test the connection (jeremyevans)

* Add Dataset#select_append, which always appends to the existing SELECTed columns (jeremyevans)

Expand Down
1 change: 1 addition & 0 deletions doc/opening_databases.rdoc
Expand Up @@ -73,6 +73,7 @@ These options are shared by all adapters unless otherwise noted.

The following options can be specified and are passed to the the database's internal connection pool.

* :after_connect - A proc called after a new connection is made, with the connection object (default: nil)
* :max_connections - The maximum size of the connection pool (default: 4 connections on most databases)
* :pool_sleep_time - The number of seconds to sleep before trying to acquire a connection again (default: 0.001 seconds)
* :pool_timeout - The number of seconds to wait if a connection cannot be acquired before raising an error (default: 5 seconds)
Expand Down
7 changes: 6 additions & 1 deletion lib/sequel/connection_pool.rb
Expand Up @@ -60,12 +60,16 @@ def connection_pool_class(opts)
# Instantiates a connection pool with the given options. The block is called
# with a single symbol (specifying the server/shard to use) every time a new
# connection is needed. The following options are respected for all connection
# pools: #
# pools:
# * :after_connect - The proc called after each new connection is made, with the
# connection object, useful for customizations that you want to apply to all
# connections.
# * :disconnection_proc - The proc called when removing connections from the pool,
# which is passed the connection to disconnect.
def initialize(opts={}, &block)
raise(Sequel::Error, "No connection proc specified") unless @connection_proc = block
@disconnection_proc = opts[:disconnection_proc]
@after_connect = opts[:after_connect]
end

# Alias for size, not aliased directly for ease of subclass implementation
Expand All @@ -85,6 +89,7 @@ def servers
def make_new(server)
begin
conn = @connection_proc.call(server)
@after_connect.call(conn) if @after_connect
rescue Exception=>exception
raise Sequel.convert_exception_class(exception, Sequel::DatabaseConnectionError)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/core/connection_pool_spec.rb
Expand Up @@ -746,6 +746,12 @@ def value
x.should == [:default, :default]
end

specify "should have respect an :after_connect proc that is called with each newly created connection" do
x = nil
@class.new(:after_connect=>proc{|c| x = [c, c]}){|c| 123}.hold{}
x.should == [123, 123]
end

specify "should raise a DatabaseConnectionError if the connection raises an exception" do
proc{@class.new({}){|c| raise Exception}.hold{}}.should raise_error(Sequel::DatabaseConnectionError)
end
Expand Down

0 comments on commit df6f285

Please sign in to comment.