Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit c5b19a2ec44daa475e63191a0056c27cb8fee990 1 parent 582ef63
Don Morrison authored April 25, 2010

Showing 1 changed file with 17 additions and 3 deletions. Show diff stats Hide diff stats

  1. 20  lib/sequel/adapters/jdbc.rb
20  lib/sequel/adapters/jdbc.rb
@@ -106,6 +106,9 @@ class Database < Sequel::Database
106 106
       # The type of database we are connecting to
107 107
       attr_reader :database_type
108 108
       
  109
+      # The Java database driver we are using
  110
+      attr_reader :driver
  111
+      
109 112
       # Whether to convert some Java types to ruby types when retrieving rows.
110 113
       # True by default, can be set to false to roughly double performance when
111 114
       # fetching rows.
@@ -123,7 +126,7 @@ def initialize(opts)
123 126
         resolved_uri = jndi? ? get_uri_from_jndi : uri
124 127
 
125 128
         if match = /\Ajdbc:([^:]+)/.match(resolved_uri) and prok = DATABASE_SETUP[match[1].to_sym]
126  
-          prok.call(self)
  129
+          @driver = prok.call(self)
127 130
         end        
128 131
       end
129 132
       
@@ -166,8 +169,19 @@ def connect(server)
166 169
         else
167 170
           args = [uri(opts)]
168 171
           args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password]
169  
-          JavaSQL::DriverManager.getConnection(*args)
170  
-        end       
  172
+          begin
  173
+            JavaSQL::DriverManager.getConnection(*args)
  174
+          rescue
  175
+            # If the DriverManager can't get the connection - use the connect
  176
+            # method of the driver. (This happens under Tomcat for instance)
  177
+            props = java.util.Properties.new
  178
+            if opts && opts[:user] && opts[:password]
  179
+              props.setProperty("user", opts[:user])
  180
+              props.setProperty("password", opts[:pass])
  181
+            end
  182
+            driver.new.connect(args[0], props)
  183
+          end
  184
+        end
171 185
         setup_connection(conn)
172 186
       end
173 187
       

0 notes on commit c5b19a2

Please sign in to comment.
Something went wrong with that request. Please try again.