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

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

Introduction

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

Prototype

BasicDataSource

Source Link

Usage

From source file:org.openrdf.sail.rdbms.RdbmsStore.java

private DataSource lookupDataSource(String url, String user, String password) throws NamingException {
    if (url.startsWith("jdbc:")) {
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);/*from w w w.  j a va2  s. c o m*/
        ds.setUsername(user);
        ds.setPassword(password);
        setBasicDataSource(ds);
        return ds;
    }
    return (DataSource) new InitialContext().lookup(url);
}

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 {/*from   ww  w .j  av a2  s.com*/
            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  ww w.  ja  v  a2 s .  co 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);//from   w w  w .  jav  a 2 s .  c o m
    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);/*from   www.ja va2  s . c  o  m*/
    return basicDataSource;
}

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

private DataSource getCacheOrCreate() {
    String key = getCacheKey();/*from   ww w . j  av a  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);//  w w  w.j  ava 2  s .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.pepstock.jem.node.persistence.DBPoolManager.java

/**
 * Initializes the database pool /*  w ww. j  a  v  a 2s  .c o  m*/
 *  
 * @throws SQLException if any errors occurs
 */
public void init() throws SQLException {
    if (pool == null) {
        // creates the database pool
        pool = new BasicDataSource();
        // sets all information to setup
        // the JDBC connection
        pool.setDriverClassName(driver);
        pool.setUrl(url);
        pool.setUsername(user);
        pool.setPassword(password);

        // if the connection properties have been configured
        // all properties are passed to database pool
        if (properties != null && !properties.isEmpty()) {
            for (Object key : properties.keySet()) {
                String value = properties.getProperty(key.toString());
                pool.addConnectionProperty(key.toString(), value);
            }
        }
        // Due to Issue #262 set AutoCommit to FALSE
        // Remember to commit all SQL statements
        pool.setDefaultAutoCommit(false);
        // sets the pool
        pool.setInitialSize(5);
        pool.setMaxActive(10);
        pool.setMaxIdle(5);
        // if configured, uses the keep alive query 
        if (keepAliveConnectionSQL != null) {
            pool.setValidationQuery(keepAliveConnectionSQL);
        }
    }
}

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  www.  j  a  v  a2 s.c om

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