Tabnine Logo
BasicDataSource
Code IndexAdd Tabnine to your IDE (free)

How to use
BasicDataSource
in
org.apache.commons.dbcp

Best Java code snippets using org.apache.commons.dbcp.BasicDataSource (Showing top 20 results out of 1,584)

origin: stackoverflow.com

 BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("driverClassName");
ds.setUrl("jdbc://...");
ds.setUsername("username");
ds.setPassword("password");
origin: apache/kylin

/**
 * To close the adaptor, because we need to close all connections on this JDBC source.
 * @throws IOException If close failed.
 */
@Override
public void close() throws IOException {
  try {
    dataSource.close();
  } catch (SQLException e) {
    throw new IllegalStateException(e);
  }
}
origin: stackoverflow.com

 BasicDataSource dataSource = new BasicDataSource();

dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUsername("username");
dataSource.setPassword("password");
dataSource.setUrl("jdbc:mysql://<host>:<port>/<database>");
dataSource.setMaxActive(10);
dataSource.setMaxIdle(5);
dataSource.setInitialSize(5);
dataSource.setValidationQuery("SELECT 1");
origin: apache/kylin

private JdbcPushDownConnectionManager(KylinConfig config) throws ClassNotFoundException {
  dataSource = new BasicDataSource();
  Class.forName(config.getJdbcDriverClass());
  dataSource.setDriverClassName(config.getJdbcDriverClass());
  dataSource.setUrl(config.getJdbcUrl());
  dataSource.setUsername(config.getJdbcUsername());
  dataSource.setPassword(config.getJdbcPassword());
  dataSource.setMaxActive(config.getPoolMaxTotal());
  dataSource.setMaxIdle(config.getPoolMaxIdle());
  dataSource.setMinIdle(config.getPoolMinIdle());
  // Default settings
  dataSource.setTestOnBorrow(true);
  dataSource.setValidationQuery("select 1");
  dataSource.setRemoveAbandoned(true);
  dataSource.setRemoveAbandonedTimeout(300);
}
origin: cmlbeliever/SpringBootLearning

@Bean(destroyMethod = "close", name = "dataSource")
public DataSource dataSource(DataSourceProperties properties) {
  log.info("*************************dataSource***********************");
  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName(properties.driverClassName);
  dataSource.setUrl(properties.url);
  dataSource.setUsername(properties.username);
  dataSource.setPassword(properties.password);
  dataSource.setMaxIdle(properties.maxIdle);
  dataSource.setMaxActive(properties.maxActive);
  dataSource.setMaxWait(properties.maxWait);
  dataSource.setInitialSize(properties.initialSize);
  dataSource.setValidationQuery(properties.validationQuery);
  dataSource.setRemoveAbandoned(true);
  dataSource.setTestWhileIdle(true);
  dataSource.setTimeBetweenEvictionRunsMillis(30000);
  dataSource.setNumTestsPerEvictionRun(30);
  dataSource.setMinEvictableIdleTimeMillis(1800000);
  return dataSource;
}
origin: alibaba/nacos

@PostConstruct
public void init() {
  BasicDataSource ds = new BasicDataSource();
  ds.setDriverClassName(JDBC_DRIVER_NAME);
  ds.setUrl("jdbc:derby:" + NACOS_HOME + File.separator + DERBY_BASE_DIR + ";create=true");
  ds.setUsername(USER_NAME);
  ds.setPassword(PASSWORD);
  ds.setInitialSize(20);
  ds.setMaxActive(30);
  ds.setMaxIdle(50);
  ds.setMaxWait(10000L);
  ds.setPoolPreparedStatements(true);
  ds.setTimeBetweenEvictionRunsMillis(TimeUnit.MINUTES
    .toMillis(10L));
  ds.setTestWhileIdle(true);
  jt = new JdbcTemplate();
  jt.setMaxRows(50000);
  jt.setQueryTimeout(5000);
  jt.setDataSource(ds);
  DataSourceTransactionManager tm = new DataSourceTransactionManager();
  tjt = new TransactionTemplate(tm);
  tm.setDataSource(ds);
  tjt.setTimeout(5000);
  if (STANDALONE_MODE && !propertyUtil.isStandaloneUseMysql()) {
    reload();
  }
}
origin: apache/incubator-gobblin

