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:edu.dfci.cccb.mev.web.configuration.PersistenceConfiguration.java

@Bean(name = "mev-datasource")
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(environment.getProperty("database.driver.class", "org.h2.Driver"));
    dataSource.setUrl(environment.getProperty("database.url", "jdbc:h2:file:" + getProperty("java.io.tmpdir")
            + separator + "mev" + ";QUERY_CACHE_SIZE=100000" + ";CACHE_SIZE=1048576"));
    dataSource.setUsername(environment.getProperty("database.username", "sa"));
    dataSource.setPassword(environment.getProperty("database.password", ""));
    return dataSource;
}

From source file:be.ugent.tiwi.sleroux.newsrec.newsreclib.dao.mysqlImpl.AbstractJDBCBaseDao.java

private synchronized BasicDataSource createConnectionPool() {
    BasicDataSource source;
    logger.debug("creating connectionpool");
    String driver = bundle.getString("dbDriver");
    String user = bundle.getString("dbUser");
    String pass = bundle.getString("dbPass");
    String url = bundle.getString("dbUrl");
    url = url + "?user=" + user + "&password=" + pass;
    source = new BasicDataSource();
    source.setDriverClassName(driver);/*  ww w  .  j a v a  2 s.c  o m*/
    source.setUsername(user);
    source.setPassword(pass);
    source.setUrl(url);
    source.setTestOnReturn(true);
    source.setValidationQuery("SELECT 1");
    logger.debug("connectionpool created");
    return source;
}

From source file:net.jetrix.DataSourceManager.java

/**
 * Configure a datasource./*from w  w  w  .j  a  va2 s . co  m*/
 *
 * @param config      the configuration of the datasource
 * @param environment the environment of the datasource
 */
public void setDataSource(DataSourceConfig config, String environment) {
    try {
        Class.forName(config.getDriver());
    } catch (ClassNotFoundException e) {
        log.warning("Unable to find the database driver (" + config.getDriver()
                + "), put the related jar in the lib directory");
        return;
    }

    try {
        // close the previous datasource if necessary
        if (datasources.containsKey(environment)) {
            BasicDataSource datasource = (BasicDataSource) datasources.get(environment);
            datasource.close();
        }

        BasicDataSource datasource = new BasicDataSource();
        datasource.setDefaultAutoCommit(false);

        datasource.setDriverClassName(config.getDriver());
        datasource.setUrl(config.getUrl());
        datasource.setUsername(config.getUsername());
        datasource.setPassword(config.getPassword());
        datasource.setMinIdle(config.getMinIdle() != 0 ? config.getMinIdle() : DEFAULT_MIN_IDLE);
        datasource.setMaxActive(config.getMaxActive() != 0 ? config.getMaxActive() : DEFAULT_MAX_ACTIVE);

        // attempts to open the connection
        datasource.getConnection().close();

        datasources.put(environment, datasource);
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unable to configure the datasource '" + environment + "'", e);
    }
}

From source file:com.test.HibernateDerbyLockingTest.java

public void testSpringWithDataSource() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(EmbeddedDriver.class.getName());
    dataSource.setUrl("jdbc:derby:lockingtest;create=true");
    dataSource.setUsername("sa");
    dataSource.setPassword("sa");

    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create");
    properties.setProperty("hibernate.dialect", "org.bpmscript.hibernate.DerbyDialect");
    properties.setProperty("hibernate.show_sql", "true");

    // properties.setProperty("hibernate.connection.driver_class",
    // EmbeddedDriver.class.getName());
    // properties.setProperty("hibernate.connection.url",
    // "jdbc:derby:lockingtest;create=true");
    // properties.setProperty("hibernate.connection.username", "sa");
    // properties.setProperty("hibernate.connection.password", "sa");
    // properties.setProperty("hibernate.connection.pool_size", "10");

    final AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setLobHandler(new DefaultLobHandler());
    sessionFactoryBean.setHibernateProperties(properties);
    sessionFactoryBean.setAnnotatedClasses(new Class[] { Person.class });
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.afterPropertiesSet();

    SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean.getObject();
    try {//w  w w  .  j  a v a 2s. c om
        runTest(sessionFactory);
    } finally {
        sessionFactory.close();
    }
}

