Search in sources :

Example 1 with SQLNestedException

use of org.apache.commons.dbcp.SQLNestedException in project oozie by apache.

the class BasicDataSourceWrapper method createConnectionFactory.

/**
 * Fixing a bug within {@link BasicDataSource#createConnectionFactory()} for {@code driverClassName} to have real effect.
 * <p>
 * Because we use currently Apache Commons DBCP 1.4.0 that has a bug not considering {@code driverClassName}, thus, we're unable
 * to create a JDBC driver using a user-provided driver class name (we try to do that by setting explicitly a value for
 * {@code openJpa.connectionProperties="DriverClassName=..."}), unless we perform the exact same fix that is applied by the DBCP
 * patch.
 * <p>
 * Note: when DBCP 1.4.1 will be released, and Oozie will update to 1.4.1, we can remove this class.
 * <p>
 * Please see <a href="https://issues.apache.org/jira/browse/DBCP-333">the DBCP bug</a> and
 * <a href="https://github.com/apache/commons-dbcp/blob/DBCP_1_4_x_BRANCH/src/java/org/apache/commons/dbcp/BasicDataSource.java#L1588-L1660">
 * the fixed method </a>
 * for details.
 * <p>
 * Please also see how OpenJPA
 * <a href="http://openjpa.apache.org/builds/2.2.1/apache-openjpa/docs/ref_guide_integration_dbcp.html"> is integrated</a>
 * with DBCP.
 */
protected ConnectionFactory createConnectionFactory() throws SQLException {
    // Load the JDBC driver class
    Class driverFromCCL = null;
    if (driverClassName != null) {
        try {
            try {
                if (driverClassLoader == null) {
                    driverFromCCL = Class.forName(driverClassName);
                } else {
                    driverFromCCL = Class.forName(driverClassName, true, driverClassLoader);
                }
            } catch (ClassNotFoundException cnfe) {
                driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName);
            }
        } catch (Throwable t) {
            String message = "Cannot load JDBC driver class '" + driverClassName + "'";
            logWriter.println(message);
            t.printStackTrace(logWriter);
            throw new SQLNestedException(message, t);
        }
    }
    // Create a JDBC driver instance
    Driver driver = null;
    try {
        if (driverFromCCL == null) {
            driver = DriverManager.getDriver(url);
        } else {
            // Usage of DriverManager is not possible, as it does not
            // respect the ContextClassLoader
            driver = (Driver) driverFromCCL.newInstance();
            if (!driver.acceptsURL(url)) {
                throw new SQLException("No suitable driver", "08001");
            }
        }
    } catch (Throwable t) {
        String message = "Cannot create JDBC driver of class '" + (driverClassName != null ? driverClassName : "") + "' for connect URL '" + url + "'";
        logWriter.println(message);
        t.printStackTrace(logWriter);
        throw new SQLNestedException(message, t);
    }
    // Can't test without a validationQuery
    if (validationQuery == null) {
        setTestOnBorrow(false);
        setTestOnReturn(false);
        setTestWhileIdle(false);
    }
    // Set up the driver connection factory we will use
    String user = username;
    if (user != null) {
        connectionProperties.put("user", user);
    } else {
        log("DBCP DataSource configured without a 'username'");
    }
    String pwd = password;
    if (pwd != null) {
        connectionProperties.put("password", pwd);
    } else {
        log("DBCP DataSource configured without a 'password'");
    }
    ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, url, connectionProperties);
    return driverConnectionFactory;
}
Also used : SQLNestedException(org.apache.commons.dbcp.SQLNestedException) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) DriverConnectionFactory(org.apache.commons.dbcp.DriverConnectionFactory) SQLException(java.sql.SQLException) Driver(java.sql.Driver) DriverConnectionFactory(org.apache.commons.dbcp.DriverConnectionFactory)

Example 2 with SQLNestedException

use of org.apache.commons.dbcp.SQLNestedException in project symmetric-ds by JumpMind.

the class ResettableBasicDataSource method createPoolableConnectionFactory.

@Override
protected void createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory, KeyedObjectPoolFactory statementPoolFactory, AbandonedConfig configuration) throws SQLException {
    PoolableConnectionFactory connectionFactory = null;
    try {
        connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, connectionPool, statementPoolFactory, validationQuery, validationQueryTimeout, connectionInitSqls, defaultReadOnly, defaultAutoCommit, defaultTransactionIsolation, defaultCatalog, configuration);
        validateConnectionFactory(connectionFactory);
    } catch (Exception e) {
        try {
            connectionPool.close();
        } catch (Exception e1) {
        }
        throw new SQLNestedException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }
}
Also used : SQLNestedException(org.apache.commons.dbcp.SQLNestedException) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) SQLException(java.sql.SQLException) SQLNestedException(org.apache.commons.dbcp.SQLNestedException)

Aggregations

SQLException (java.sql.SQLException)2 SQLNestedException (org.apache.commons.dbcp.SQLNestedException)2 Driver (java.sql.Driver)1 ConnectionFactory (org.apache.commons.dbcp.ConnectionFactory)1 DriverConnectionFactory (org.apache.commons.dbcp.DriverConnectionFactory)1 PoolableConnectionFactory (org.apache.commons.dbcp.PoolableConnectionFactory)1