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.ambraproject.rhino.config.TestConfiguration.java

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:testdb");
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    return dataSource;
}

From source file:org.apache.airavata.common.utils.DBUtil.java

/**
 * Gets a new DBCP data source./*from  w w w.  j  ava  2  s  . c  o  m*/
 * 
 * @return A new data source.
 */
public DataSource getDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(this.driverName);
    ds.setUsername(this.databaseUserName);
    ds.setPassword(this.databasePassword);
    ds.setUrl(this.jdbcUrl);

    return ds;
}

From source file:org.apache.drill.exec.store.http.InsertTestData.java

public static DataSource createDataSource(String driver, String url, String userName, String password) {

    BasicDataSource source = new BasicDataSource();
    source.setDriverClassName(driver);/*from  ww  w  . j  av a  2 s  .  c  om*/
    source.setUrl(url);

    if (userName != null) {
        source.setUsername(userName);
    }

    if (password != null) {
        source.setPassword(password);
    }
    source.setInitialSize(1);
    source.setPoolPreparedStatements(true);
    try {
        // initial a connection
        source.getConnection();
    } catch (SQLException sqlE) {

        logger.error("db connection error: ", sqlE);
    }
    return source;

}

From source file:org.apache.drill.exec.store.http.util.DBUtil.java

public static DataSource createDataSource(String driver, String url, String userName, String password) {

    BasicDataSource source = new BasicDataSource();
    source.setDriverClassName(driver);/*from  ww w .  ja va2s  . c o m*/
    source.setUrl(url);

    if (userName != null) {
        source.setUsername(userName);
    }

    if (password != null) {
        source.setPassword(password);
    }

    source.setPoolPreparedStatements(true);
    source.setInitialSize(1);
    try {
        // initial a connection
        Connection conn = source.getConnection();
        conn.close();
    } catch (SQLException sqlE) {

        logger.error("db connection error: ", sqlE);
    }
    return source;

}

From source file:org.apache.drill.exec.store.jdbc.JdbcStoragePlugin.java

public JdbcStoragePlugin(JdbcStorageConfig config, DrillbitContext context, String name) {
    this.context = context;
    this.config = config;
    this.name = name;
    BasicDataSource source = new BasicDataSource();
    source.setDriverClassName(config.getDriver());
    source.setUrl(config.getUrl());// ww  w  .  j a  v  a2s .  co m

    if (config.getUsername() != null) {
        source.setUsername(config.getUsername());
    }

    if (config.getPassword() != null) {
        source.setPassword(config.getPassword());
    }

    this.source = source;
    this.dialect = JdbcSchema.createDialect(source);
    this.convention = new DrillJdbcConvention(dialect, name);
}

From source file:org.apache.drill.exec.store.jdbc.TestJdbcPlugin.java

@BeforeClass
public static void setupDefaultTestCluster() throws Exception {
    System.setProperty("derby.drda.startNetworkServer", "true");
    server = new NetworkServerControl(InetAddress.getByName("localhost"), 20000, "admin", "admin");
    java.io.PrintWriter consoleWriter = new java.io.PrintWriter(System.out, true);
    server.start(consoleWriter);//w w w  .  j av  a2s  .  c om

    BasicDataSource source = new BasicDataSource();
    source.setUrl("jdbc:derby://localhost:20000/memory:testDB;create=true");
    source.setDriverClassName("org.apache.derby.jdbc.ClientDriver");

    final String insertValues1 = "INSERT INTO person VALUES (1, 'Smith', null, '{number:\"123 Main\"}','mtrx', "
            + "'xy', 333.333, 444.444, 555.00, TIME('15:09:02'), DATE('1994-02-23'), TIMESTAMP('1962-09-23 03:23:34.234'),"
            + " 666.66, 1, -1, false)";
    final String insertValues2 = "INSERT INTO person (PersonId) VALUES (null)";
    try (Connection c = source.getConnection()) {
        c.createStatement()
                .execute("CREATE TABLE person\n" + "(\n" + "PersonID int,\n" + "LastName varchar(255),\n"
                        + "FirstName varchar(255),\n" + "Address varchar(255),\n" + "City varchar(255),\n"
                        + "Code char(2),\n" + "dbl double,\n" + "flt float,\n" + "rel real,\n" + "tm time,\n"
                        + "dt date,\n" + "tms timestamp,\n" + "num numeric(10,2), \n" + "sm smallint,\n"
                        + "bi bigint,\n" + "bool boolean\n" +

                        ")");

        c.createStatement().execute(insertValues1);
        c.createStatement().execute(insertValues2);
        c.createStatement().execute(insertValues1);
    }

    BaseTestQuery.setupDefaultTestCluster();
}

From source file:org.apache.eagle.alert.metadata.impl.JdbcMetadataHandler.java

public JdbcMetadataHandler(Config config) {
    try {/*from ww w . j  a v  a  2  s .  c  o m*/
        //JdbcSchemaManager.getInstance().init(config);
        BasicDataSource bDatasource = new BasicDataSource();
        bDatasource.setDriverClassName(config.getString(MetadataUtils.JDBC_DRIVER_PATH));
        if (config.hasPath(MetadataUtils.JDBC_USERNAME_PATH)) {
            bDatasource.setUsername(config.getString(MetadataUtils.JDBC_USERNAME_PATH));
            bDatasource.setPassword(config.getString(MetadataUtils.JDBC_PASSWORD_PATH));
        }
        bDatasource.setUrl(config.getString(MetadataUtils.JDBC_CONNECTION_PATH));
        if (config.hasPath(MetadataUtils.JDBC_CONNECTION_PROPERTIES_PATH)) {
            bDatasource
                    .setConnectionProperties(config.getString(MetadataUtils.JDBC_CONNECTION_PROPERTIES_PATH));
        }
        this.dataSource = bDatasource;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:org.apache.eagle.metadata.store.jdbc.provider.JDBCDataSourceProvider.java

@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//from w  w w. j  a va2  s.  c o  m
        public void run() {
            try {
                LOGGER.info("Shutting down data fromStream");
                datasource.close();
            } catch (SQLException e) {
                LOGGER.error("SQLException: {}", e.getMessage(), e);
                throw new IllegalStateException("Failed to close datasource", e);
            }
        }
    });
    return datasource;
}

From source file:org.apache.gobblin.data.management.retention.CleanableMysqlDatasetStoreDatasetTest.java

@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);//from  ww  w  . j  a  v a2 s. c  o  m
    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);
}

From source file:org.apache.gobblin.data.management.retention.sql.SqlBasedRetentionPoc.java

/**
 * <ul>//w w w. j  a  v a 2s .  c o m
 * <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'");
}