Skip to content

Commit 2e65ff5

Browse files
committed
Namespace ORACLE_DRIVER and polish JDBC loader error handling
Move ORACLE_DRIVER and the DriverManager.registerDriver call inside class PLSQL::JDBCConnection so the driver instance is no longer a top-level Ruby constant that pollutes the host application's namespace. Update the spec test harness to reference it via its fully qualified name PLSQL::JDBCConnection::ORACLE_DRIVER. Polish the loader's error reporting: - Hoist `ojdbc_jars = []` above the begin block so the `rescue LoadError` interpolation never references an unbound local even when `require "java"` itself fails. - When the Oracle JDBC jar isn't on the classpath (NameError on Java::oracle.jdbc.OracleDriver), raise a LoadError that hints at installing an Oracle JDBC driver such as ojdbc17.jar and points at https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html
1 parent 819434d commit 2e65ff5

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

lib/plsql/jdbc_connection.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ojdbc_jars = []
2+
13
begin
24
require "java"
35
require "jruby"
@@ -11,7 +13,6 @@
1113
java_version.to_i
1214
end
1315

14-
ojdbc_jars = []
1516
ojdbc_jars << "ojdbc17.jar" if java_major >= 17
1617
ojdbc_jars << "ojdbc11.jar" if java_major >= 11
1718
ojdbc_jars << "ojdbc8.jar" if java_major >= 8
@@ -35,21 +36,27 @@
3536
end
3637
end
3738

38-
ORACLE_DRIVER = Java::oracle.jdbc.OracleDriver.new
39-
java.sql.DriverManager.registerDriver ORACLE_DRIVER
40-
4139
# set tns_admin property from TNS_ADMIN environment variable
4240
if !java.lang.System.get_property("oracle.net.tns_admin") && ENV["TNS_ADMIN"]
4341
java.lang.System.set_property("oracle.net.tns_admin", ENV["TNS_ADMIN"])
4442
end
4543

46-
rescue LoadError, NameError
44+
rescue LoadError
4745
# JDBC driver is unavailable.
4846
raise LoadError, "ERROR: ruby-plsql could not load Oracle JDBC driver. Please install #{ojdbc_jars.empty? ? "Oracle JDBC" : ojdbc_jars.join(' or ') } library."
4947
end
5048

5149
module PLSQL
5250
class JDBCConnection < Connection # :nodoc:
51+
begin
52+
ORACLE_DRIVER = Java::oracle.jdbc.OracleDriver.new
53+
java.sql.DriverManager.registerDriver ORACLE_DRIVER
54+
rescue NameError
55+
raise LoadError, "ERROR: ruby-plsql could not load Oracle JDBC driver. " \
56+
"Please install Oracle JDBC driver like ojdbc17.jar. " \
57+
"See https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html"
58+
end
59+
5360
def self.create_raw(params)
5461
url = jdbc_connection_url(params)
5562
conn = begin

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def get_connection(user_number = 0)
9595
# bypass DriverManager to work in cases where ojdbc*.jar
9696
# is added to the load path at runtime and not on the
9797
# system classpath
98-
ORACLE_DRIVER.connect(get_connection_url, java.util.Properties.new.tap do |props|
98+
PLSQL::JDBCConnection::ORACLE_DRIVER.connect(get_connection_url, java.util.Properties.new.tap do |props|
9999
props.setProperty("user", database_user)
100100
props.setProperty("password", database_password)
101101
end)

0 commit comments

Comments
 (0)