Permalink
Browse files

Fix for jdbc connection problems using the JavaSQL::DriverManager und…

…er environments where driver auto-loading doesn't work. (Tomcat/Trinidad) The fix was lifted from the activerecord-jdbc-adapter.
  • Loading branch information...
1 parent 582ef63 commit c5b19a2ec44daa475e63191a0056c27cb8fee990 @elskwid elskwid committed Apr 26, 2010
Showing with 17 additions and 3 deletions.
  1. +17 −3 lib/sequel/adapters/jdbc.rb
@@ -106,6 +106,9 @@ class Database < Sequel::Database
# The type of database we are connecting to
attr_reader :database_type
+ # The Java database driver we are using
+ attr_reader :driver
+
# Whether to convert some Java types to ruby types when retrieving rows.
# True by default, can be set to false to roughly double performance when
# fetching rows.
@@ -123,7 +126,7 @@ def initialize(opts)
resolved_uri = jndi? ? get_uri_from_jndi : uri
if match = /\Ajdbc:([^:]+)/.match(resolved_uri) and prok = DATABASE_SETUP[match[1].to_sym]
- prok.call(self)
+ @driver = prok.call(self)
end
end
@@ -166,8 +169,19 @@ def connect(server)
else
args = [uri(opts)]
args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password]
- JavaSQL::DriverManager.getConnection(*args)
- end
+ begin
+ JavaSQL::DriverManager.getConnection(*args)
+ rescue
+ # If the DriverManager can't get the connection - use the connect
+ # method of the driver. (This happens under Tomcat for instance)
+ props = java.util.Properties.new
+ if opts && opts[:user] && opts[:password]
+ props.setProperty("user", opts[:user])
+ props.setProperty("password", opts[:pass])
+ end
+ driver.new.connect(args[0], props)
+ end
+ end
setup_connection(conn)
end

0 comments on commit c5b19a2

Please sign in to comment.