Example usage for org.apache.commons.dbcp BasicDataSource setDriverClassName

List of usage examples for org.apache.commons.dbcp BasicDataSource setDriverClassName

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource setDriverClassName.

Prototype

public synchronized void setDriverClassName(String driverClassName) 

Source Link

Document

Sets the jdbc driver class name.

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

Usage

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 .ja v  a  2 s  .  com

    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);//from w  ww.  jav  a  2  s  . co m

    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 {//  w  ww  .ja  va  2 s.c  om
        //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   www.j a  v a2s .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);/*  w  w  w  . j a  v  a  2s. co 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.runtime.MysqlDatasetStateStoreTest.java

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

From source file:org.apache.jackrabbit.core.util.db.ConnectionFactory.java

/**
 * Creates and returns a pooling JDBC {@link DataSource} for accessing
 * the database identified by the given driver class and JDBC
 * connection URL. The driver class can be <code>null</code> if
 * a specific driver has not been configured.
 *
 * @param driverClass the JDBC driver class, or <code>null</code>
 * @param url the JDBC connection URL//from w  w w.j  a  va  2s  .c o  m
 * @return pooling DataSource for accessing the specified database
 */
private BasicDataSource getDriverDataSource(Class<?> driverClass, String url, String user, String password) {
    BasicDataSource ds = new BasicDataSource();
    created.add(ds);

    if (driverClass != null) {
        Driver instance = null;
        try {
            // Workaround for Apache Derby:
            // The JDBC specification recommends the Class.forName
            // method without the .newInstance() method call,
            // but it is required after a Derby 'shutdown'
            instance = (Driver) driverClass.newInstance();
        } catch (Throwable e) {
            // Ignore exceptions as there's no requirement for
            // a JDBC driver class to have a public default constructor
        }
        if (instance != null) {
            if (instance.jdbcCompliant()) {
                // JCR-3445 At the moment the PostgreSQL isn't compliant because it doesn't implement this method...                   
                ds.setValidationQueryTimeout(3);
            }
        }
        ds.setDriverClassName(driverClass.getName());
    }

    ds.setUrl(url);
    ds.setUsername(user);
    ds.setPassword(password);
    ds.setDefaultAutoCommit(true);
    ds.setTestOnBorrow(false);
    ds.setTestWhileIdle(true);
    ds.setTimeBetweenEvictionRunsMillis(600000); // 10 Minutes
    ds.setMinEvictableIdleTimeMillis(60000); // 1 Minute
    ds.setMaxActive(-1); // unlimited
    ds.setMaxIdle(GenericObjectPool.DEFAULT_MAX_IDLE + 10);
    ds.setValidationQuery(guessValidationQuery(url));
    ds.setAccessToUnderlyingConnectionAllowed(true);
    ds.setPoolPreparedStatements(true);
    ds.setMaxOpenPreparedStatements(-1); // unlimited
    return ds;
}

From source file:org.apache.james.mailrepository.jdbc.JDBCMailRepositoryTest.java

private BasicDataSource getDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(EmbeddedDriver.class.getName());
    ds.setUrl("jdbc:derby:target/testdb;create=true");
    ds.setUsername("james");
    ds.setPassword("james");
    return ds;//w  w w.  ja  v a 2 s .co  m
}

From source file:org.apache.james.rrt.jdbc.JDBCStepdefs.java

private BasicDataSource getDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(EmbeddedDriver.class.getName());
    ds.setUrl("jdbc:derby:target/" + UUID.randomUUID() + ";create=true");
    ds.setUsername("james");
    ds.setPassword("james");
    return ds;/* w  w  w  .j ava 2s  .c o m*/
}

From source file:org.apache.james.user.jdbc.DefaultUsersJdbcRepositoryTest.java

private BasicDataSource getDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(EmbeddedDriver.class.getName());
    ds.setUrl("jdbc:derby:memory:testdb;create=true");
    ds.setUsername("james");
    ds.setPassword("james");
    return ds;//w w w .j av  a2  s .co  m
}