Java Code Examples for org.apache.commons.dbcp.BasicDataSource#setDefaultAutoCommit()

The following examples show how to use org.apache.commons.dbcp.BasicDataSource#setDefaultAutoCommit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DbHelper.java    From MediaPlayer with GNU General Public License v2.0 6 votes vote down vote up
public static QueryRunner getQueryRunner(){
    if(DbHelper.dataSource==null){
        //����dbcp����Դ
        BasicDataSource dbcpDataSource = new BasicDataSource();
        dbcpDataSource.setUrl("jdbc:mysql://localhost:3306/employees?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull");
        dbcpDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dbcpDataSource.setUsername("root");
        dbcpDataSource.setPassword("root");
        dbcpDataSource.setDefaultAutoCommit(true);
        dbcpDataSource.setMaxActive(100);
        dbcpDataSource.setMaxIdle(30);
        dbcpDataSource.setMaxWait(500);
        DbHelper.dataSource = (DataSource)dbcpDataSource;
        System.out.println("Initialize dbcp...");
    }
    return new QueryRunner(DbHelper.dataSource);
}
 
Example 2
Source File: DataSourceConfig.java    From mango with Apache License 2.0 6 votes vote down vote up
public static DataSource getDataSource(int i, boolean autoCommit, int maxActive) {
  String driverClassName = getDriverClassName(i);
  String url = getUrl(i);
  String username = getUsername(i);
  String password = getPassword(i);

  BasicDataSource ds = new BasicDataSource();
  ds.setUrl(url);
  ds.setUsername(username);
  ds.setPassword(password);
  ds.setInitialSize(1);
  ds.setMaxActive(maxActive);
  ds.setDriverClassName(driverClassName);
  ds.setDefaultAutoCommit(autoCommit);
  return ds;
}
 
Example 3
Source File: MysqlDagStateStoreTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
protected StateStore<State> createStateStore(Config config) {
  try {
    // Setting up mock DB
    ITestMetastoreDatabase testMetastoreDatabase = TestMetastoreDatabaseFactory.get();
    String jdbcUrl = testMetastoreDatabase.getJdbcUrl();
    BasicDataSource mySqlDs = new BasicDataSource();

    mySqlDs.setDriverClassName(ConfigurationKeys.DEFAULT_STATE_STORE_DB_JDBC_DRIVER);
    mySqlDs.setDefaultAutoCommit(false);
    mySqlDs.setUrl(jdbcUrl);
    mySqlDs.setUsername(TEST_USER);
    mySqlDs.setPassword(TEST_PASSWORD);

    return new MysqlStateStore<>(mySqlDs, TEST_DAG_STATE_STORE, false, State.class);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: MysqlStateStore.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: DataSourceFactory.java    From polyjdbc with Apache License 2.0 5 votes vote down vote up
public static DataSource create(Dialect dialect, String databaseUrl, String user, String password) {
    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(DIALECT_DRIVER_CLASS.get(dialect.getCode()));
    dataSource.setUrl(databaseUrl);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setDefaultAutoCommit(false);

    return dataSource;
}
 
Example 6
Source File: WorkerNodeServiceTest.java    From score with Apache License 2.0 5 votes vote down vote up
@Bean
DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test");
    ds.setUsername("sa");
    ds.setPassword("sa");
    ds.setDefaultAutoCommit(false);
    return new TransactionAwareDataSourceProxy(ds);
}
 
Example 7
Source File: ExecutionQueueRepositoryTest.java    From score with Apache License 2.0 5 votes vote down vote up
@Bean
DataSource dataSource(){
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test");
    ds.setUsername("sa");
    ds.setPassword("sa");
    ds.setDefaultAutoCommit(false);
    return new TransactionAwareDataSourceProxy(ds);
}
 
Example 8
Source File: VersionRepositoryTest.java    From score with Apache License 2.0 5 votes vote down vote up
@Bean
DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test");
    ds.setUsername("sa");
    ds.setPassword("sa");
    ds.setDefaultAutoCommit(false);
    return new TransactionAwareDataSourceProxy(ds);
}
 
Example 9
Source File: PartitionTemplateWithEmfTest.java    From score with Apache License 2.0 5 votes vote down vote up
@Bean
DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test");
    ds.setUsername("sa");
    ds.setPassword("sa");
    ds.setDefaultAutoCommit(false);
    return new TransactionAwareDataSourceProxy(ds);
}
 
Example 10
Source File: ExecutionStateRepositoryTest.java    From score with Apache License 2.0 5 votes vote down vote up
@Bean
DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test");
    ds.setUsername("sa");
    ds.setPassword("sa");
    ds.setDefaultAutoCommit(false);
    return ds;
}
 
