diff --git a/lib/activerecord-jdbcsqlserver-adapter.rb b/lib/activerecord-jdbcsqlserver-adapter.rb index bc034c800..b5fad00c3 100644 --- a/lib/activerecord-jdbcsqlserver-adapter.rb +++ b/lib/activerecord-jdbcsqlserver-adapter.rb @@ -5,7 +5,7 @@ require 'active_record/log_subscriber' # Need to make sure this is loaded before we load Core for monkey patching # Load the jar file for the jdbc driver -require 'jdbc/mssql' +require_relative './jdbc_mssql_driver_loader' # Standadard arjdbc functionality require 'arjdbc/abstract/connection_management' diff --git a/lib/jdbc_mssql_driver_loader.rb b/lib/jdbc_mssql_driver_loader.rb new file mode 100644 index 000000000..b63f38b6f --- /dev/null +++ b/lib/jdbc_mssql_driver_loader.rb @@ -0,0 +1,22 @@ +module JdbcMssqlDriverLoader + def self.check_and_maybe_load_driver + driver_name = "com.microsoft.sqlserver.jdbc.SQLServerDriver" + if (Java::JavaClass.for_name(driver_name) rescue nil) + driver = Java::ComMicrosoftSqlserverJdbc::SQLServerDriver.new + which = driver + .getClass().getClassLoader().loadClass(driver_name) + .getProtectionDomain().getCodeSource().getLocation().to_s + warn "You already required a mssql jdbc driver (#{which}), skipping gem jdbc-mssql" + + major_version = driver.major_version + required_major_version = 8 + if major_version < required_major_version + raise "MSSQL jdbc driver version is to old (given major version #{major_version} < required major version #{required_major_version})" + end + else + require "jdbc/mssql" + end + end + + check_and_maybe_load_driver +end