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

The following examples show how to use org.apache.commons.dbcp.BasicDataSource#setDriverClassName() . 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: DBCPDataSource.java    From maven-framework-project with MIT License 6 votes vote down vote up
private DBCPDataSource() throws IOException, SQLException,
		PropertyVetoException {

	ds = new BasicDataSource();
	ds.setDriverClassName("org.h2.Driver");
	ds.setUsername("sa");
	ds.setPassword("");
	ds.setUrl("jdbc:h2:./target/test;AUTO_SERVER=TRUE");

	// the settings below are optional -- dbcp can work with defaults
	ds.setMinIdle(5);
	ds.setMaxIdle(20);
	ds.setMaxOpenPreparedStatements(180);

	this.setDataSource(ds);

}
 
Example 2
Source File: DataSourceContainer.java    From DBus with Apache License 2.0 6 votes vote down vote up
public boolean register(JdbcVo conf) {
    boolean isOk = true;
    try {
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName(conf.getDriverClass());
        bds.setUrl(conf.getUrl());
        bds.setUsername(conf.getUserName());
        bds.setPassword(conf.getPassword());
        bds.setInitialSize(conf.getInitialSize());
        bds.setMaxActive(conf.getMaxActive());
        bds.setMaxIdle(conf.getMaxIdle());
        bds.setMinIdle(conf.getMinIdle());
        cmap.put(conf.getKey(), bds);
    } catch (Exception e) {
        logger.error("[db container init key " + conf.getKey() + " datasource error!]", e);
        isOk = false;
    }
    return isOk;
}
 
Example 3
Source File: JdbcPushDownConnectionManager.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private JdbcPushDownConnectionManager(KylinConfig config, String id) throws ClassNotFoundException {
    dataSource = new BasicDataSource();

    Class.forName(config.getJdbcDriverClass(id));
    dataSource.setDriverClassName(config.getJdbcDriverClass(id));
    dataSource.setUrl(config.getJdbcUrl(id));
    dataSource.setUsername(config.getJdbcUsername(id));
    dataSource.setPassword(config.getJdbcPassword(id));
    dataSource.setMaxActive(config.getPoolMaxTotal(id));
    dataSource.setMaxIdle(config.getPoolMaxIdle(id));
    dataSource.setMinIdle(config.getPoolMinIdle(id));

    // Default settings
    dataSource.setTestOnBorrow(true);
    dataSource.setValidationQuery("select 1");
    dataSource.setRemoveAbandoned(true);
    dataSource.setRemoveAbandonedTimeout(300);
}
 
Example 4
Source File: JDBCDataSourceProvider.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource get() {
    BasicDataSource datasource = new BasicDataSource();
    datasource.setDriverClassName(config.getDriverClassName());
    datasource.setUsername(config.getUsername());
    datasource.setPassword(config.getPassword());
    datasource.setUrl(config.getConnection());
    datasource.setConnectionProperties(config.getConnectionProperties());
    LOGGER.info("Register JDBCDataSourceShutdownHook");
    Runtime.getRuntime().addShutdownHook(new Thread("JDBCDataSourceShutdownHook") {
        @Override
        public void run() {
            try {
                LOGGER.info("Shutting down data source");
                datasource.close();
            } catch (SQLException e) {
                LOGGER.error("SQLException: {}", e.getMessage(), e);
                throw new IllegalStateException("Failed to close datasource", e);
            }
        }
    });
    return datasource;
}
 
Example 5
Source File: SqlBasedRetentionPoc.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * <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'");
}
 
Example 6
Source File: JdbcStorageUpgradeTest.java    From apicurio-studio with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a datasource from a DB file found in the test resources.
 * @throws SQLException
 */
