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

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

Introduction

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

Prototype

public synchronized void setUsername(String username) 

Source Link

Document

Sets the #username .

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

Usage

From source file:com.dangdang.ddframe.rdb.sharding.example.transaction.Main.java

private static DataSource createTransactionLogDataSource() {
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
    result.setUrl("jdbc:mysql://localhost:3306/trans_log");
    result.setUsername("root");
    result.setPassword("");
    return result;
}

From source file:io.apicurio.hub.core.editing.EditingSessionManagerTest.java

/**
 * Creates an in-memory datasource.//from   ww  w  .jav  a 2  s. c  o  m
 * @throws SQLException
 */
private static BasicDataSource createInMemoryDatasource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:test" + (counter++) + ";DB_CLOSE_DELAY=-1");
    return ds;
}

From source file:com.dangdang.ddframe.rdb.sharding.example.jdbc.Main.java

private static DataSource createDataSource(final String dataSourceName) {
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
    result.setUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName));
    result.setUsername("root");
    result.setPassword("");
    return result;
}

From source file:com.dangdang.ddframe.rdb.sharding.config.yaml.AbstractYamlShardingDataSourceTest.java

protected static DataSource createDataSource(final String dsName) {
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName(org.h2.Driver.class.getName());
    result.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL", dsName));
    result.setUsername("sa");
    result.setPassword("");
    result.setMaxActive(100);//from  w  w w  .  j a va  2 s  .  c om
    return result;
}

From source file:com.pinterest.deployservice.db.DatabaseUtil.java

/**
 * Create a MySQL datasource./*from   w w w. j ava2 s .c o  m*/
 *
 * @param url             the url of the DB.
 * @param user            the user name to connect to MySQL as.
 * @param passwd          the password for the corresponding MySQL user.
 * @param poolSize        the connection pool size string, in the format of
 *                        initialSize:maxActive:maxIdle:minIdle.
 * @param maxWaitInMillis the max wait time in milliseconds to get a connection from the pool.
 * @return a BasicDataSource for the target MySQL instance.
 */
