Example usage for org.apache.commons.dbcp SQLNestedException SQLNestedException

List of usage examples for org.apache.commons.dbcp SQLNestedException SQLNestedException

Introduction

In this page you can find the example usage for org.apache.commons.dbcp SQLNestedException SQLNestedException.

Prototype

public SQLNestedException(String msg, Throwable cause) 

Source Link

Document

Constructs a new SQLNestedException with specified detail message and nested Throwable.

Usage

From source file:org.agnitas.dao.LoggingEnhBasicDataSource.java

/**
  * <p>Create (if necessary) and return the internal data source we are
  * using to manage our connections.</p>
  */* w w w  .jav  a  2s . c o  m*/
  * <p><strong>IMPLEMENTATION NOTE</strong> - It is tempting to use the
  * "double checked locking" idiom in an attempt to avoid synchronizing
  * on every single call to this method.  However, this idiom fails to
  * work correctly in the face of some optimizations that are legal for
  * a JVM to perform.</p>
  *
  * @exception SQLException if the object pool cannot be created.
  */
protected synchronized DataSource createDataSource() throws SQLException {

    // Return the pool if we have already created it
    if (dataSource != null) {
        return (dataSource);
    }

    logger.error("-------------------------------------------- Create new datasource");
    // Load the JDBC driver class
    if (driverClassName != null) {
        try {
            Class.forName(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 {
        driver = DriverManager.getDriver(url);
    } 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);
    }

    // Create an object pool to contain our active connections
    connectionPool = new LoggingObjectPool();
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxWait(maxWait);
    connectionPool.setTestOnBorrow(testOnBorrow);
    connectionPool.setTestOnReturn(testOnReturn);
    connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    connectionPool.setTestWhileIdle(testWhileIdle);

    // Set up statement pool, if desired
    GenericKeyedObjectPoolFactory statementPoolFactory = null;
    if (isPoolPreparedStatements()) {
        statementPoolFactory = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive (per key)
                GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait
                1, // maxIdle (per key) 
                maxOpenPreparedStatements);
    }

    // Set up the driver connection factory we will use
    if (username != null) {
        connectionProperties.put("user", username);
    } else {
        logger.error("DBCP DataSource configured without a 'username'");
    }

    if (password != null) {
        connectionProperties.put("password", password);
    } else {
        logger.error("DBCP DataSource configured without a 'password'");
    }

    DriverConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, url,
            connectionProperties);

    // Set up the poolable connection factory we will use
    PoolableConnectionFactory connectionFactory = null;
    try {
        connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, connectionPool,
                statementPoolFactory, validationQuery, defaultReadOnly, defaultAutoCommit,
                defaultTransactionIsolation, defaultCatalog, null);
        if (connectionFactory == null) {
            throw new SQLException("Cannot create PoolableConnectionFactory");
        }
        validateConnectionFactory(connectionFactory);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SQLNestedException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }

    // Create and return the pooling data source to manage the connections
    dataSource = new PoolingDataSource(connectionPool);
    ((PoolingDataSource) dataSource)
            .setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    dataSource.setLogWriter(logWriter);

    try {
        for (int i = 0; i < initialSize; i++) {
            connectionPool.addObject();
        }
    } catch (Exception e) {
        throw new SQLNestedException("Error preloading the connection pool", e);
    }

    return dataSource;
}

From source file:org.apache.openejb.resource.jdbc.BasicDataSourceUtil.java

/**
 * Create a {@link PasswordCipher} instance from the
 *  passwordCipher class name./*from   w  w  w . j  a v a 2s .  c  o  m*/
 * 
 * @param passwordCipherClass the password cipher to look for
 * @return the password cipher from the passwordCipher class name
 *         optionally set.
 * @throws SQLException
 *             if the driver can not be found.
 */