private static BasicDataSource createDatasourceFrom(String sqlFileName) throws Exception {
    // Step 1 - copy the db file from the classpath to a temp location
    URL sqlUrl = JdbcStorageUpgradeTest.class.getResource(sqlFileName);
    Assert.assertNotNull(sqlUrl);
    File tempDbFile = File.createTempFile("_apicurio_junit", ".mv.db");
    tempDbFile.deleteOnExit();

    String jdbcUrl = "jdbc:h2:file:" + tempDbFile.getAbsolutePath().replaceAll(".mv.db", "");
    
    // Step 2 - create a datasource from the file path
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("sa");
    ds.setUrl(jdbcUrl);
    
    try (Connection connection = ds.getConnection(); Reader reader = new InputStreamReader(sqlUrl.openStream())) {
        RunScript.execute(connection, reader);
    }
    
    return ds;
}
 
Example 7
Source File: TaskExecutor.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public void registered(final ExecutorDriver executorDriver, final Protos.ExecutorInfo executorInfo, final Protos.FrameworkInfo frameworkInfo, final Protos.SlaveInfo slaveInfo) {
    if (!executorInfo.getData().isEmpty()) {
        Map<String, String> data = SerializationUtils.deserialize(executorInfo.getData().toByteArray());
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(data.get("event_trace_rdb_driver"));
        dataSource.setUrl(data.get("event_trace_rdb_url"));
        dataSource.setPassword(data.get("event_trace_rdb_password"));
        dataSource.setUsername(data.get("event_trace_rdb_username"));
        jobEventBus = new JobEventBus(new JobEventRdbConfiguration(dataSource));
    }
}
 
Example 8
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 9
Source File: DSSMybatisConfig.java    From DataSphereStudio with Apache License 2.0 5 votes vote down vote up
@Bean(name = "dataSource", destroyMethod = "close")
@Primary
public DataSource dataSource() {
    BasicDataSource datasource = new BasicDataSource();
    String dbUrl = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_URL.getValue();
    String username = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_USERNAME.getValue();
    String password = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_PASSWORD.getValue();
    String driverClassName = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_DRIVER_CLASS_NAME.getValue();
    int initialSize = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_INITIALSIZE.getValue();
    int minIdle = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MINIDLE.getValue();
    int maxActive = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MAXACTIVE.getValue();
    int maxWait = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MAXWAIT.getValue();
    int timeBetweenEvictionRunsMillis = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TBERM.getValue();
    int minEvictableIdleTimeMillis = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MEITM.getValue();
    String validationQuery = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_VALIDATIONQUERY.getValue();
    boolean testWhileIdle = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TESTWHILEIDLE.getValue();
    boolean testOnBorrow = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TESTONBORROW.getValue();
    boolean testOnReturn = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TESTONRETURN.getValue();
    boolean poolPreparedStatements = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_POOLPREPAREDSTATEMENTS.getValue();
    datasource.setUrl(dbUrl);
    datasource.setUsername(username);
    datasource.setPassword(password);
    datasource.setDriverClassName(driverClassName);
    datasource.setInitialSize(initialSize);
    datasource.setMinIdle(minIdle);
    datasource.setMaxActive(maxActive);
    datasource.setMaxWait(maxWait);
    datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    datasource.setValidationQuery(validationQuery);
    datasource.setTestWhileIdle(testWhileIdle);
    datasource.setTestOnBorrow(testOnBorrow);
    datasource.setTestOnReturn(testOnReturn);
    datasource.setPoolPreparedStatements(poolPreparedStatements);
    logger.info("Database connection address information(数据库连接地址信息)=" + dbUrl);
    return datasource;
}
 
Example 10
Source File: ConnectionAndTxLifecycleIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
    dataSource.setUrl("jdbc:hsqldb:mem:test");
    // BasicDataSource opens and closes a test connection on first getConnection(),
    // so just getting that out of the way before starting transaction
    dataSource.getConnection().close();
    transactionMarker();
}
 
Example 11
Source File: Main.java    From sharding-jdbc-1.5.1 with Apache License 2.0 5 votes vote down vote up
private static DataSource createTransactionLogDataSource() {
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
    result.setUrl("jdbc:mysql://localhost:3306/trans_log");
    result.setUsername("root");
    result.setPassword("");
    return result;
}
 
