Example usage for org.apache.commons.dbcp2 BasicDataSource setUrl

List of usage examples for org.apache.commons.dbcp2 BasicDataSource setUrl

Introduction

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

Prototype

public synchronized void setUrl(String url) 

Source Link

Document

Sets the #url .

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

Usage

From source file:org.libx4j.dbcp.DataSources.java

public static BasicDataSource createDataSource(final $dbcp_dbcp dbcp) throws SQLException {
    if (dbcp.isNull())
        throw new BindingRuntimeException("/dbcp:jdbc is missing");

    final dbcp_dbcp._jdbc jdbc = dbcp._jdbc(0);
    final BasicDataSource dataSource = new BasicDataSource() {
        @Override/*www .ja  va2s .  c  om*/
        public Connection getConnection() throws SQLException {
            //        try {
            return super.getConnection();
            //        }
            //        catch (final SQLException e) {
            //          // TODO: Finish this!
            //          if ("Cannot get a connection, pool error Timeout waiting for idle object".equals(e.getMessage()))
            //            Throwables.set(e, "XX" + e.getMessage());
            //
            //          throw e;
            //        }
        }
    };

    if (jdbc._driverClassName(0).isNull())
        throw new BindingRuntimeException("/dbcp:jdbc/dbcp:driverClassName is missing");

    dataSource.setDriverClassName(jdbc._driverClassName(0).text());

    //    if (jdbc._loginTimeout() != null && jdbc._loginTimeout().size() != 0 && jdbc._loginTimeout(0).text() != null) {
    // FIXME: This causes a ClassNotFoundException: com.sybase.jdbc3.jdbc.SybDriver
    //      try {
    //        dataSource.setLoginTimeout(jdbc._loginTimeout(0).text());
    //      }
    //      catch(final SQLException e) {
    //        throw new SQLException(e);
    //      }
    //  }

    if (jdbc._url(0).isNull())
        throw new BindingRuntimeException("/dbcp:jdbc/dbcp:url is missing");

    dataSource.setUrl(jdbc._url(0).text());

    if (jdbc._username(0).isNull())
        throw new BindingRuntimeException("/dbcp:jdbc/dbcp:username is missing");

    dataSource.setUsername(jdbc._username(0).text());

    if (jdbc._password(0).isNull())
        throw new BindingRuntimeException("/dbcp:jdbc/dbcp:password is missing");

    dataSource.setPassword(jdbc._password(0).text());
    final dbcp_dbcp._default defaults = dbcp._default(0);
    if (!defaults._connectionProperties(0).isNull())
        for (final dbcp_dbcp._default._connectionProperties._property property : defaults
                ._connectionProperties(0)._property())
            if (property._name$() != null && property._name$().text() != null && property._value$() != null
                    && property._value$().text() != null)
                dataSource.addConnectionProperty(property._name$().text(), property._value$().text());

    if (!defaults._autoCommit(0).isNull())
        dataSource.setDefaultAutoCommit(defaults._autoCommit(0).text());

    if (!defaults._readOnly(0).isNull())
        dataSource.setDefaultReadOnly(defaults._readOnly(0).text());

    if (!defaults._transactionIsolation(0).isNull()) {
        if (dbcp_dbcp._default._transactionIsolation.NONE.text()
                .equals(defaults._transactionIsolation(0).text()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
        else if (dbcp_dbcp._default._transactionIsolation.READ_5FCOMMITTED.text()
                .equals(defaults._transactionIsolation(0).text()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        else if (dbcp_dbcp._default._transactionIsolation.READ_5FUNCOMMITTED.text()
                .equals(defaults._transactionIsolation(0).text()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        else if (dbcp_dbcp._default._transactionIsolation.REPEATABLE_5FREAD.text()
                .equals(defaults._transactionIsolation(0).text()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        else if (dbcp_dbcp._default._transactionIsolation.SERIALIZABLE.text()
                .equals(defaults._transactionIsolation(0).text()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        else
            throw new UnsupportedOperationException(
                    "Unsupported transaction isolation: " + defaults._transactionIsolation(0).text());
    }

    final dbcp_dbcp._size size = dbcp._size(0);
    if (!size.isNull()) {
        if (!size._initialSize(0).isNull())
            dataSource.setInitialSize(size._initialSize(0).text().intValue());

        if (!size._maxActive(0).isNull())
            dataSource.setMaxTotal(size._maxActive(0).text().intValue());

        if (!size._maxIdle(0).isNull())
            dataSource.setMaxIdle(size._maxIdle(0).text().intValue());

        if (!size._minIdle(0).isNull())
            dataSource.setMinIdle(size._minIdle(0).text().intValue());

        if (!size._maxWait(0).isNull())
            dataSource.setMaxWaitMillis(size._maxWait(0).text().intValue());
    }

    final dbcp_dbcp._management management = dbcp._management(0);
    if (!management.isNull()) {
        if (!management._validationQuery(0).isNull())
            dataSource.setValidationQuery(management._validationQuery(0).text());

        if (!management._testOnBorrow(0).isNull())
            dataSource.setTestOnBorrow(management._testOnBorrow(0).text());

        if (!management._testOnReturn(0).isNull())
            dataSource.setTestOnReturn(management._testOnReturn(0).text());

        if (!management._testWhileIdle(0).isNull())
            dataSource.setTestWhileIdle(management._testWhileIdle(0).text());

        if (!management._timeBetweenEvictionRuns(0).isNull())
            dataSource
                    .setTimeBetweenEvictionRunsMillis(management._timeBetweenEvictionRuns(0).text().intValue());

        if (!management._numTestsPerEvictionRun(0).isNull())
            dataSource.setNumTestsPerEvictionRun(management._numTestsPerEvictionRun(0).text().intValue());

        if (!management._minEvictableIdleTime(0).isNull())
            dataSource.setMinEvictableIdleTimeMillis(management._minEvictableIdleTime(0).text().intValue());
    }

    final dbcp_dbcp._preparedStatements preparedStatements = dbcp._preparedStatements(0);
    if (!preparedStatements.isNull()) {
        if (!preparedStatements._poolPreparedStatements(0).isNull())
            dataSource.setPoolPreparedStatements(preparedStatements._poolPreparedStatements(0).text());

        if (!preparedStatements._maxOpenPreparedStatements(0).isNull())
            dataSource.setMaxOpenPreparedStatements(
                    preparedStatements._maxOpenPreparedStatements(0).text().intValue());
    }

    final dbcp_dbcp._removal removal = dbcp._removal(0);
    if (!removal.isNull()) {
        if (!removal._removeAbandoned(0).isNull())
            dataSource.setRemoveAbandonedOnBorrow(removal._removeAbandoned(0).text());

        if (!removal._removeAbandonedTimeout(0).isNull())
            dataSource.setRemoveAbandonedTimeout(removal._removeAbandonedTimeout(0).text().intValue());

        if (!removal._logAbandoned(0).isNull())
            dataSource.setLogAbandoned(removal._logAbandoned(0).text());
    }

    final dbcp_dbcp._logging logging = dbcp._logging(0);
    if (!logging.isNull()) {
        final Logger logger = LoggerFactory.getLogger(DataSources.class);
        final LoggerPrintWriter loggerPrintWriter = new LoggerPrintWriter(logger,
                Level.valueOf(logging._level(0).text()));
        dataSource.setLogWriter(loggerPrintWriter);
        dataSource.setLogExpiredConnections(
                !logging._logExpiredConnections(0).isNull() && logging._logExpiredConnections(0).text());
        if (!logging._logAbandoned(0).isNull() && logging._logAbandoned(0).text()) {
            dataSource.setAbandonedLogWriter(loggerPrintWriter);
            dataSource.setLogAbandoned(true);
        }
    }

    return dataSource;
}

From source file:org.mingle.pear.config.DataAccessJpaConfig.java

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(propDatabase.getDriverClassName());
    dataSource.setUrl(propDatabase.getUrl());
    dataSource.setUsername(propDatabase.getUsername());
    dataSource.setPassword(propDatabase.getPassword());
    dataSource.setInitialSize(5);/*from w  w w . java  2s .co  m*/
    dataSource.setTestOnBorrow(true);
    dataSource.setTestOnReturn(true);
    dataSource.setTestWhileIdle(true);
    dataSource.setTimeBetweenEvictionRunsMillis(1800000L);
    dataSource.setNumTestsPerEvictionRun(3);
    dataSource.setMinEvictableIdleTimeMillis(1800000L);
    return dataSource;
}

From source file:org.my.spring.batch.java.config.demo.configuration.BatchConfiguration.java

private DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(environment.getProperty("batch.jdbc.driver"));
    dataSource.setUrl(environment.getProperty("batch.jdbc.url"));
    dataSource.setUsername(environment.getProperty("batch.jdbc.user"));
    dataSource.setPassword(environment.getProperty("batch.jdbc.password"));
    return dataSource;
}

From source file:org.my.spring.batch.java.config.demo.configuration.JobSetup.java

@Bean
public DataSource job1DataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(environment.getProperty("job1.jdbc.driver"));
    dataSource.setUrl(environment.getProperty("job1.jdbc.url"));
    dataSource.setUsername(environment.getProperty("job1.jdbc.user"));
    dataSource.setPassword(environment.getProperty("job1.jdbc.password"));
    return dataSource;
}

From source file:org.ofbiz.core.entity.transaction.DBCPConnectionFactory.java

private static BasicDataSource createDataSource(JdbcDatasourceInfo jdbcDatasource) throws Exception {
    final Properties dbcpProperties = loadDbcpProperties();

    final BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(dbcpProperties);
    dataSource.setDriverClassLoader(Thread.currentThread().getContextClassLoader());
    dataSource.setDriverClassName(jdbcDatasource.getDriverClassName());
    dataSource.setUrl(jdbcDatasource.getUri());
    dataSource.setUsername(jdbcDatasource.getUsername());
    dataSource.setPassword(jdbcDatasource.getPassword());

    if (isNotEmpty(jdbcDatasource.getIsolationLevel())) {
        dataSource.setDefaultTransactionIsolation(
                TransactionIsolations.fromString(jdbcDatasource.getIsolationLevel()));
    }/*w w  w.  j a  v a  2 s.c om*/

    if (dbcpProperties.containsKey(PROP_JMX) && Boolean.valueOf(dbcpProperties.getProperty(PROP_JMX))) {
        dataSource.setJmxName(
                ObjectName.getInstance(dbcpProperties.getProperty(PROP_MBEANNAME)).getCanonicalName());
    }

    return dataSource;
}

From source file:org.ramadda.repository.database.DatabaseManager.java

/**
 * _more_//  w  ww  .ja v a 2 s. c o m
 *
 * @param connectionUrl _more_
 * @param userName _more_
 * @param password _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public BasicDataSource makeDataSource(String connectionUrl, String userName, String password) throws Exception {
    String driverClassName = loadDriver(connectionUrl);
    BasicDataSource ds = new BasicDataSource();

    //ds.setMaxActive(getRepository().getProperty(PROP_DB_POOL_MAXACTIVE, 100));
    //ds.setMaxIdle(getRepository().getProperty(PROP_DB_POOL_MAXIDLE,100));
    ds.setMaxTotal(getRepository().getProperty(PROP_DB_POOL_MAXACTIVE, 100));
    //30 second time out
    ds.setMaxWaitMillis(1000 * 30);
    //60 seconds
    ds.setRemoveAbandonedTimeout(60);
    //ds.setRemoveAbandoned(true);
    ds.setRemoveAbandonedOnBorrow(true);
    ds.setRemoveAbandonedOnMaintenance(true);

    //        System.err.println("DatabaseManager.makeDataSource: url="  + connectionUrl);
    //        System.err.println("JDBC driver class:" + driverClassName + " db type:" + dbType);

    ds.setDriverClassName(driverClassName);
    ds.setUsername(userName);
    ds.setPassword(password);
    ds.setUrl(connectionURL);

    /*
    Logger logger = getLogManager().getLogger(LOGID);
    if (logger != null) {
    ds.setLogWriter(new Log4jPrintWriter(logger));
    }
    */

    return ds;
}

From source file:org.springframework.batch.sample.remotepartitioning.DataSourceConfiguration.java

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(url);
    dataSource.setUsername(username);//from   w ww.j  a va  2 s.  c  om
    dataSource.setPassword(password);
    return dataSource;
}

From source file:org.wte4j.examples.showcase.server.hsql.HsqlServerBean.java

/**
 * create a new connection source as {@link DataSource} for this server
 * //from  w ww .  ja v  a  2 s.  c o m
 * @return a new {@link DataSource} object for this server with pooled
 *         connections
 * 
 */
public synchronized DataSource createDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl("jdbc:hsqldb:hsql://localhost/" + databaseName);
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUsername("sa");
    return dataSource;
}

From source file:pgsql.connection.PooledConnectionFactory.java

private void initializeConnectionPool(BasicDataSource connectionPool, URI databaseUri) {
    final String dbUrl = "jdbc:postgresql://" + databaseUri.getHost() + databaseUri.getPath();

    if (databaseUri.getUserInfo() != null) {
        connectionPool.setUsername(databaseUri.getUserInfo().split(":")[0]);
        connectionPool.setPassword(databaseUri.getUserInfo().split(":")[1]);
    }//from  w  w w . j  a  v  a 2s  .  c  o m
    connectionPool.setDriverClassName("org.postgresql.Driver");
    connectionPool.setUrl(dbUrl);
    connectionPool.setInitialSize(1);
}

From source file:ro.pippo.session.jdbc.JDBCSessionDataStorageTest.java

@BeforeClass
public static void setUpClass() {
    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName("org.h2.Driver");
    bds.setUrl("jdbc:h2:mem:test;INIT=runscript from 'src/test/resources/create.sql'");
    bds.setUsername("sa");
    bds.setPassword("sa");
    dataSource = bds;/*from   ww  w  .j  a  va  2 s  .  c o m*/
}