Permalink
Browse files

Add Sequel::DatabaseConnectionError, for indicating that Sequel wasn'…

…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...
1 parent 5313fc6 commit 2aa33cd029d88d7758b18631ac9f1342122ae5fa @jeremyevans committed Jan 26, 2009
Showing with 30 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +10 −2 lib/sequel_core/connection_pool.rb
  3. +4 −0 lib/sequel_core/exceptions.rb
  4. +14 −0 spec/sequel_core/connection_pool_spec.rb
View
@@ -1,5 +1,7 @@
=== 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)
* Don't modify Model#new? and Model#changed_columns when saving a record until after the after hooks have been run (tamas, jeremyevans)
@@ -156,9 +156,17 @@ def available(server)
# the server is less than the maximum size of the pool.
def make_new(server)
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)
- @connection_proc ? @connection_proc.call(server) : \
- (raise Error, "No connection proc specified")
+ conn
end
end
@@ -31,6 +31,10 @@ class Rollback < Error ; end
# Generic error raised by the database adapters, indicating a
# problem originating from the database server.
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
# to the database has been lost. Instructs the connection pool code to
@@ -88,6 +88,20 @@
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
@@value = 0
def initialize

0 comments on commit 2aa33cd

Please sign in to comment.