/**
 * creates a new {@link BasicDataSource}
 * @param config the properties used for datasource instantiation
 * @return
 */
public static BasicDataSource newDataSource(Config config) {
 BasicDataSource basicDataSource = new BasicDataSource();
 PasswordManager passwordManager = PasswordManager.getInstance(ConfigUtils.configToProperties(config));
 basicDataSource.setDriverClassName(ConfigUtils.getString(config, ConfigurationKeys.STATE_STORE_DB_JDBC_DRIVER_KEY,
   ConfigurationKeys.DEFAULT_STATE_STORE_DB_JDBC_DRIVER));
 // MySQL server can timeout a connection so need to validate connections before use
 basicDataSource.setValidationQuery("select 1");
 basicDataSource.setTestOnBorrow(true);
 basicDataSource.setDefaultAutoCommit(false);
 basicDataSource.setTimeBetweenEvictionRunsMillis(60000);
 basicDataSource.setUrl(config.getString(ConfigurationKeys.STATE_STORE_DB_URL_KEY));
 basicDataSource.setUsername(passwordManager.readPassword(
   config.getString(ConfigurationKeys.STATE_STORE_DB_USER_KEY)));
 basicDataSource.setPassword(passwordManager.readPassword(
   config.getString(ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY)));
 basicDataSource.setMinEvictableIdleTimeMillis(
   ConfigUtils.getLong(config, ConfigurationKeys.STATE_STORE_DB_CONN_MIN_EVICTABLE_IDLE_TIME_KEY,
     ConfigurationKeys.DEFAULT_STATE_STORE_DB_CONN_MIN_EVICTABLE_IDLE_TIME));
 return basicDataSource;
}
origin: Meituan-Dianping/Zebra

