Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle DriverWrapper when scanning registered drivers in JDBWrapper.getConnector() #147

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,23 @@ private[redshift] class JDBCWrapper {
val subprotocol = url.stripPrefix("jdbc:").split(":")(0)
val driverClass: String = getDriverClass(subprotocol, userProvidedDriverClass)
registerDriver(driverClass)
val driverWrapperClass: Class[_] = if (SPARK_VERSION.startsWith("1.4")) {
Utils.classForName("org.apache.spark.sql.jdbc.package$DriverWrapper")
} else { // Spark 1.5.0+
Utils.classForName("org.apache.spark.sql.execution.datasources.jdbc.DriverWrapper")
}
def getWrapped(d: Driver): Driver = {
require(driverWrapperClass.isAssignableFrom(d.getClass))
driverWrapperClass.getDeclaredMethod("wrapped").invoke(d).asInstanceOf[Driver]
}
// Note that we purposely don't call DriverManager.getConnection() here: we want to ensure
// that an explicitly-specified user-provided driver class can take precedence over the default
// class, but DriverManager.getConnection() might return a according to a different precedence.
// At the same time, we don't want to create a driver-per-connection, so we use the
// DriverManager's driver instances to handle that singleton logic for us.
val driver: Driver = DriverManager.getDrivers.asScala.collectFirst {
case d if driverWrapperClass.isAssignableFrom(d.getClass)
&& getWrapped(d).getClass.getCanonicalName == driverClass => d
case d if d.getClass.getCanonicalName == driverClass => d
}.getOrElse {
throw new IllegalArgumentException(s"Did not find registered driver with class $driverClass")
Expand Down