Example usage for org.apache.commons.dbcp BasicDataSource setDriverClassName

List of usage examples for org.apache.commons.dbcp BasicDataSource setDriverClassName

Introduction

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

Prototype

public synchronized void setDriverClassName(String driverClassName) 

Source Link

Document

Sets the jdbc driver class name.

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:org.geotoolkit.coverage.postgresql.PGCoverageStoreFactory.java

/**
 * Creates the datasource for the coverage store.
 *///w ww. j a  v  a2s. c o m
private DataSource createDataSource(final ParameterValueGroup params) throws IOException {
    //create a datasource
    final BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    final String user = (String) params.parameter(USER.getName().toString()).getValue();
    dataSource.setUsername(user);

    // password
    final String passwd = (String) params.parameter(PASSWORD.getName().toString()).getValue();
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    final Integer maxWait = (Integer) params.parameter(MAXWAIT.getName().toString()).getValue();
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    final Integer minConn = (Integer) params.parameter(MINCONN.getName().toString()).getValue();
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    final Integer maxConn = (Integer) params.parameter(MAXCONN.getName().toString()).getValue();
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    final Boolean validate = (Boolean) params.parameter(VALIDATECONN.getName().toString()).getValue();
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    return new DBCPDataSource(dataSource);
}

From source file:org.geotoolkit.data.om.SOSDatabaseFeatureStoreFactory.java

