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.bpmscript.test.hibernate.SpringSessionFactoryTestSupport.java

public void execute(ITestCallback<IServiceLookup> callback) throws Exception {

    //         final BasicDataSource dataSource = new BasicDataSource();
    //         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    //         dataSource.setUrl("jdbc:mysql://localhost:3306/bpmscript");
    //         dataSource.setUsername("bpmscript");
    //         dataSource.setPassword("sa");
    //        /*from   ww w  . j ava 2 s.  c  o  m*/
    //         Properties properties = new Properties();
    //         properties.setProperty("hibernate.hbm2ddl.auto", "create");
    //         properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    //         properties.setProperty("hibernate.show_sql", "false");

    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUrl("jdbc:h2:~/bpmscript");
    dataSource.setUsername("sa");
    dataSource.setPassword("");

    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create");
    properties.setProperty("hibernate.dialect", H2Dialect.class.getName());
    properties.setProperty("hibernate.show_sql", "false");

    //        final BasicDataSource dataSource = new BasicDataSource();
    //        dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
    //        dataSource.setUrl("jdbc:derby:test;create=true");
    //        dataSource.setUsername("sa");
    //        dataSource.setPassword("sa");
    //
    //        Properties properties = new Properties();
    //        properties.setProperty("hibernate.hbm2ddl.auto", "update");
    //        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
    //        properties.setProperty("hibernate.query.substitutions", "true 1, false 0");
    //        properties.setProperty("hibernate.show_sql", "false");

    ServiceLookup lookup = new ServiceLookup();
    final AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setLobHandler(new DefaultLobHandler());
    sessionFactoryBean.setHibernateProperties(properties);
    sessionFactoryBean.setAnnotatedClasses(classes);
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.afterPropertiesSet();

    SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean.getObject();
    lookup.addService("sessionFactory", sessionFactory);

    try {
        callback.execute(lookup);
    } finally {
        sessionFactory.close();
        sessionFactoryBean.destroy();
    }
}

From source file:org.bzewdu.tools.perftrend.data.oracledalHelper.java

/**
 *
 * @param connectURI/*  w  ww.  j a v a 2  s  . c  om*/
 * @return
 */
public static DataSource setupDataSource(final String connectURI) {
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    ds.setUsername(oracledalHelper.username);
    ds.setPassword(oracledalHelper.passwd);
    ds.setUrl(connectURI);
    return ds;
}

From source file:org.cambillaum.jpapersistor.persistence.configuration.PersistenceConfiguration.java

@Bean
public BasicDataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUrl("jdbc:hsqldb:mem:testdb");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    dataSource.setInitialSize(0);/*from w w w. java 2s  .c o  m*/
    dataSource.setMaxActive(15);
    dataSource.setMaxIdle(0);
    dataSource.setMinEvictableIdleTimeMillis(60000);
    return dataSource;
}

From source file:org.cbioportal.database.annotator.DataSourceConfiguration.java

public BasicDataSource dataSource() throws SQLException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUsername(username);/*from  w ww. ja  v  a2 s  . c o  m*/
    dataSource.setPassword(password);
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(connection_string);
    return dataSource;
}

From source file:org.cfr.capsicum.test.AbstractSimpleCayenneJUnitTests.java

protected static DataSource createDatasource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(EmbeddedDriver.class.getCanonicalName());
    ds.setUrl("jdbc:derby:memory:testdb;create=true");
    datasource = ds;/* w ww .j  a v  a  2 s.com*/
    return ds;
}

From source file:org.chaston.oakfunds.jdbc.AppEngineDataStoreModule.java

@Provides
@Singleton/*from  w w  w .ja va  2s  .c o m*/
DataSource provideDataSource() throws SQLException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.GoogleDriver");
    dataSource.setUrl(
            "jdbc:google:mysql://mchaston-oakfunds:oakfunds2/" + SystemColumnDefs.SCHEMA + "?user=root");
    return dataSource;
}

From source file:org.compass.core.lucene.engine.store.jdbc.DbcpDataSourceProvider.java

