Skip to content

Commit f231661

Browse files
yahondaclaude
andcommitted
Bypass DriverManager for JDBC connections loaded at runtime
When ojdbc*.jar is added to the load path at runtime (via require) rather than being on the system classpath, DriverManager.getConnection fails with "No suitable driver found" due to classloader isolation in JRuby. Fall back to using ORACLE_DRIVER.connect directly, matching the same workaround used in oracle_enhanced adapter. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3de277a commit f231661

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

lib/plsql/jdbc_connection.rb

Lines changed: 14 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,18 @@ 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
58+
# bypass DriverManager to work in cases where ojdbc*.jar
59+
# is added to the load path at runtime and not on the
60+
# system classpath
61+
ORACLE_DRIVER.connect(url, java.util.Properties.new.tap do |props|
62+
props.setProperty("user", params[:username])
63+
props.setProperty("password", params[:password])
64+
end)
65+
end
66+
new(conn)
5567
end
5668

5769
def self.jdbc_connection_url(params)

spec/spec_helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,17 @@ 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
94+
# bypass DriverManager to work in cases where ojdbc*.jar
95+
# is added to the load path at runtime and not on the
96+
# system classpath
97+
ORACLE_DRIVER.connect(get_connection_url, java.util.Properties.new.tap do |props|
98+
props.setProperty("user", database_user)
99+
props.setProperty("password", database_password)
100+
end)
101+
end
92102
end
93103
end
94104
end

0 commit comments

Comments
 (0)