Skip to content

Commit

Permalink
Add Sequel::DatabaseConnectionError, for indicating that Sequel wasn'…
Browse files Browse the repository at this point in the history
…t able to connect to the database

This error is only raised by the connection pool if there is an error
calling the connection proc (or the connection proc returns nil).
Generally, it indicates that your database connection parameters are
wrong (bad host, bad username, bad password, etc.).

This commit also fixes a bug that would cause the created_count of
the connection pool to increase when there was an error raised during
the connection.  In practice, this didn't matter much as the
connection pool would be unlikely to have valid connections (at least
for the same server).  The reason it catches nil as well is that is
also what is returned by make_new if it is not able to get a valid
connection.  Before, Adapters that returned nil instead of raising an
error on a call to connect would cause a busy wait in the connection
pool.
  • Loading branch information
jeremyevans committed Jan 26, 2009
1 parent 5313fc6 commit 2aa33cd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD === HEAD


* Add Sequel::DatabaseConnectionError, for indicating that Sequel wasn't able to connect to the database (jeremyevans)

* Add validates_not_string validation, useful in conjunction with raise_on_typecast_failure = false (jeremyevans) * Add validates_not_string validation, useful in conjunction with raise_on_typecast_failure = false (jeremyevans)


* Don't modify Model#new? and Model#changed_columns when saving a record until after the after hooks have been run (tamas, jeremyevans) * Don't modify Model#new? and Model#changed_columns when saving a record until after the after hooks have been run (tamas, jeremyevans)
Expand Down
12 changes: 10 additions & 2 deletions lib/sequel_core/connection_pool.rb
Expand Up @@ -156,9 +156,17 @@ def available(server)
# the server is less than the maximum size of the pool. # the server is less than the maximum size of the pool.
def make_new(server) def make_new(server)
if @created_count[server] < @max_size if @created_count[server] < @max_size
raise(Sequel::Error, "No connection proc specified") unless @connection_proc
begin
conn = @connection_proc.call(server)
rescue Exception=>exception
e = Sequel::DatabaseConnectionError.new("#{exception.class} #{exception.message}")
e.set_backtrace(exception.backtrace)
raise e
end
raise(Sequel::DatabaseConnectionError, "Connection parameters not valid") unless conn
set_created_count(server, @created_count[server] + 1) set_created_count(server, @created_count[server] + 1)
@connection_proc ? @connection_proc.call(server) : \ conn
(raise Error, "No connection proc specified")
end end
end end


Expand Down
4 changes: 4 additions & 0 deletions lib/sequel_core/exceptions.rb
Expand Up @@ -31,6 +31,10 @@ class Rollback < Error ; end
# Generic error raised by the database adapters, indicating a # Generic error raised by the database adapters, indicating a
# problem originating from the database server. # problem originating from the database server.
class DatabaseError < Error; end class DatabaseError < Error; end

# Error raised when the Sequel is unable to connect to the database with the
# connection parameters it was given.
class DatabaseConnectionError < DatabaseError; end


# Error that should be raised by adapters when they determine that the connection # Error that should be raised by adapters when they determine that the connection
# to the database has been lost. Instructs the connection pool code to # to the database has been lost. Instructs the connection pool code to
Expand Down
14 changes: 14 additions & 0 deletions spec/sequel_core/connection_pool_spec.rb
Expand Up @@ -88,6 +88,20 @@
end end
end end


context "A connection pool handling connection errors" do
specify "#hold should raise a Sequel::DatabaseConnectionError if an exception is raised by the connection_proc" do
cpool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS){raise Interrupt}
proc{cpool.hold{:block_return}}.should raise_error(Sequel::DatabaseConnectionError)
cpool.created_count.should == 0
end

specify "#hold should raise a Sequel::DatabaseConnectionError if nil is returned by the connection_proc" do
cpool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS){nil}
proc{cpool.hold{:block_return}}.should raise_error(Sequel::DatabaseConnectionError)
cpool.created_count.should == 0
end
end

class DummyConnection class DummyConnection
@@value = 0 @@value = 0
def initialize def initialize
Expand Down

0 comments on commit 2aa33cd

Please sign in to comment.