@Override
public DataSource build(DataSourceConfig config, boolean withDefaultValue) {
  BasicDataSource dbcpDataSource = new BasicDataSource();
  dbcpDataSource.setUrl(config.getJdbcUrl());
  dbcpDataSource.setUsername(config.getUsername());
  dbcpDataSource.setPassword(config.getPassword());
  dbcpDataSource.setDriverClassName(StringUtils.isNotBlank(config.getDriverClass()) ? config.getDriverClass() : JdbcDriverClassHelper.getDriverClassNameByJdbcUrl(config.getJdbcUrl()));
    dbcpDataSource.setInitialSize(getIntProperty(config, "initialPoolSize", 5));
    dbcpDataSource.setMaxActive(getIntProperty(config, "maxPoolSize", 30));
    dbcpDataSource.setMinIdle(getIntProperty(config, "minPoolSize", 5));
    dbcpDataSource.setMaxIdle(getIntProperty(config, "maxPoolSize", 20));
    dbcpDataSource.setMaxWait(getIntProperty(config, "checkoutTimeout", 1000));
    dbcpDataSource.setValidationQuery(getStringProperty(config, "preferredTestQuery", "SELECT 1"));
    dbcpDataSource.setMinEvictableIdleTimeMillis(getIntProperty(config, "minEvictableIdleTimeMillis", 1800000));// 30min
    dbcpDataSource.setTimeBetweenEvictionRunsMillis(getIntProperty(config, "timeBetweenEvictionRunsMillis", 30000)); // 30s
    dbcpDataSource.setRemoveAbandonedTimeout(getIntProperty(config, "removeAbandonedTimeout", 300)); // 30s
    dbcpDataSource.setNumTestsPerEvictionRun(getIntProperty(config, "numTestsPerEvictionRun", 6)); // 30s
    dbcpDataSource.setValidationQueryTimeout(getIntProperty(config, "validationQueryTimeout", 0));
    if (StringUtils.isNotBlank(getStringProperty(config, "connectionInitSql", null))) {
      List<String> initSqls = new ArrayList<String>();
      initSqls.add(getStringProperty(config, "connectionInitSql", null));
      dbcpDataSource.setConnectionInitSqls(initSqls);
    dbcpDataSource.setTestWhileIdle(true);
    dbcpDataSource.setTestOnBorrow(false);
    dbcpDataSource.setTestOnReturn(false);
    dbcpDataSource.setRemoveAbandoned(true);
origin: stackoverflow.com

 BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(DATABASE_DRIVER_CLASS);
ds.setUsername(DATABASE_USERNAME);
ds.setPassword(DATABASE_PASSWORD);
ds.setUrl(DATABASE_URL);
ds.setInitialSize(1);
ds.setMaxActive(50);
ds.setDefaultAutoCommit(false);
origin: geotools/geotools

  throws DataSourceException {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setAccessToUnderlyingConnectionAllowed(true);
dataSource.setMaxActive(maxActive);
dataSource.setMinIdle(minIdle);
dataSource.setMinEvictableIdleTimeMillis(1000 * 20);
dataSource.setTimeBetweenEvictionRunsMillis(1000 * 10);
  dataSource.setTestOnBorrow(true);
  dataSource.setValidationQuery(validationQuery);
  dataSource.setPoolPreparedStatements(true);
  dataSource.setMaxOpenPreparedStatements(10);
  dataSource.setRemoveAbandoned(true);
  dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
  dataSource.setLogAbandoned(true);
  conn = dataSource.getConnection();
} catch (Exception e) {
  throw new DataSourceException("Connection test failed ", e);
origin: geotools/geotools

BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(getDriverClassName());
dataSource.setUrl(getJDBCUrl(params));
  dataSource.setUsername(user);
  dataSource.setPassword(passwd);
  dataSource.setMaxWait(maxWait * 1000);
  dataSource.setMinIdle(minConn);
  dataSource.setMaxActive(maxConn);
  dataSource.setTestOnBorrow(true);
  dataSource.setValidationQuery(getValidationQuery());
  dataSource.setTestWhileIdle(testWhileIdle);
  dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictorRuns * 1000l);
  dataSource.setMinEvictableIdleTimeMillis(minEvictableTime * 1000l);
  dataSource.setNumTestsPerEvictionRun(evictorTestsPerRun);
dataSource.setAccessToUnderlyingConnectionAllowed(true);
return dataSource;
origin: stackoverflow.com

 private static DataSource setupDataSource() {
  BasicDataSource ds = new BasicDataSource();
  ds.setDriverClassName(getDriver());
  ds.setUsername(getUser());
  ds.setPassword(getPassword());
  ds.setUrl(getConnectionString());

  ds.setDefaultAutoCommit(false);
  ds.setInitialSize(4);
  ds.setMaxActive(60);
  ds.setMaxIdle(10);

  ds.setValidationQuery("/* ping */ SELECT 1");//config to validate against mysql
  ds.setValidationQueryTimeout(3);
  ds.setTestOnBorrow(true);
  ds.setTestOnReturn(true);

  return ds;
}
origin: uk.org.taverna.configuration/taverna-database-configuration-impl

private void setupDataSource() {
  setDerbyPaths();
  dataSource = new BasicDataSource();
  dataSource.setDriverClassName(databaseConfiguration.getDriverClassName());
  System.setProperty("hibernate.dialect", databaseConfiguration.getHibernateDialect());
  dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  dataSource.setMaxActive(databaseConfiguration.getPoolMaxActive());
  dataSource.setMinIdle(databaseConfiguration.getPoolMinIdle());
  dataSource.setMaxIdle(databaseConfiguration.getPoolMaxIdle());
  dataSource.setDefaultAutoCommit(true);
  dataSource.setInitialSize(databaseConfiguration.getPoolMinIdle());
  //Derby blows up if the username of password is empty (even an empty string thats not null).
  if (databaseConfiguration.getUsername()!=null && databaseConfiguration.getUsername().length()>=1) dataSource.setUsername(databaseConfiguration.getUsername());
  if (databaseConfiguration.getPassword()!=null && databaseConfiguration.getPassword().length()>=1) dataSource.setPassword(databaseConfiguration.getPassword());
  dataSource.setUrl(databaseConfiguration.getJDBCUri());
}
origin: geotools/geotools

public DataSource createNewDataSource(Map params) throws IOException {
  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName((String) DRIVERCLASS.lookUp(params));
  dataSource.setUrl((String) JDBC_URL.lookUp(params));
  dataSource.setUsername((String) USERNAME.lookUp(params));
  dataSource.setPassword((String) PASSWORD.lookUp(params));
  dataSource.setAccessToUnderlyingConnectionAllowed(true);
  dataSource.setMaxActive(((Integer) MAXACTIVE.lookUp(params)).intValue());
  dataSource.setMaxIdle(((Integer) MAXIDLE.lookUp(params)).intValue());
  // check the data source is properly setup by trying to gather a connection out of it
  Connection conn = null;
  try {
    conn = dataSource.getConnection();
  } catch (SQLException e) {
    throw new DataSourceException(
        "Connection pool improperly set up: " + e.getMessage(), e);
  } finally {
    // close the connection at once
    if (conn != null)
      try {
        conn.close();
      } catch (SQLException e) {
      }
  }
  return dataSource;
}
origin: geotools/geotools

/**
 * Creates a data source by reading properties from a file called 'db.properties', located
 * paralell to the test setup instance.
 */
protected DataSource createDataSource() throws IOException {
  Properties db = fixture;
  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName(db.getProperty("driver"));
  dataSource.setUrl(db.getProperty("url"));
  if (db.containsKey("user")) {
    dataSource.setUsername(db.getProperty("user"));
  } else if (db.containsKey("username")) {
    dataSource.setUsername(db.getProperty("username"));
  }
  if (db.containsKey("password")) {
    dataSource.setPassword(db.getProperty("password"));
  }
  dataSource.setPoolPreparedStatements(true);
  dataSource.setAccessToUnderlyingConnectionAllowed(true);
  dataSource.setMinIdle(1);
  dataSource.setMaxActive(4);
  // if we cannot get a connection within 5 seconds give up
  dataSource.setMaxWait(5000);
  initializeDataSource(dataSource, db);
  // return a closeable data source (DisposableDataSource interface)
  // so that the connection pool will be tore down on datastore dispose
  return new DBCPDataSource(dataSource);
}
origin: pentaho/mondrian

    bds.setDefaultReadOnly(true);
    bds.setDriverClassName(olap4jDriverClassName);
    bds.setPassword(pwd);
    bds.setUsername(user);
    bds.setUrl(olap4jDriverConnectionString);
    bds.setPoolPreparedStatements(false);
    bds.setMaxIdle(maxPerUserConnectionCount);
    bds.setMaxActive(maxPerUserConnectionCount);
    bds.setMinEvictableIdleTimeMillis(
      idleConnectionsCleanupTimeoutMs);
    bds.setAccessToUnderlyingConnectionAllowed(true);
    bds.setInitialSize(1);
    bds.setTimeBetweenEvictionRunsMillis(60000);
    if (catalog != null) {
      bds.setDefaultCatalog(catalog);
Connection connection = bds.getConnection();
DelegatingConnection dc = (DelegatingConnection) connection;
Connection underlyingOlapConnection = dc.getInnermostDelegate();
origin: brettwooldridge/HikariCP-benchmark

protected void setupDbcp()
{
  org.apache.commons.dbcp.BasicDataSource ds = new org.apache.commons.dbcp.BasicDataSource();
  ds.setUrl(jdbcUrl);
  ds.setUsername("brettw");
  ds.setPassword("");
  ds.setInitialSize(MIN_POOL_SIZE);
  ds.setMinIdle(MIN_POOL_SIZE);
  ds.setMaxIdle(maxPoolSize);
  ds.setMaxActive(maxPoolSize);
  ds.setDefaultAutoCommit(false);
  ds.setTestOnBorrow(true);
  ds.setValidationQuery("SELECT 1");
  DS = ds;
}
origin: stackoverflow.com

 BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(JDBCDriver);
ds.setUrl(JDBCUrl);
ds.setUsername(JDBCUser);
ds.setPassword(JDBCPassword);
ds.setInitialSize(initSize);
ds.setMaxTotal(maxTotal);//negative for no limit
ds.setTestOnBorrow(false);
ds.setTestWhileIdle(true);

Connection con = ds.getConnection();
origin: pentaho/pentaho-kettle

@SuppressWarnings( "deprecation" )
private static void setPoolProperties( BasicDataSource ds, Properties properties, int initialSize, int maxSize ) {
 ds.setInitialSize( initialSize );
 ds.setMaxActive( maxSize );
  ds.setDefaultAutoCommit( Boolean.valueOf( value ) );
  ds.setDefaultReadOnly( Boolean.valueOf( value ) );
  ds.setDefaultTransactionIsolation( Integer.valueOf( value ) );
  ds.setDefaultCatalog( value );
  ds.setInitialSize( Integer.valueOf( value ) );
  ds.setMaxActive( Integer.valueOf( value ) );
  ds.setMaxIdle( Integer.valueOf( value ) );
  ds.setMinIdle( Integer.valueOf( value ) );
  ds.setMaxWait( Long.valueOf( value ) );
  ds.setValidationQuery( value );
  ds.setTestOnBorrow( Boolean.valueOf( value ) );
  ds.setTestOnReturn( Boolean.valueOf( value ) );
  ds.setTestWhileIdle( Boolean.valueOf( value ) );
origin: apache/incubator-gobblin

/**
 * <ul>
 * <li>Create the in-memory database and connect
 * <li>Create tables for snapshots and daily_paritions
 * <li>Attach all user defined functions from {@link SqlUdfs}
 * </ul>
 *
 */
@BeforeClass
public void setup() throws SQLException {
 basicDataSource = new BasicDataSource();
 basicDataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
 basicDataSource.setUrl("jdbc:derby:memory:derbypoc;create=true");
 Connection connection = basicDataSource.getConnection();
 connection.setAutoCommit(false);
 this.connection = connection;
 execute("CREATE TABLE Snapshots (dataset_path VARCHAR(255), name VARCHAR(255), path VARCHAR(255), ts TIMESTAMP, row_count bigint)");
 execute("CREATE TABLE Daily_Partitions (dataset_path VARCHAR(255), path VARCHAR(255), ts TIMESTAMP)");
 // Register UDFs
 execute("CREATE FUNCTION TIMESTAMP_DIFF(timestamp1 TIMESTAMP, timestamp2 TIMESTAMP, unitString VARCHAR(50)) RETURNS BIGINT PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA EXTERNAL NAME 'org.apache.gobblin.data.management.retention.sql.SqlUdfs.timestamp_diff'");
}
org.apache.commons.dbcpBasicDataSource

Javadoc

Basic implementation of javax.sql.DataSource that is configured via JavaBeans properties. This is not the only way to combine the commons-dbcp and commons-pool packages, but provides a "one stop shopping" solution for basic requirements.

Most used methods

  • <init>
  • setUrl
    Sets the #url. Note: this method currently has no effect once the pool has been initialized. The p
  • setPassword
    Sets the #password. Note: this method currently has no effect once the pool has been initialized.
  • setUsername
    Sets the #username. Note: this method currently has no effect once the pool has been initialized.
  • setDriverClassName
    Sets the jdbc driver class name. Note: this method currently has no effect once the pool has been
  • close
    Close and release all connections that are currently stored in the connection pool associated with o
  • setMaxActive
    Sets the maximum number of active connections that can be allocated at the same time.
  • getConnection
    BasicDataSource does NOT support this method.
  • setMaxIdle
    Sets the maximum number of connections that can remail idle in the pool.
  • setValidationQuery
    Sets the #validationQuery. Note: this method currently has no effect once the pool has been initia
  • setMaxWait
    Sets the maxWait property.
  • setDefaultAutoCommit
    Sets default auto-commit state of connections returned by this datasource. Note: this method curre
  • setMaxWait,
  • setDefaultAutoCommit,
  • setTestOnBorrow,
  • setMinIdle,
  • setTimeBetweenEvictionRunsMillis,
  • setMinEvictableIdleTimeMillis,
  • setInitialSize,
  • setPoolPreparedStatements,
  • setTestWhileIdle,
  • addConnectionProperty

Popular in Java

  • Updating database using SQL prepared statement
  • onCreateOptionsMenu (Activity)
  • putExtra (Intent)
  • findViewById (Activity)
  • URL (java.net)
    A Uniform Resource Locator that identifies the location of an Internet resource as specified by RFC
  • SortedMap (java.util)
    A map that has its keys ordered. The sorting is according to either the natural ordering of its keys
  • SortedSet (java.util)
    SortedSet is a Set which iterates over its elements in a sorted order. The order is determined eithe
  • Semaphore (java.util.concurrent)
    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
  • Table (org.hibernate.mapping)
    A relational table
  • Reflections (org.reflections)
    Reflections one-stop-shop objectReflections scans your classpath, indexes the metadata, allows you t
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now