use of org.apache.spark.sql.execution.datasources.jdbc.DriverWrapper in project hudi by apache.
the class UtilHelpers method createConnectionFactory.
/**
* Returns a factory for creating connections to the given JDBC URL.
*
* @param options - JDBC options that contains url, table and other information.
* @return
* @throws SQLException if the driver could not open a JDBC connection.
*/
private static Connection createConnectionFactory(Map<String, String> options) throws SQLException {
String driverClass = options.get(JDBCOptions.JDBC_DRIVER_CLASS());
DriverRegistry.register(driverClass);
Enumeration<Driver> drivers = DriverManager.getDrivers();
Driver driver = null;
while (drivers.hasMoreElements()) {
Driver d = drivers.nextElement();
if (d instanceof DriverWrapper) {
if (((DriverWrapper) d).wrapped().getClass().getCanonicalName().equals(driverClass)) {
driver = d;
}
} else if (d.getClass().getCanonicalName().equals(driverClass)) {
driver = d;
}
if (driver != null) {
break;
}
}
Objects.requireNonNull(driver, String.format("Did not find registered driver with class %s", driverClass));
Properties properties = new Properties();
properties.putAll(options);
Connection connect;
String url = options.get(JDBCOptions.JDBC_URL());
connect = driver.connect(url, properties);
Objects.requireNonNull(connect, String.format("The driver could not open a JDBC connection. Check the URL: %s", url));
return connect;
}
Aggregations