public static PasswordCipher getPasswordCipher(String passwordCipherClass) throws SQLException {
    // Load the password cipher class
    Class<? extends PasswordCipher> pwdCipher = null;

    // try looking for implementation in /META-INF/org.apache.openejb.resource.jdbc.PasswordCipher
    ResourceFinder finder = new ResourceFinder("META-INF/");
    Map<String, Class<? extends PasswordCipher>> impls;
    try {
        impls = finder.mapAllImplementations(PasswordCipher.class);

    } catch (Throwable t) {
        String message = "Password cipher '" + passwordCipherClass
                + "' not found in META-INF/org.apache.openejb.resource.jdbc.PasswordCipher.";
        throw new SQLNestedException(message, t);
    }
    pwdCipher = impls.get(passwordCipherClass);

    //
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    final URL url = tccl
            .getResource("META-INF/org.apache.openejb.resource.jdbc.PasswordCipher/" + passwordCipherClass);
    if (url != null) {
        try {
            final String clazz = new BufferedReader(new InputStreamReader(url.openStream())).readLine().trim();
            pwdCipher = tccl.loadClass(clazz).asSubclass(PasswordCipher.class);
        } catch (Exception e) {
            // ignored
        }
    }

    // if not found in META-INF/org.apache.openejb.resource.jdbc.PasswordCipher
    // we can try to load the class.
    if (null == pwdCipher) {
        try {
            try {
                pwdCipher = Class.forName(passwordCipherClass).asSubclass(PasswordCipher.class);

            } catch (ClassNotFoundException cnfe) {
                pwdCipher = tccl.loadClass(passwordCipherClass).asSubclass(PasswordCipher.class);
            }
        } catch (Throwable t) {
            String message = "Cannot load password cipher class '" + passwordCipherClass + "'";
            throw new SQLNestedException(message, t);
        }
    }

    // Create an instance
    PasswordCipher cipher = null;
    try {
        cipher = pwdCipher.newInstance();

    } catch (Throwable t) {
        String message = "Cannot create password cipher instance";
        throw new SQLNestedException(message, t);
    }

    return cipher;
}

From source file:org.jumpmind.db.util.ResettableBasicDataSource.java

@Override
protected void createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory,
        KeyedObjectPoolFactory statementPoolFactory, AbandonedConfig configuration) throws SQLException {
    PoolableConnectionFactory connectionFactory = null;
    try {//from   w  w w .  java 2 s .  c  o m
        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);
    }
}

From source file:org.onecmdb.core.utils.transform.jdbc.ClassLoaderBasicDataSource.java

/**
 * Close and release all connections that are currently stored in the
 * connection pool associated with our data source.
 *
 * @throws SQLException if a database error occurs
 *///from www. ja v a 2  s  .c om
public synchronized void close() throws SQLException {
    GenericObjectPool oldpool = connectionPool;
    connectionPool = null;
    dataSource = null;
    try {
        if (oldpool != null) {
            oldpool.close();
        }
    } catch (SQLException e) {
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SQLNestedException("Cannot close connection pool", e);
    }
}

From source file:org.onecmdb.core.utils.transform.jdbc.ClassLoaderBasicDataSource.java

/**
 * <p>Create (if necessary) and return the internal data source we are
 * using to manage our connections.</p>
 *
 * <p><strong>IMPLEMENTATION NOTE</strong> - It is tempting to use the
 * "double checked locking" idiom in an attempt to avoid synchronizing
 * on every single call to this method.  However, this idiom fails to
 * work correctly in the face of some optimizations that are legal for
 * a JVM to perform.</p>/* w w w  . j  a v a2s  . c  o m*/
 *
 * @throws SQLException if the object pool cannot be created.
 */
protected synchronized DataSource createDataSource() throws SQLException {

    // Return the pool if we have already created it
    if (dataSource != null) {
        return (dataSource);
    }

    // Load the JDBC driver class
    Driver driver = null;
    if (driverClassName != null) {
        try {
            Class driverClass = Class.forName(driverClassName, true, driverLoader);
            driver = (Driver) driverClass.newInstance();
            //Class.forName(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
    if (driver == null) {
        try {
            driver = DriverManager.getDriver(url);
        } 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);
    }

    // Create an object pool to contain our active connections
    if ((abandonedConfig != null) && (abandonedConfig.getRemoveAbandoned())) {
        connectionPool = new AbandonedObjectPool(null, abandonedConfig);
    } else {
        connectionPool = new GenericObjectPool();
    }
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxWait(maxWait);
    connectionPool.setTestOnBorrow(testOnBorrow);
    connectionPool.setTestOnReturn(testOnReturn);
    connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    connectionPool.setTestWhileIdle(testWhileIdle);

    // Set up statement pool, if desired
    GenericKeyedObjectPoolFactory statementPoolFactory = null;
    if (isPoolPreparedStatements()) {
        statementPoolFactory = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive (per key)
                GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait
                1, // maxIdle (per key) 
                maxOpenPreparedStatements);
    }

    // Set up the driver connection factory we will use
    if (username != null) {
        connectionProperties.put("user", username);
    } else {
        log("DBCP DataSource configured without a 'username'");
    }

    if (password != null) {
        connectionProperties.put("password", password);
    } else {
        log("DBCP DataSource configured without a 'password'");
    }

    DriverConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, url,
            connectionProperties);

    // Set up the poolable connection factory we will use
    PoolableConnectionFactory connectionFactory = null;
    try {
        connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, connectionPool,
                statementPoolFactory, validationQuery, defaultReadOnly, defaultAutoCommit,
                defaultTransactionIsolation, defaultCatalog, abandonedConfig);
        if (connectionFactory == null) {
            throw new SQLException("Cannot create PoolableConnectionFactory");
        }
        validateConnectionFactory(connectionFactory);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SQLNestedException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }

    // Create and return the pooling data source to manage the connections
    dataSource = new PoolingDataSource(connectionPool);
    ((PoolingDataSource) dataSource)
            .setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    dataSource.setLogWriter(logWriter);

    try {
        for (int i = 0; i < initialSize; i++) {
            connectionPool.addObject();
        }
    } catch (Exception e) {
        throw new SQLNestedException("Error preloading the connection pool", e);
    }

    return dataSource;
}