Example 12
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 13
Source File: ValidationProfilesResourceTest.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an in-memory datasource.
 * @throws SQLException
 */
private static BasicDataSource createInMemoryDatasource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:vptest" + (counter++) + ";DB_CLOSE_DELAY=-1");
    return ds;
}
 
Example 14
Source File: DbCommonServiceImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource(String url, String driverClassName, String username, String password) {
	BasicDataSource ds = new SerializableBasicDataSource();
	ds.setUsername(username);
	ds.setPassword(password);
	ds.setUrl(url);
	ds.setDriverClassName(driverClassName);
	ds.setMaxActive(5);
	ds.setMaxIdle(2);
	return ds;
}
 
Example 15
Source File: RDBTracingListenerTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws SQLException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(org.h2.Driver.class.getName());
    dataSource.setUrl("jdbc:h2:mem:job_event_storage");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    RDBTracingListener tracingListener = new RDBTracingListener(dataSource);
    setRepository(tracingListener);
    jobEventBus = new JobEventBus(new TracingConfiguration<DataSource>("RDB", dataSource));
}
 
Example 16
Source File: TestUtils.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public static void initiateH2Base() throws Exception {

        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUsername("username");
        dataSource.setPassword("password");
        dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME);
        try (Connection connection = dataSource.getConnection()) {
            connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + getFilePath(H2_SCRIPT_NAME) + "'");
        }
        dataSourceMap.put(DB_NAME, dataSource);
    }
 
Example 17
Source File: JdbcStorageTest.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an in-memory datasource.
 * @throws SQLException
 */
private static BasicDataSource createInMemoryDatasource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:test" + (counter++) + ";DB_CLOSE_DELAY=-1");
    return ds;
}
 
Example 18
Source File: DdlUtilsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
static void runImportExportTest_Import(final Connection netConn,
    final String clientUrl, final String userName, final String password,
    final String schemaFileName, final String dataFileName) throws Throwable {

  // now load the tables using the XML data

  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.pivotal.gemfirexd.jdbc.ClientDriver");
  dataSource.setUrl("jdbc:gemfirexd://" + clientUrl);
  if (userName != null) {
    dataSource.setUsername(userName);
    dataSource.setPassword(password);
  }

  DdlToDatabaseTask toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  File schemaFile = new File(schemaFileName);
  toDBTask.setSchemaFile(schemaFile);

  WriteSchemaToDatabaseCommand writeSchemaToDB =
      new WriteSchemaToDatabaseCommand();
  writeSchemaToDB.setAlterDatabase(true);
  writeSchemaToDB.setFailOnError(true);

  toDBTask.addWriteSchemaToDatabase(writeSchemaToDB);
  toDBTask.execute();

  WriteDataToDatabaseCommand writeDataToDB = new WriteDataToDatabaseCommand();
  File dataFile = new File(dataFileName);
  writeDataToDB.setIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
  writeDataToDB.setUseBatchMode(true);
  writeDataToDB.setBatchSize(1000);
  writeDataToDB.setDataFile(dataFile);
  writeDataToDB.setFailOnError(true);

  // next check for the success case
  toDBTask = new DdlToDatabaseTask();
  toDBTask.addConfiguredDatabase(dataSource);
  toDBTask.setSchemaFile(schemaFile);

  toDBTask.addWriteDataToDatabase(writeDataToDB);
  toDBTask.execute();

  dataSource.close();

  // end load the tables using the XML data
}
 
Example 19
Source File: DefaultDataSourceFactory.java    From smart-framework with Apache License 2.0 4 votes vote down vote up
@Override
public void setDriver(BasicDataSource ds, String driver) {
    ds.setDriverClassName(driver);
}
 
Example 20
Source File: DefaultDataSourceFactory.java    From smart-framework with Apache License 2.0 4 votes vote down vote up
@Override
public void setDriver(BasicDataSource ds, String driver) {
    ds.setDriverClassName(driver);
}