Search in sources :

Example 1 with JDBCDriverShim

use of io.cdap.cdap.spi.data.sql.jdbc.JDBCDriverShim in project cdap by caskdata.

the class PostgreSqlStorageProvider method loadJDBCDriver.

private static void loadJDBCDriver(CConfiguration cConf, String storageImpl) {
    String driverExtensionPath = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_DRIVER_DIRECTORY);
    String driverName = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_JDBC_DRIVER_NAME);
    if (driverExtensionPath == null || driverName == null) {
        throw new IllegalArgumentException("The JDBC driver directory and driver name must be specified.");
    }
    File driverExtensionDir = new File(driverExtensionPath, storageImpl);
    if (!driverExtensionDir.exists()) {
        throw new IllegalArgumentException("The JDBC driver driver " + driverExtensionDir + " does not exist.");
    }
    // Create a separate classloader for the JDBC driver, which doesn't have any CDAP dependencies in it.
    ClassLoader driverClassLoader = new DirectoryClassLoader(driverExtensionDir, null);
    try {
        Driver driver = (Driver) Class.forName(driverName, true, driverClassLoader).newInstance();
        // wrap the driver class and register it ourselves since the driver manager will not use driver from other
        // classloader
        JDBCDriverShim driverShim = new JDBCDriverShim(driver);
        DriverManager.registerDriver(driverShim);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
        throw Throwables.propagate(e);
    }
    LOG.info("Successfully loaded {} from {}", driverName, driverExtensionPath);
}
Also used : DirectoryClassLoader(io.cdap.cdap.common.lang.DirectoryClassLoader) JDBCDriverShim(io.cdap.cdap.spi.data.sql.jdbc.JDBCDriverShim) SQLException(java.sql.SQLException) DirectoryClassLoader(io.cdap.cdap.common.lang.DirectoryClassLoader) Driver(java.sql.Driver) File(java.io.File)

Example 2 with JDBCDriverShim

use of io.cdap.cdap.spi.data.sql.jdbc.JDBCDriverShim in project cdap by cdapio.

the class PostgreSqlStorageProvider method loadJDBCDriver.

private static void loadJDBCDriver(CConfiguration cConf, String storageImpl) {
    String driverExtensionPath = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_DRIVER_DIRECTORY);
    String driverName = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_JDBC_DRIVER_NAME);
    if (driverExtensionPath == null || driverName == null) {
        throw new IllegalArgumentException("The JDBC driver directory and driver name must be specified.");
    }
    File driverExtensionDir = new File(driverExtensionPath, storageImpl);
    if (!driverExtensionDir.exists()) {
        throw new IllegalArgumentException("The JDBC driver driver " + driverExtensionDir + " does not exist.");
    }
    // Create a separate classloader for the JDBC driver, which doesn't have any CDAP dependencies in it.
    ClassLoader driverClassLoader = new DirectoryClassLoader(driverExtensionDir, null);
    try {
        Driver driver = (Driver) Class.forName(driverName, true, driverClassLoader).newInstance();
        // wrap the driver class and register it ourselves since the driver manager will not use driver from other
        // classloader
        JDBCDriverShim driverShim = new JDBCDriverShim(driver);
        DriverManager.registerDriver(driverShim);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
        throw Throwables.propagate(e);
    }
    LOG.info("Successfully loaded {} from {}", driverName, driverExtensionPath);
}
Also used : DirectoryClassLoader(io.cdap.cdap.common.lang.DirectoryClassLoader) JDBCDriverShim(io.cdap.cdap.spi.data.sql.jdbc.JDBCDriverShim) SQLException(java.sql.SQLException) DirectoryClassLoader(io.cdap.cdap.common.lang.DirectoryClassLoader) Driver(java.sql.Driver) File(java.io.File)

Aggregations

DirectoryClassLoader (io.cdap.cdap.common.lang.DirectoryClassLoader)2 JDBCDriverShim (io.cdap.cdap.spi.data.sql.jdbc.JDBCDriverShim)2 File (java.io.File)2 Driver (java.sql.Driver)2 SQLException (java.sql.SQLException)2