From source file:org.openconcerto.sql.model.SQLDataSource.java

@Override
protected void createDataSourceInstance() throws SQLException {
    // PoolingDataSource returns a PoolGuardConnectionWrapper that complicates a lot of
    // things for nothing, so overload to simply return an object of the pool
    this.dataSource = new PoolingDataSource(this.connectionPool) {

        // we'll migrate to plain SQLException when our superclass does
        @SuppressWarnings("deprecation")
        @Override/*  ww  w. j  a  v a 2s  .  c o  m*/
        public Connection getConnection() throws SQLException {
            try {
                return (Connection) this._pool.borrowObject();
            } catch (SQLException e) {
                throw e;
            } catch (NoSuchElementException e) {
                throw new SQLNestedException("Cannot get a connection, pool exhausted", e);
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new SQLNestedException("Cannot get a connection, general error", e);
            }
        }

        @Override
        public Connection getConnection(String username, String password) throws SQLException {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.openspaces.jdbc.datasource.DbcpBasicDataSource.java

/**
 * <p>Create (if necessary) and return the internal data source we are
 * using to manage our connections.</p>
 *
 * <p><strong>IMPLEMENTATION NOTE</strong> - It is tempting to use the
 * "double checked locking" idiom in an attempt to avoid synchronizing
 * on every single call to this method.  However, this idiom fails to
 * work correctly in the face of some optimizations that are legal for
 * a JVM to perform.</p>//from  w  w  w.  ja va 2s. co m
 *
 * @throws SQLException if the object pool cannot be created.
 */
protected void createDataSource() throws SQLException {

    // Can't test without a validationQuery
    if (validationQuery == null) {
        setTestOnBorrow(false);
        setTestOnReturn(false);
        setTestWhileIdle(false);
    }

    // Create an object pool to contain our active connections
    if ((abandonedConfig != null) && (abandonedConfig.getRemoveAbandoned() == true)) {
        connectionPool = new AbandonedObjectPool(null, abandonedConfig);
    } else {
        connectionPool = new GenericObjectPool();
    }
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxWait(maxWait);
    connectionPool.setTestOnBorrow(testOnBorrow);
    connectionPool.setTestOnReturn(testOnReturn);
    connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    connectionPool.setTestWhileIdle(testWhileIdle);

    // Set up statement pool, if desired
    GenericKeyedObjectPoolFactory statementPoolFactory = null;
    if (isPoolPreparedStatements()) {
        statementPoolFactory = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive (per key)
                GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait
                1, // maxIdle (per key)
                maxOpenPreparedStatements);
    }

    DataSourceConnectionFactory dataSourceConnectionFactory = new DataSourceConnectionFactory(
            new SpaceDriverManagerDataSource(space));

    // Set up the poolable connection factory we will use
    try {
        PoolableConnectionFactory connectionFactory = new PoolableConnectionFactory(dataSourceConnectionFactory,
                connectionPool, statementPoolFactory, validationQuery, defaultReadOnly, defaultAutoCommit,
                defaultTransactionIsolation, defaultCatalog, abandonedConfig);
        validateConnectionFactory(connectionFactory);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SQLNestedException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }

    // Create and return the pooling data source to manage the connections
    dataSource = new PoolingDataSource(connectionPool);
    ((PoolingDataSource) dataSource)
            .setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    dataSource.setLogWriter(logWriter);

    try {
        for (int i = 0; i < initialSize; i++) {
            connectionPool.addObject();
        }
    } catch (Exception e) {
        throw new SQLNestedException("Error preloading the connection pool", e);
    }
}