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 6e1fafc commit 416479e0fec997e98567eb92766d3a50f44cc404 @elskwid elskwid committed Apr 26, 2010
Showing with 16 additions and 3 deletions.
  1. +16 −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
@@ -163,9 +166,19 @@ def connect(server)
conn = if jndi?
get_connection_from_jndi
else
- args = [uri(server_opts(server))]
+ url = uri(server_opts(server))
+ args = [url]
args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password]
- JavaSQL::DriverManager.getConnection(*args)
+ 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)
+ properties = java.util.Properties.new
+ # properties.setProperty("user", opts[:user])
+ # properties.setProperty("password", opts[:pass])
+ driver.new.connect(url, properties)
+ end
end
setup_connection(conn)
end

0 comments on commit 416479e

Please sign in to comment.