From source file:com.talkingdata.orm.tool.ORMGenerateAction.java

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    ORMConfig config = ORMConfig.getInstance(project);

    // 1. validate input parameter
    if (!validateConfig(project, config)) {
        return;/*from   w  w  w .ja  va 2s. com*/
    }

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl(
            String.format("jdbc:mysql://%s:%s/%s", config.getIp(), config.getPort(), config.getDatabase()));
    dataSource.setUsername(config.getUser());
    dataSource.setPassword(config.getPassword());

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    // 2. validate database is connected
    try {
        jdbcTemplate.execute("SELECT 1");
    } catch (Exception exception) {
        Messages.showWarningDialog(project, "The database is not connected.", "Warning");
        return;
    }

    File resourceDirectory = new File(project.getBasePath(), "/src/main/resources/mybatis");
    if (!resourceDirectory.exists()) {
        resourceDirectory.mkdirs();
    }

    File packageDirectory = new File(project.getBasePath(),
            "/src/main/java/" + config.getPackageName().replaceAll("\\.", "/"));
    if (!packageDirectory.exists()) {
        packageDirectory.mkdirs();
    }

    Properties p = new Properties();
    p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
            "org.apache.velocity.runtime.log.Log4JLogChute");
    Velocity.init(p);

    // 3. query all table
    SqlParser sqlParser = new SqlParser();

    try {
        for (ClassDefinition cls : sqlParser.getTables(jdbcTemplate, config.getDatabase(),
                config.getPackageName())) {
            Map<String, Object> map = new HashMap<>(1);
            map.put("cls", cls);

            File domainDirectory = new File(packageDirectory, "domain");
            if (!domainDirectory.exists()) {
                domainDirectory.mkdirs();
            }
            File clsFile = new File(domainDirectory, cls.getClassName() + ".java");
            if (!clsFile.exists()) {
                clsFile.createNewFile();
            }
            writeFile("com/talkingdata/orm/tool/vm/class.vm", map, new FileWriter(clsFile));

            File daoDirectory = new File(packageDirectory, "dao");
            if (!daoDirectory.exists()) {
                daoDirectory.mkdirs();
            }
            File daoFile = new File(daoDirectory, cls.getClassName() + "Dao.java");
            if (!daoFile.exists()) {
                daoFile.createNewFile();
            }
            writeFile("com/talkingdata/orm/tool/vm/dao.vm", map, new FileWriter(daoFile));

            File mapperFile = new File(resourceDirectory, cls.getClassInstanceName() + ".xml");
            if (!mapperFile.exists()) {
                mapperFile.createNewFile();
            }
            writeFile("com/talkingdata/orm/tool/vm/mapper.vm", map, new FileWriter(mapperFile));
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

}

From source file:com.bstek.dorado.core.store.SqlBaseStoreSupport.java

protected synchronized DataSource getDataSource() throws Exception {
    if (dataSource != null) {
        return dataSource;
    }//from   w ww .  j  a va 2  s. c  o m

    if (StringUtils.isBlank(namespace)) {
        throw new IllegalArgumentException("The namespace of store cannot be empty. ");
    }

    prepareNamespace();

    BasicDataSource pds = new BasicDataSource();
    dataSource = pds;

    pds.setDriverClassName(driverClassName);
    pds.setUrl(getConnectionUrl());
    pds.setUsername(username);
    pds.setPassword(password);
    pds.setDefaultCatalog(defaultCatalog);

    if (defaultAutoCommit != null) {
        pds.setDefaultAutoCommit(defaultAutoCommit.booleanValue());
    }
    if (defaultReadOnly != null) {
        pds.setDefaultReadOnly(defaultReadOnly.booleanValue());
    }
    if (defaultTransactionIsolation != null) {
        pds.setDefaultTransactionIsolation(defaultTransactionIsolation.intValue());
    }
    if (maxActive != null) {
        pds.setMaxActive(maxActive.intValue());
    }
    if (maxIdle != null) {
        pds.setMaxIdle(maxIdle.intValue());
    }
    if (minIdle != null) {
        pds.setMinIdle(minIdle.intValue());
    }
    if (initialSize != null) {
        pds.setInitialSize(initialSize.intValue());
    }
    if (maxWait != null) {
        pds.setMaxWait(maxWait.longValue());
    }
    if (timeBetweenEvictionRunsMillis != null) {
        pds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis.longValue());
    }
    if (minEvictableIdleTimeMillis != null) {
        pds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis.longValue());
    }
    return dataSource;
}

From source file:com.alibaba.druid.benckmark.pool.Oracle_Case3.java

public void test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setMaxActive(maxActive);//from www . j a  va2  s  . co m
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);

    for (int i = 0; i < loopCount; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}

From source file:gtu._work.ui.RegexCatchReplacer_Ebao.java

private DataSource getDbDataSource() {
    String url = null;/*from   w  ww .  j a v  a2s. c o  m*/
    String username = null;
    String password = null;
    if (ebaoProp.containsKey("url")) {
        url = ebaoProp.getProperty("url");
    }
    if (ebaoProp.containsKey("username")) {
        username = ebaoProp.getProperty("username");
    }
    if (ebaoProp.containsKey("password")) {
        password = ebaoProp.getProperty("password");
    }
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl(url);
    bds.setUsername(username);
    bds.setPassword(password);
    bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    return bds;
}

From source file:com.alibaba.druid.benckmark.pool.Case_Concurrent_50.java

public void test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);/* w  w w . j a v  a2 s .  com*/
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    for (int i = 0; i < LOOP_COUNT; ++i) {
        p0(dataSource, "dbcp");
    }

    System.out.println();
}

From source file:com.manpowergroup.cn.icloud.util.Case0.java

public void test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);/*w w  w  .  j av  a  2  s  . c  o  m*/
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    for (int i = 0; i < LOOP_COUNT; ++i) {
        p0(dataSource, "dbcp");
    }
    System.out.println();
}