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.orbisgis.geoserver.h2gis.datastore.H2GISDataStoreFactory.java

@Override
protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException {
    String database = (String) DATABASE.lookUp(params);
    String host = (String) HOST.lookUp(params);
    Boolean mvcc = (Boolean) MVCC.lookUp(params);
    Boolean mvstore = (Boolean) MVSTORE.lookUp(params);
    BasicDataSource dataSource = new BasicDataSource();

    if (host != null && !host.equals("")) {
        Integer port = (Integer) PORT.lookUp(params);
        if (port != null) {
            dataSource.setUrl("jdbc:h2:tcp://" + host + ":" + port + "/" + database);
        } else {/*  w  w w.j av a  2s  .c om*/
            dataSource.setUrl("jdbc:h2:tcp://" + host + "/" + database);
        }
    } else if (baseDirectory == null) {
        //use current working directory
        dataSource.setUrl("jdbc:h2:" + database + ";AUTO_SERVER=TRUE" + (mvcc != null ? (";MVCC=" + mvcc) : "")
                + (mvstore != null ? (";MVSTORE=" + mvstore) : ""));
    } 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 + ";AUTO_SERVER=TRUE"
                + (mvcc != null ? (";MVCC=" + mvcc) : "") + (mvstore != null ? (";MVSTORE=" + mvstore) : ""));
    }

    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);

    // if we got here the database has been created, now verify it has the H2GIS extension
    // and eventually try to create them
    JDBCDataStore closer = new JDBCDataStore();
    Connection cx = null;
    try {
        cx = dataSource.getConnection();
        //Add the spatial function
        if (!JDBCUtilities.tableExists(cx, "PUBLIC.GEOMETRY_COLUMNS")) {
            CreateSpatialExtension.initSpatialExtension(cx);
        }
    } catch (SQLException e) {
        throw new IOException("Failed to create the target database", e);
    } finally {
        closer.closeSafe(cx);
    }

    return new DBCPDataSource(dataSource);
}

From source file:org.overlord.dtgov.devsvr.bpm.DTGovBpmDevServer.java

/**
 * Creates an in-memory datasource.//from w w w  . j a v  a  2  s  .  c  o  m
 * @throws SQLException
 */
private static DataSource createInMemoryDatasource() throws SQLException {
    System.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    Connection connection = ds.getConnection();
    connection.close();
    return ds;
}

From source file:org.owasp.dependencytrack.config.DatabaseConfiguration.java

@Bean
public DataSource dataSource() {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(driverClassName);
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);
    basicDataSource.setUrl(url);//www .j a  v  a2 s. com
    basicDataSource.setMaxActive(maxActive);
    return basicDataSource;
}

From source file:org.owasp.dependencytrack.config.JunitDatabaseConfiguration.java

@Bean
public DataSource dataSource() {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName("org.h2.Driver");
    basicDataSource.setUsername("sa");
    basicDataSource.setPassword("");
    basicDataSource.setUrl("jdbc:h2:mem:dtrack");
    basicDataSource.setMaxActive(100);//w w  w .ja v a2 s  . co  m
    return basicDataSource;
}

From source file:org.paxml.tag.sql.SqlDataSourceTag.java

private DataSource getCacheOrCreate() {
    String key = getCacheKey();//www  .ja  va 2s.c om
    DataSource ds = CACHE.get(key);
    if (ds == null) {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(getDriver());
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDefaultAutoCommit(true);
        DataSource existing = CACHE.putIfAbsent(key, dataSource);
        if (existing != null) {
            ds = existing;
        } else {
            ds = dataSource;
        }
    }
    return ds;
}

From source file:org.paxml.util.DBUtils.java

public static DataSource getPooledDataSource(String driverClass, String username, String password, String url) {
    String key = driverClass + "::" + url;
    BasicDataSource ds = pooledDataSources.get(key);
    if (ds == null) {
        ds = new BasicDataSource();
        ds.setDriverClassName(driverClass);
        ds.setUsername(username);/*www  .j  a  v  a2s .  co  m*/
        ds.setPassword(password);
        ds.setUrl(url);
        BasicDataSource existingDs = pooledDataSources.putIfAbsent(key, ds);
        if (existingDs != null) {
            try {
                ds.close();
            } catch (SQLException e) {
                // do nothing
            }
            ds = existingDs;
        }

    }

    return ds;
}

From source file:org.pentaho.platform.engine.services.connection.datasource.dbcp.NonPooledDatasourceService.java