Example 11
Source File: PersistenceConfig.java    From batchers with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driverClassName);
    ds.setUrl(url);
    ds.setUsername(user);
    ds.setPassword(password);
    ds.setDefaultAutoCommit(false);
    return ds;
}
 
Example 12
Source File: MysqlDatasetStateStoreTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void setUp() throws Exception {
  testMetastoreDatabase = TestMetastoreDatabaseFactory.get();
  String jdbcUrl = testMetastoreDatabase.getJdbcUrl();
  ConfigBuilder configBuilder = ConfigBuilder.create();
  BasicDataSource mySqlDs = new BasicDataSource();

  mySqlDs.setDriverClassName(ConfigurationKeys.DEFAULT_STATE_STORE_DB_JDBC_DRIVER);
  mySqlDs.setDefaultAutoCommit(false);
  mySqlDs.setUrl(jdbcUrl);
  mySqlDs.setUsername(TEST_USER);
  mySqlDs.setPassword(TEST_PASSWORD);

  dbJobStateStore = new MysqlStateStore<>(mySqlDs, TEST_STATE_STORE, false, JobState.class);

  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_DB_URL_KEY, jdbcUrl);
  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_DB_USER_KEY, TEST_USER);
  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, TEST_PASSWORD);

  ClassAliasResolver<DatasetStateStore.Factory> resolver =
      new ClassAliasResolver<>(DatasetStateStore.Factory.class);

  DatasetStateStore.Factory stateStoreFactory =
        resolver.resolveClass("mysql").newInstance();
  dbDatasetStateStore = stateStoreFactory.createStateStore(configBuilder.build());

  // clear data that may have been left behind by a prior test run
  dbJobStateStore.delete(TEST_JOB_NAME);
  dbDatasetStateStore.delete(TEST_JOB_NAME);
  dbJobStateStore.delete(TEST_JOB_NAME2);
  dbDatasetStateStore.delete(TEST_JOB_NAME2);
}
 
Example 13
Source File: CleanableMysqlDatasetStoreDatasetTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void setUp() throws Exception {
  this.testMetastoreDatabase = TestMetastoreDatabaseFactory.get();
  String jdbcUrl = this.testMetastoreDatabase.getJdbcUrl();
  ConfigBuilder configBuilder = ConfigBuilder.create();
  BasicDataSource mySqlDs = new BasicDataSource();

  mySqlDs.setDriverClassName(ConfigurationKeys.DEFAULT_STATE_STORE_DB_JDBC_DRIVER);
  mySqlDs.setDefaultAutoCommit(false);
  mySqlDs.setUrl(jdbcUrl);
  mySqlDs.setUsername(TEST_USER);
  mySqlDs.setPassword(TEST_PASSWORD);

  this.dbJobStateStore = new MysqlStateStore<>(mySqlDs, TEST_STATE_STORE, false, JobState.class);

  configBuilder.addPrimitive("selection.timeBased.lookbackTime", "10m");
  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_TYPE_KEY, "mysql");
  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_DB_TABLE_KEY, TEST_STATE_STORE);
  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_DB_URL_KEY, jdbcUrl);
  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_DB_USER_KEY, TEST_USER);
  configBuilder.addPrimitive(ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, TEST_PASSWORD);

  ClassAliasResolver<DatasetStateStore.Factory> resolver = new ClassAliasResolver<>(DatasetStateStore.Factory.class);

  DatasetStateStore.Factory stateStoreFactory = resolver.resolveClass("mysql").newInstance();
  this.config = configBuilder.build();
  this.dbDatasetStateStore = stateStoreFactory.createStateStore(configBuilder.build());

  // clear data that may have been left behind by a prior test run
  this.dbJobStateStore.delete(TEST_JOB_NAME1);
  this.dbDatasetStateStore.delete(TEST_JOB_NAME1);
  this.dbJobStateStore.delete(TEST_JOB_NAME2);
  this.dbDatasetStateStore.delete(TEST_JOB_NAME2);
}
 
Example 14
Source File: DataSourceConfig.java    From oneops with Apache License 2.0 5 votes vote down vote up
private BasicDataSource getBaseDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setUsername(dbUser);
    ds.setPassword(dbPass);
    ds.setDriverClassName(DRIVER);
    ds.setTestOnBorrow(true);
    ds.setValidationQuery("select 1");
    ds.setDefaultAutoCommit(false);
    return ds;
}
 