@Override
public FeatureStore open(final ParameterValueGroup params) throws DataStoreException {
    checkCanProcessWithError(params);/*from  www  . j  av a2s. c o m*/
    try {
        //create a datasource
        final BasicDataSource dataSource = new BasicDataSource();

        // some default data source behaviour
        dataSource.setPoolPreparedStatements(true);

        // driver
        final String driver = getDriverClassName(params);
        dataSource.setDriverClassName(driver);
        final boolean isPostgres = driver.startsWith("org.postgresql");

        // url
        dataSource.setUrl(getJDBCUrl(params));

        // username
        final String user = (String) params.parameter(USER.getName().toString()).getValue();
        dataSource.setUsername(user);

        // password
        final String passwd = (String) params.parameter(PASSWD.getName().toString()).getValue();
        if (passwd != null) {
            dataSource.setPassword(passwd);
        }

        // some datastores might need this
        dataSource.setAccessToUnderlyingConnectionAllowed(true);

        final ManageableDataSource source = new DBCPDataSource(dataSource);
        return new SOSDatabaseFeatureStore(params, source, isPostgres);
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
}

From source file:org.geotoolkit.data.sml.SMLFeatureStoreFactory.java

@Override
public FeatureStore open(final ParameterValueGroup params) throws DataStoreException {
    checkCanProcessWithError(params);/*from  w ww.  j av  a 2  s  .c  om*/
    try {
        //create a datasource
        final BasicDataSource dataSource = new BasicDataSource();

        // some default data source behaviour
        dataSource.setPoolPreparedStatements(true);

        // driver
        dataSource.setDriverClassName(getDriverClassName(params));

        // url
        dataSource.setUrl(getJDBCUrl(params));

        // username
        final String user = (String) params.parameter(USER.getName().toString()).getValue();
        dataSource.setUsername(user);

        // password
        final String passwd = (String) params.parameter(PASSWD.getName().toString()).getValue();
        if (passwd != null) {
            dataSource.setPassword(passwd);
        }

        // some datastores might need this
        dataSource.setAccessToUnderlyingConnectionAllowed(true);

        final ManageableDataSource source = new DBCPDataSource(dataSource);
        return new SMLFeatureStore(params, source);
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
}

From source file:org.geotoolkit.db.AbstractJDBCFeatureStoreFactory.java

/**
 * Create a datasource using given parameters.
 *///from  w  w w .  j  a  v a  2  s. co m
protected DataSource createDataSource(final ParameterValueGroup params) throws IOException {
    //create a datasource
    final BasicDataSource dataSource = new BasicDataSource();

    // some default data source behaviour
    dataSource.setPoolPreparedStatements(false);

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    final String user = (String) params.parameter(USER.getName().toString()).getValue();
    dataSource.setUsername(user);

    // password
    final String passwd = (String) params.parameter(PASSWORD.getName().toString()).getValue();
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    final Integer maxWait = (Integer) params.parameter(MAXWAIT.getName().toString()).getValue();
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    final Integer minConn = (Integer) params.parameter(MINCONN.getName().toString()).getValue();
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    final Integer maxConn = (Integer) params.parameter(MAXCONN.getName().toString()).getValue();
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    final Boolean validate = (Boolean) params.parameter(VALIDATECONN.getName().toString()).getValue();
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // allow manipulating connections for possible tuning.
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    return new DBCPDataSource(dataSource);
}

From source file:org.geotools.data.h2.H2DataStoreFactory.java

protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException {
    String database = (String) DATABASE.lookUp(params);
    String host = (String) HOST.lookUp(params);
    BasicDataSource dataSource = new BasicDataSource();

    if (host != null && !host.equals("")) {
        Integer port = (Integer) PORT.lookUp(params);
        if (port != null && !port.equals("")) {
            dataSource.setUrl("jdbc:h2:tcp://" + host + ":" + port + "/" + database);
        } else {//from   ww w.j ava  2s.  co m
            dataSource.setUrl("jdbc:h2:tcp://" + host + "/" + database);
        }
    } else if (baseDirectory == null) {
        //use current working directory
        dataSource.setUrl("jdbc:h2:" + database);
    } else {
        //use directory specified if the patch is relative
        String location;
        if (!new File(database).isAbsolute()) {
            location = new File(baseDirectory, database).getAbsolutePath();
        } else {
            location = database;
        }

        dataSource.setUrl("jdbc:h2:file:" + location);
    }

    String username = (String) USER.lookUp(params);
    if (username != null) {
        dataSource.setUsername(username);
    }
    String password = (String) PASSWD.lookUp(params);
    if (password != null) {
        dataSource.setPassword(password);
    }

    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setPoolPreparedStatements(false);

    return new DBCPDataSource(dataSource);
}

From source file:org.geotools.data.jdbc.ds.UnWrapperTest.java

public void testDBCPUnwrapper() throws SQLException, IOException {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test_mem");
    ds.setAccessToUnderlyingConnectionAllowed(true);

    Connection conn = ds.getConnection();
    UnWrapper uw = DataSourceFinder.getUnWrapper(conn);
    assertNotNull(uw);/*www. ja  v a2s .  c  o  m*/
    assertTrue(uw.canUnwrap(conn));
    Connection unwrapped = uw.unwrap(conn);
    assertNotNull(unwrapped);
    assertTrue(unwrapped instanceof org.h2.jdbc.JdbcConnection);

    Statement st = conn.createStatement();
    uw = DataSourceFinder.getUnWrapper(st);
    assertNotNull(uw);
    assertTrue(uw.canUnwrap(st));
    Statement uwst = uw.unwrap(st);
    assertNotNull(uwst);
    assertTrue(uwst instanceof org.h2.jdbc.JdbcStatement);
    st.close();

    PreparedStatement ps = conn.prepareStatement("select curtime()");
    uw = DataSourceFinder.getUnWrapper(ps);
    assertNotNull(uw);
    assertTrue(uw.canUnwrap(ps));
    PreparedStatement uwps = (PreparedStatement) uw.unwrap(ps);
    assertNotNull(uwps);
    assertTrue(uwps instanceof org.h2.jdbc.JdbcPreparedStatement);
    ps.close();

    conn.close();
    ds.close();
}

From source file:org.geotools.jdbc.JDBCDataStoreFactory.java

/**
 * DataSource access allowing SQL use: intended to allow client code to query available schemas.
 * <p>/*  w  ww. jav a 2 s  . c o  m*/
 * This DataSource is the clients responsibility to close() when they are finished using it.
 * </p> 
 * @param params Map of connection parameter.
 * @return DataSource for SQL use
 * @throws IOException
 */
public BasicDataSource createDataSource(Map params) throws IOException {
    //create a datasource
    BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    String user = (String) USER.lookUp(params);
    dataSource.setUsername(user);

    // password
    String passwd = (String) PASSWD.lookUp(params);
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    Integer maxWait = (Integer) MAXWAIT.lookUp(params);
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    Integer minConn = (Integer) MINCONN.lookUp(params);
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    Integer maxConn = (Integer) MAXCONN.lookUp(params);
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    Boolean validate = (Boolean) VALIDATECONN.lookUp(params);
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // some datastores might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    return dataSource;
}

From source file:org.geotools.jdbc.JDBCTestSetup.java

/**
 * Creates a data source by reading properties from a file called 'db.properties', 
 * located paralell to the test setup instance.
 *///from   w  w w .  j ava 2  s. c o  m
protected DataSource createDataSource() throws IOException {
    Properties db = fixture;

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(db.getProperty("driver"));
    dataSource.setUrl(db.getProperty("url"));

    if (db.containsKey("user")) {
        dataSource.setUsername(db.getProperty("user"));
    } else if (db.containsKey("username")) {
        dataSource.setUsername(db.getProperty("username"));
    }
    if (db.containsKey("password")) {
        dataSource.setPassword(db.getProperty("password"));
    }

    dataSource.setPoolPreparedStatements(true);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setMinIdle(1);
    dataSource.setMaxActive(4);
    // if we cannot get a connection within 5 seconds give up
    dataSource.setMaxWait(5000);

    initializeDataSource(dataSource, db);

    // return a closeable data source (DisposableDataSource interface)
    // so that the connection pool will be tore down on datastore dispose
    return new DBCPDataSource(dataSource);
}

From source file:org.geotools.referencing.factory.epsg.OracleDialectEpsgMediatorStarvationOnlineStressTest.java

protected DataSource connect(String user, String password, String url, Properties params) throws SQLException {
    //DataSource origional = super.connect( user, password, url, params );

    BasicDataSource origional = new BasicDataSource();
    origional.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    origional.setUsername(user);//from   w w  w . j  av a 2  s .c o m
    origional.setPassword(password);
    origional.setUrl(url);
    origional.setMaxActive(10);
    origional.setMaxIdle(1);
    return origional;
}

From source file:org.geowebcache.diskquota.jdbc.JDBCQuotaStoreFactory.java

private DataSource getDataSource(JDBCConfiguration config) throws ConfigurationException {
    try {//  w  ww  . j a v  a  2 s  . c  o m
        DataSource ds = null;
        if (config.getJNDISource() != null) {
            InitialContext context = new InitialContext();
            ds = (DataSource) context.lookup(config.getJNDISource());
        } else if (config.getConnectionPool() != null) {
            ConnectionPoolConfiguration cp = config.getConnectionPool();

            BasicDataSource bds = new BasicDataSource();
            bds.setDriverClassName(cp.getDriver());
            bds.setUrl(cp.getUrl());
            bds.setUsername(cp.getUsername());
            bds.setPassword(cp.getPassword());
            bds.setPoolPreparedStatements(true);
            bds.setMaxOpenPreparedStatements(cp.getMaxOpenPreparedStatements());
            bds.setMinIdle(cp.getMinConnections());
            bds.setMaxActive(cp.getMaxConnections());
            bds.setMaxWait(cp.getConnectionTimeout() * 1000);
            bds.setValidationQuery(cp.getValidationQuery());

            ds = bds;
        }

        // verify the datasource works
        Connection c = null;
        try {
            c = ds.getConnection();
        } catch (SQLException e) {
            throw new ConfigurationException("Failed to get a database connection: " + e.getMessage(), e);
        } finally {
            if (c != null) {
                try {
                    c.close();
                } catch (SQLException e) {
                    // nothing we can do about it, but at least let the admin know
                    log.debug("An error occurred while closing the test JDBC connection: " + e.getMessage(), e);
                }
            }
        }

        return ds;
    } catch (NamingException e) {
        throw new ConfigurationException("Failed to locate the data source in JNDI", e);
    }
}