protected DataSource doCreateDataSource(String url, CompassSettings settings) throws CompassException {
    BasicDataSource dataSource = new BasicDataSource();
    if (!externalAutoCommit) {
        dataSource.setDefaultAutoCommit(autoCommit);
    }//from ww w . j a  va  2  s .com
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);

    if (settings.getSetting(
            LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.DEFAULT_TRANSACTION_ISOLATION) != null) {
        dataSource.setDefaultTransactionIsolation(settings.getSettingAsInt(
                LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.DEFAULT_TRANSACTION_ISOLATION, 0));
    }

    if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.INITIAL_SIZE) != null) {
        dataSource.setInitialSize(
                settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.INITIAL_SIZE, 0));
    }

    if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_ACTIVE) != null) {
        dataSource.setMaxActive(
                settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_ACTIVE, 0));
    }

    if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_IDLE) != null) {
        dataSource.setMaxIdle(
                settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_IDLE, 0));
    }

    if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MIN_IDLE) != null) {
        dataSource.setMinIdle(
                settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MIN_IDLE, 0));
    }

    if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_WAIT) != null) {
        dataSource.setMaxWait(
                settings.getSettingAsLong(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_WAIT, 0));
    }

    if (settings.getSetting(
            LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_OPEN_PREPARED_STATEMENTS) != null) {
        dataSource.setMaxOpenPreparedStatements(settings.getSettingAsInt(
                LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_OPEN_PREPARED_STATEMENTS, 0));
    }

    if (settings
            .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.POOL_PREPARED_STATEMENTS) != null) {
        dataSource.setPoolPreparedStatements(settings.getSettingAsBoolean(
                LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.POOL_PREPARED_STATEMENTS, false));
    }

    return dataSource;
}

From source file:org.danann.cernunnos.sql.BasicDataSourceTemplate.java

public final void perform(TaskRequest req, TaskResponse res) {
    //Get the JDBC properties
    final String driverClassName = (String) this.driverPhrase.evaluate(req, res);
    final String url = (String) this.urlPhrase.evaluate(req, res);
    final String username = (String) this.usernamePhrase.evaluate(req, res);
    final String password = (String) this.passwordPhrase.evaluate(req, res);

    final String dataSourceInfo = "driverClassName='" + driverClassName + "', url='" + url + "', username='"
            + username + "'";
    this.logger.debug("Creating DataSource for " + dataSourceInfo + ".");

    final BasicDataSource dataSource = new BasicDataSource();
    try {//w ww  . j  ava  2s .c  o m
        //Configure the pooling DataSource
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxActive(-1);
        dataSource.setMaxIdle(32);

        //Provide the DataSource on the response environment
        final String dataSourceAttrName = (String) this.attributeNamePhrase.evaluate(req, res);
        res.setAttribute(dataSourceAttrName, dataSource);
        this.logger.debug("Attached DataSource '" + dataSource + "' for " + dataSourceInfo
                + " to response under attribute '" + dataSourceAttrName + "'.");

        //Execute subtasks
        this.performWithDataSource(req, res, dataSource);
    } finally {
        try {
            //Cleanup after the subtasks
            dataSource.close();
            this.logger.debug("Closed DataSource '" + dataSource + "' for " + dataSourceInfo + ".");
        } catch (SQLException e) {
            throw new RuntimeException(
                    "Failed to close BasicDataSource '" + dataSource + "' for " + dataSourceInfo + ".", e);
        }
    }
}

From source file:org.datacleaner.connection.JdbcDatastore.java

public DataSource createDataSource() {
    initializeDriver();//from   www . ja va  2s. com

    BasicDataSource ds = new BasicDataSource();
    ds.setDefaultAutoCommit(false);
    ds.setUrl(_jdbcUrl);

    ds.setMaxActive(getSystemPropertyValue(SYSTEM_PROPERTY_CONNECTION_POOL_MAX_SIZE, -1));
    ds.setMinEvictableIdleTimeMillis(
            getSystemPropertyValue(SYSTEM_PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS, 500));
    ds.setTimeBetweenEvictionRunsMillis(
            getSystemPropertyValue(SYSTEM_PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTION_RUNS_MILLIS, 1000));

    if (_username != null && _password != null) {
        ds.setUsername(_username);
        ds.setPassword(_password);
    }
    return ds;
}

From source file:org.datacleaner.test.TestHelper.java

public static DataSource createSampleDatabaseDataSource() {
    BasicDataSource _dataSource = new BasicDataSource();
    _dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    _dataSource.setUrl("jdbc:hsqldb:res:testwareorderdb;readonly=true");
    _dataSource.setMaxActive(-1);/*from   w  ww  . java2  s .com*/
    _dataSource.setDefaultAutoCommit(false);
    return _dataSource;
}