Skip to content

Commit adea17e

Browse files
committed
Bypass DriverManager for JDBC connections loaded at runtime
When the Oracle JDBC jar is added to the load path at runtime (rather than being on the system classpath), java.sql.DriverManager refuses to hand out connections with "No suitable driver". Fall back to calling ORACLE_DRIVER.connect directly with username/password properties so the connection still succeeds. The fallback is scoped narrowly: only Java::JavaSql::SQLException whose message matches /no suitable driver/i triggers it; any other SQLException (auth/network/SQL errors) re-raises so the original error surfaces unchanged. Apply the same change in spec/spec_helper.rb so the JRuby test harness behaves consistently.
1 parent dbf19f3 commit adea17e

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

lib/plsql/jdbc_connection.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
end
3636
end
3737

38-
java.sql.DriverManager.registerDriver Java::oracle.jdbc.OracleDriver.new
38+
ORACLE_DRIVER = Java::oracle.jdbc.OracleDriver.new
39+
java.sql.DriverManager.registerDriver ORACLE_DRIVER
3940

4041
# set tns_admin property from TNS_ADMIN environment variable
4142
if !java.lang.System.get_property("oracle.net.tns_admin") && ENV["TNS_ADMIN"]
@@ -51,7 +52,19 @@ module PLSQL
5152
class JDBCConnection < Connection # :nodoc:
5253
def self.create_raw(params)
5354
url = jdbc_connection_url(params)
54-
new(java.sql.DriverManager.getConnection(url, params[:username], params[:password]))
55+
conn = begin
56+
java.sql.DriverManager.getConnection(url, params[:username], params[:password])
57+
rescue Java::JavaSql::SQLException => e
58+
raise unless e.message =~ /no suitable driver/i
59+
# bypass DriverManager to work in cases where ojdbc*.jar
60+
# is added to the load path at runtime and not on the
61+
# system classpath
62+
ORACLE_DRIVER.connect(url, java.util.Properties.new.tap do |props|
63+
props.setProperty("user", params[:username])
64+
props.setProperty("password", params[:password])
65+
end)
66+
end
67+
new(conn)
5568
end
5669

5770
def self.jdbc_connection_url(params)

spec/spec_helper.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,18 @@ def get_connection(user_number = 0)
8888
end
8989
else
9090
try_to_connect(Java::JavaSql::SQLException) do
91-
java.sql.DriverManager.getConnection(get_connection_url, database_user, database_password)
91+
begin
92+
java.sql.DriverManager.getConnection(get_connection_url, database_user, database_password)
93+
rescue Java::JavaSql::SQLException => e
94+
raise unless e.message =~ /no suitable driver/i
95+
# bypass DriverManager to work in cases where ojdbc*.jar
96+
# is added to the load path at runtime and not on the
97+
# system classpath
98+
ORACLE_DRIVER.connect(get_connection_url, java.util.Properties.new.tap do |props|
99+
props.setProperty("user", database_user)
100+
props.setProperty("password", database_password)
101+
end)
102+
end
92103
end
93104
end
94105
end

0 commit comments

Comments
 (0)