private DataSource convert(IDatasource datasource) {
    BasicDataSource basicDatasource = new BasicDataSource();
    basicDatasource.setDriverClassName(datasource.getDriverClass());
    basicDatasource.setMaxActive(datasource.getMaxActConn());
    basicDatasource.setMaxIdle(datasource.getIdleConn());
    basicDatasource.setMaxWait(datasource.getWait());
    basicDatasource.setUrl(datasource.getUrl());
    basicDatasource.setUsername(datasource.getUserName());
    basicDatasource.setPassword(datasource.getPassword());
    basicDatasource.setValidationQuery(datasource.getQuery());
    return basicDatasource;
}

From source file:org.pinus4j.cluster.impl.AppDBClusterImpl.java

@Override
public void buildDataSource(DBInfo dbConnInfo) throws LoadConfigException {
    AppDBInfo appDbConnInfo = (AppDBInfo) dbConnInfo;

    LOG.info(dbConnInfo.toString());//from w w  w. j  a  v a 2  s . c  o  m

    try {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(enumDb.getDriverClass());
        ds.setUsername(appDbConnInfo.getUsername());
        ds.setPassword(appDbConnInfo.getPassword());
        ds.setUrl(appDbConnInfo.getUrl());

        // ?
        Map<String, Object> dbConnPoolInfo = appDbConnInfo.getConnPoolInfo();
        ds.setValidationQuery("SELECT 1");
        ds.setMaxActive((Integer) dbConnPoolInfo.get(Const.PROP_MAXACTIVE));
        ds.setMinIdle((Integer) dbConnPoolInfo.get(Const.PROP_MINIDLE));
        ds.setMaxIdle((Integer) dbConnPoolInfo.get(Const.PROP_MAXIDLE));
        ds.setInitialSize((Integer) dbConnPoolInfo.get(Const.PROP_INITIALSIZE));
        ds.setRemoveAbandoned((Boolean) dbConnPoolInfo.get(Const.PROP_REMOVEABANDONED));
        ds.setRemoveAbandonedTimeout((Integer) dbConnPoolInfo.get(Const.PROP_REMOVEABANDONEDTIMEOUT));
        ds.setMaxWait((Integer) dbConnPoolInfo.get(Const.PROP_MAXWAIT));
        ds.setTimeBetweenEvictionRunsMillis(
                (Integer) dbConnPoolInfo.get(Const.PROP_TIMEBETWEENEVICTIONRUNSMILLIS));
        ds.setNumTestsPerEvictionRun((Integer) dbConnPoolInfo.get(Const.PROP_NUMTESTSPEREVICTIONRUN));
        ds.setMinEvictableIdleTimeMillis((Integer) dbConnPoolInfo.get(Const.PROP_MINEVICTABLEIDLETIMEMILLIS));

        dbConnInfo.setDatasource(ds);
    } catch (Exception e) {
        throw new LoadConfigException(e);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

public MySqlStorage(StorageConfiguration storageConf, String label, BasicDataSource dataSource) {

    dataSource.setDriverClassName(storageConf.getJdbcDriverClass());
    dataSource.setUrl(storageConf.getJdbcUrl());
    dataSource.setUsername(storageConf.getUsername());
    dataSource.setPassword(storageConf.getPassword());

    //TODO should be made configurable
    dataSource.setMaxActive(10);//from w  w w  .java  2 s  . com
    dataSource.setMinIdle(5);
    dataSource.setInitialSize(5);
    dataSource.setValidationQuery("SELECT 1;");
    dataSource.setTestOnBorrow(false);
    dataSource.setTestOnReturn(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setTimeBetweenEvictionRunsMillis(5000);

    dataModel = new LabeledMySQLJDBCDataModel(dataSource, "taste_preferences", "user_id", "item_id",
            "preference", "timestamp", "taste_candidates", "label", label);
    this.dataSource = dataSource;
    this.timeWindow = storageConf.getTimeWindow();
    if (timeWindow % 6 != 0 || timeWindow == 0) {
        timeWindow = 24;
    }

}

From source file:org.polyjdbc.core.infrastructure.DataSourceFactory.java

public static DataSource create(Dialect dialect, String databaseUrl, String user, String password) {
    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(DIALECT_DRIVER_CLASS.get(dialect.getCode()));
    dataSource.setUrl(databaseUrl);//  w  w w  .j av  a 2s.  c  o  m
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setDefaultAutoCommit(false);

    return dataSource;
}