public static BasicDataSource createDataSource(String driverClassName, String url, String user, String passwd,
        String poolSize, int maxWaitInMillis) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(user);
    dataSource.setPassword(passwd);
    dataSource.setDefaultAutoCommit(true);
    dataSource.setDefaultReadOnly(false);

    // poolSize parsing, the poolsize string passed in the following format
    // initialSize:maxActive:maxIdle:minIdle
    String[] sizeStrs = poolSize.split(":");
    dataSource.setInitialSize(Integer.parseInt(sizeStrs[0]));
    dataSource.setMaxActive(Integer.parseInt(sizeStrs[1]));
    dataSource.setMaxIdle(Integer.parseInt(sizeStrs[2]));
    dataSource.setMinIdle(Integer.parseInt(sizeStrs[3]));

    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(true);
    dataSource.setTestOnReturn(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
    dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
    // dataSource.setNumTestsPerEvictionRun(3);
    // max wait in milliseconds for a connection.
    dataSource.setMaxWait(maxWaitInMillis);

    // force connection pool initialization.
    Connection conn = null;
    try {
        // Here not getting the connection from ThreadLocal no need to worry about that.
        conn = dataSource.getConnection();
    } catch (SQLException e) {
        LOG.error(String.format("Failed to get a db connection when creating DataSource, url = %s", url), e);
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return dataSource;
}

From source file:com.dangdang.ddframe.rdb.common.sql.base.AbstractSQLTest.java

private static BasicDataSource buildDataSource(final String dbName, final DatabaseType type) {
    DataBaseEnvironment dbEnv = new DataBaseEnvironment(type);
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName(dbEnv.getDriverClassName());
    result.setUrl(dbEnv.getURL(dbName));
    result.setUsername(dbEnv.getUsername());
    result.setPassword(dbEnv.getPassword());
    result.setMaxActive(1000);/*w  w w.j  a va  2  s . c  om*/
    if (DatabaseType.Oracle == dbEnv.getDatabaseType()) {
        result.setConnectionInitSqls(Collections.singleton("ALTER SESSION SET CURRENT_SCHEMA = " + dbName));
    }
    return result;
}

From source file:net.certifi.audittablegen.HsqldbDMR.java

/**
 * Generate a Hsqldb DataSource from Properties
 *
 * @param props//from   w  w  w  .j  a  va2 s .  c  o m
 * @return BasicDataSource as DataSource
 */
static DataSource getRunTimeDataSource(Properties props) {

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUsername(props.getProperty("username"));
    dataSource.setPassword(props.getProperty("password"));
    dataSource.setUrl(props.getProperty("url"));
    dataSource.setMaxActive(10);
    dataSource.setMaxIdle(5);
    dataSource.setInitialSize(5);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");

    return dataSource;
}

From source file:io.apiman.manager.test.server.ManagerApiTestServer.java

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

From source file:edu.ncsa.uiuc.rdfrepo.testing.USeekMSailFac.java

public static IndexingSail getIndexingSail(String dburl, String dbuser, String password,
        String capturepredicate, Sail sail) throws SailException {
    //set tup the postgis datasource for indexer
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUrl(dburl);//  ww  w  . j  a  va2s .  c  o  m
    dataSource.setUsername(dbuser);
    dataSource.setPassword(password);
    PostgisIndexerSettings indexerSettings = new PostgisIndexerSettings();

    //        String patternString =
    //            "?observation <http://purl.oclc.org/NET/ssnx/ssn#observationResultTime> ?time." +
    //                "?time  <http://www.w3.org/2006/time#inXSDDateTime> ?timevalue. " +
    //                "?loc <http://www.opengis.net/rdf#hasWKT> ?coord. " +
    //                "?sensor <http://www.loa-cnr.it/ontologies/DUL.owl#hasLocation> ?loc." +
    //                "?observation <http://purl.oclc.org/NET/ssnx/ssn#observedBy> ?sensor. ";

    String patternString = "?geometry <http://www.opengis.net/rdf#hasWKT> ?wkt.";

    sail.initialize();
    indexerSettings.setMatchSatatments(createPatternFromString(patternString, sail));
    indexerSettings.setDataSource(dataSource);

    //a matcher decides which object should be indexed by its associated predicate, we can use "http://www.opengis.net/rdf#hasWKT"
    PostgisIndexMatcher matcher = new PostgisIndexMatcher();
    matcher.setPredicate(capturepredicate);

    //create a IndexingSail from the basic sail and the indexer
    indexerSettings.setMatchers(Arrays.asList(new PostgisIndexMatcher[] { matcher }));

    //        PartitionDef p = new P
    //        IndexingSail indexingSail = new IndexingSail(sail, indexerSettings);
    //        indexingSail.getConnection().

    //        indexingSail.initialize();
    return null;
}

From source file:edu.ncsa.uiuc.rdfrepo.testing.USeekMSailFac.java

public static IndexingSail getIndexingSail(String dburl, String dbuser, String password,
        String capturepredicate, RepositorySail sail) throws SailException {
    //set tup the postgis datasource for indexer

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUrl(dburl);//  w w w  .  ja  va2  s .  com
    dataSource.setUsername(dbuser);
    dataSource.setPassword(password);
    PostgisIndexerSettings indexerSettings = new PostgisIndexerSettings();
    indexerSettings.setDataSource(dataSource);

    String patternString = "?observation <http://purl.oclc.org/NET/ssnx/ssn#observationResultTime> ?time."
            + "?time  <http://www.w3.org/2006/time#inXSDDateTime> ?timevalue. "
            + "?loc <http://www.opengis.net/rdf#hasWKT> ?coord. "
            + "?sensor <http://www.loa-cnr.it/ontologies/DUL.owl#hasLocation> ?loc."
            + "?observation <http://purl.oclc.org/NET/ssnx/ssn#observedBy> ?sensor. ";

    //        String patternString =
    //            "?geometry <http://www.opengis.net/rdf#hasWKT> ?wkt.";

    sail.initialize();
    indexerSettings.setMatchSatatments(createPatternFromString(patternString, sail));
    indexerSettings.setDataSource(dataSource);

    //a matcher decides which object should be indexed by its associated predicate, we can use "http://www.opengis.net/rdf#hasWKT"
    PostgisIndexMatcher matcher = new PostgisIndexMatcher();
    matcher.setPredicate(capturepredicate);

    //create a IndexingSail from the basic sail and the indexer
    indexerSettings.setMatchers(Arrays.asList(new PostgisIndexMatcher[] { matcher }));
    return null;

    //        PartitionDef p = new P
    //        IndexingSail indexingSail = new IndexingSail(sail, indexerSettings);
    //
    //        indexingSail.initialize();
    //        return indexingSail;
}