Skip to content

Commit

Permalink
Modify jdbc adapter exception handling to work around ::NativeExcepti…
Browse files Browse the repository at this point in the history
…on deprecation in JRuby 9.2

This creates a Sequel::JDBC::NativeException alias for
java.lang.Exception.  Because java.sql.SQLException is a subclass
of java.lang.Exception, we can chagne the rescue classes to remove
JavaSQL::SQLException.

While here, don't create a new array each time in
Database#database_error_classes, use a shared frozen constant,
moving the implementation from the jdbc/postgresql adapter to
the main jdbc adapter.
  • Loading branch information
jeremyevans committed May 25, 2018
1 parent 118d193 commit b4fab95
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== master

* Modify jdbc adapter exception handling to work around ::NativeException deprecation in JRuby 9.2 (jeremyevans)

* Work around broken BC date handling in JRuby 9.2.0.0 (jeremyevans)

* Switch use of BigDecimal.new() to BigDecimal(), since the former is deprecated (jeremyevans)
Expand Down
19 changes: 13 additions & 6 deletions lib/sequel/adapters/jdbc.rb
Expand Up @@ -17,7 +17,14 @@ module JavaSQL
# Contains procs keyed on subadapter type that extend the
# given database object so it supports the correct database type.
DATABASE_SETUP = {}

# Create custom NativeException alias for nicer access, and also so that
# JRuby 9.2+ so it doesn't use the deprecated ::NativeException
NativeException = java.lang.Exception

# Default database error classes
DATABASE_ERROR_CLASSES = [NativeException].freeze

# Allow loading the necessary JDBC support via a gem.
def self.load_gem(name)
begin
Expand Down Expand Up @@ -168,7 +175,7 @@ def call_sproc(name, opts = OPTS)
last_insert_id(conn, opts)
end
end
rescue NativeException, JavaSQL::SQLException => e
rescue NativeException => e
raise_error(e)
ensure
cps.close
Expand All @@ -189,7 +196,7 @@ def connect(server)
JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout]
raise StandardError, "skipping regular connection" if opts[:jdbc_properties]
JavaSQL::DriverManager.getConnection(*args)
rescue JavaSQL::SQLException, NativeException, StandardError => e
rescue NativeException, StandardError => e
raise e unless driver
# If the DriverManager can't get the connection - use the connect
# method of the driver. (This happens under Tomcat for instance)
Expand All @@ -203,7 +210,7 @@ def connect(server)
c = driver.new.connect(args[0], props)
raise(Sequel::DatabaseError, 'driver.new.connect returned nil: probably bad JDBC connection string') unless c
c
rescue JavaSQL::SQLException, NativeException, StandardError => e2
rescue NativeException, StandardError => e2
if e2.respond_to?(:message=) && e2.message != e.message
e2.message = "#{e2.message}\n#{e.class.name}: #{e.message}"
end
Expand Down Expand Up @@ -355,7 +362,7 @@ def cps_sync(conn, &block)
end

def database_error_classes
[NativeException]
DATABASE_ERROR_CLASSES
end

def database_exception_sqlstate(exception, opts)
Expand Down Expand Up @@ -436,7 +443,7 @@ def execute_prepared_statement(name, opts=OPTS)
log_connection_yield(msg, conn, args){cps.executeUpdate}
end
end
rescue NativeException, JavaSQL::SQLException => e
rescue NativeException => e
raise_error(e)
ensure
cps.close unless name
Expand Down Expand Up @@ -661,7 +668,7 @@ def setup_type_convertor_map_early
def statement(conn)
stmt = conn.createStatement
yield stmt
rescue NativeException, JavaSQL::SQLException => e
rescue NativeException => e
raise_error(e)
ensure
stmt.close if stmt
Expand Down
5 changes: 0 additions & 5 deletions lib/sequel/adapters/jdbc/postgresql.rb
Expand Up @@ -135,11 +135,6 @@ def oid_convertor_proc(oid)

private

DATABASE_ERROR_CLASSES = [NativeException].freeze
def database_error_classes
DATABASE_ERROR_CLASSES
end

def disconnect_error?(exception, opts)
super || exception.message =~ /\A(This connection has been closed\.|FATAL: terminating connection due to administrator command|An I\/O error occurred while sending to the backend\.)\z/
end
Expand Down

0 comments on commit b4fab95

Please sign in to comment.