Example 15
Source File: MySQLDataSources.java    From pinlater with Apache License 2.0 4 votes vote down vote up
private static DataSource createDataSource(
    String host, int port, String user, String passwd, int poolSize,
    int maxWaitMillis, int socketTimeoutMillis) {
  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  dataSource.setUrl(String.format(
      "jdbc:mysql://%s:%d?"
          + "connectTimeout=5000&"
          + "socketTimeout=%d&"
          + "enableQueryTimeouts=false&"
          + "cachePrepStmts=true&"
          + "characterEncoding=UTF-8",
      host,
      port,
      socketTimeoutMillis));
  dataSource.setUsername(user);
  dataSource.setPassword(passwd);
  dataSource.setDefaultAutoCommit(true);
  dataSource.setInitialSize(poolSize);
  dataSource.setMaxActive(poolSize);
  dataSource.setMaxIdle(poolSize);
  // deal with idle connection eviction
  dataSource.setValidationQuery("SELECT 1 FROM DUAL");
  dataSource.setTestOnBorrow(false);
  dataSource.setTestOnReturn(false);
  dataSource.setTestWhileIdle(true);
  dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
  dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
  dataSource.setNumTestsPerEvictionRun(poolSize);
  // max wait in milliseconds for a connection.
  dataSource.setMaxWait(maxWaitMillis);
  // 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 mysql connection when creating DataSource, "
            + "host: %s, port: %d", host, port),
        e);
  } finally {
    JdbcUtils.closeConnection(conn);
  }
  return dataSource;
}
 
Example 16
Source File: PooledConnectionDBCP.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static void setupDataSource(Isolation isolation) {
  ds = new BasicDataSource();
  Endpoint locatorEndPoint = (Endpoint) (NetworkServerHelper.getNetworkLocatorEndpoints()).get(0);
  String hostname = getHostNameFromEndpoint(locatorEndPoint);
  int port = getPortFromEndpoint(locatorEndPoint); 
  
  connProp.putAll(getExtraConnProp());
  
  try {
    ds.setDriverClassName(driver);
    ds.setUrl(protocol + hostname+ ":" + port);
    
    ds.setMaxActive(100);
    
    if (isolation == Isolation.NONE) {
    } else if (isolation == Isolation.READ_COMMITTED) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
      ds.setDefaultAutoCommit(false);
    } else {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
      ds.setDefaultAutoCommit(false);
    }
    
    StringBuilder sb = new StringBuilder();
    
    for (Iterator<?> iter = connProp.entrySet().iterator(); iter.hasNext(); ) {
      Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
      ds.addConnectionProperty(entry.getKey(), entry.getValue()); //add additional conn prop
      
      sb.append(entry.getKey() + " is set to " + entry.getValue() +"\n");
    }
         
    dsSet = true;
    Log.getLogWriter().info("basic source url is set as " + ds.getUrl());
    Log.getLogWriter().info("basic data source setting the following connection prop: " + sb.toString());
    
  } catch (Exception e) {
    throw new TestException("could not set data source" + TestHelper.getStackTrace(e));
  }
}
 
Example 17
Source File: PooledConnectionDBCP.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static void setupDataSource(Isolation isolation) {
  ds = new BasicDataSource();
  Endpoint locatorEndPoint = (Endpoint) (NetworkServerHelper.getNetworkLocatorEndpoints()).get(0);
  String hostname = getHostNameFromEndpoint(locatorEndPoint);
  int port = getPortFromEndpoint(locatorEndPoint); 
  
  connProp.putAll(getExtraConnProp());
  
  try {
    ds.setDriverClassName(driver);
    ds.setUrl(protocol + hostname+ ":" + port);
    
    ds.setMaxActive(100);
    
    if (isolation == Isolation.NONE) {
    } else if (isolation == Isolation.READ_COMMITTED) {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
      ds.setDefaultAutoCommit(false);
    } else {
      ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
      ds.setDefaultAutoCommit(false);
    }
    
    StringBuilder sb = new StringBuilder();
    
    for (Iterator<?> iter = connProp.entrySet().iterator(); iter.hasNext(); ) {
      Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
      ds.addConnectionProperty(entry.getKey(), entry.getValue()); //add additional conn prop
      
      sb.append(entry.getKey() + " is set to " + entry.getValue() +"\n");
    }
         
    dsSet = true;
    Log.getLogWriter().info("basic source url is set as " + ds.getUrl());
    Log.getLogWriter().info("basic data source setting the following connection prop: " + sb.toString());
    
  } catch (Exception e) {
    throw new TestException("could not set data source" + TestHelper.getStackTrace(e));
  }
}