Skip to content

Commit

Permalink
Fix for jdbc connection problems using the JavaSQL::DriverManager und…
Browse files Browse the repository at this point in the history
…er environments where driver auto-loading doesn't work. (Tomcat/Trinidad) The fix was lifted from the activerecord-jdbc-adapter.
  • Loading branch information
elskwid committed Apr 29, 2010
1 parent 582ef63 commit c5b19a2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/sequel/adapters/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c5b19a2

Please sign in to comment.