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.taverna.provenance.api.ProvenanceAccess.java

/**
 * Initialises a named JNDI DataSource if not already set up externally. The
 * DataSource is named jdbc/taverna//from  www.  ja  va  2 s  . com
 * 
 * @param driverClassName
 *            - the classname for the driver to be used.
 * @param jdbcUrl
 *            - the jdbc connection url
 * @param username
 *            - the username, if required (otherwise null)
 * @param password
 *            - the password, if required (oteherwise null)
 * @param minIdle
 *            - if the driver supports multiple connections, then the
 *            minumum number of idle connections in the pool
 * @param maxIdle
 *            - if the driver supports multiple connections, then the
 *            maximum number of idle connections in the pool
 * @param maxActive
 *            - if the driver supports multiple connections, then the
 *            minumum number of connections in the pool
 */
public static void initDataSource(String driverClassName, String jdbcUrl, String username, String password,
        int minIdle, int maxIdle, int maxActive) {
    System.setProperty(INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory");
    System.setProperty("org.osjava.sj.jndi.shared", "true");

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driverClassName);
    ds.setDefaultTransactionIsolation(TRANSACTION_READ_UNCOMMITTED);
    ds.setMaxActive(maxActive);
    ds.setMinIdle(minIdle);
    ds.setMaxIdle(maxIdle);
    ds.setDefaultAutoCommit(true);
    if (username != null)
        ds.setUsername(username);
    if (password != null)
        ds.setPassword(password);
    ds.setUrl(jdbcUrl);

    try {
        new InitialContext().rebind("jdbc/taverna", ds);
    } catch (NamingException ex) {
        logger.error("Problem rebinding the jdbc context", ex);
    }
}

From source file:org.apache.wookie.server.Start.java

private static void configureServer() throws Exception {
    // create embedded jetty instance
    logger.info("Configuring Jetty server");
    server = new Server(port);

    // configure embedded jetty to handle wookie web application
    WebAppContext context = new WebAppContext();
    context.setServer(server);/*from  w  w w .jav a2 s  .c  o m*/
    context.setContextPath("/wookie");
    context.setWar("build/webapp/wookie");

    // enable and configure JNDI container resources
    context.setConfigurationClasses(new String[] { "org.mortbay.jetty.webapp.WebInfConfiguration",
            "org.mortbay.jetty.plus.webapp.EnvConfiguration", "org.mortbay.jetty.plus.webapp.Configuration",
            "org.mortbay.jetty.webapp.JettyWebXmlConfiguration",
            "org.mortbay.jetty.webapp.TagLibConfiguration" });
    if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JPA)) {
        logger.info("Configuring JPA persistence manager");

        // setup derby database directory and logging properties
        if (dbType.equals("derby") && dbUri.startsWith("jdbc:derby:")) {
            int dbUriArgsIndex = dbUri.indexOf(";", 11);
            if (dbUriArgsIndex == -1) {
                dbUriArgsIndex = dbUri.length();
            }
            String databasePath = dbUri.substring(11, dbUriArgsIndex);
            int databaseDirIndex = databasePath.lastIndexOf(File.separatorChar);
            if ((databaseDirIndex == -1) && (File.separatorChar != '/')) {
                databaseDirIndex = databasePath.lastIndexOf('/');
            }
            if (databaseDirIndex != -1) {
                String databaseDir = databasePath.substring(0, databaseDirIndex);
                File databaseDirFile = new File(databaseDir);
                if (!databaseDirFile.exists()) {
                    databaseDirFile.mkdirs();
                }
                String derbyLog = databaseDirFile.getAbsolutePath() + File.separator + "derby.log";
                System.setProperty("derby.stream.error.file", derbyLog);
            }
        }

        // Setup a database connection resource using DBCP
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(dbDriver);
        dataSource.setUrl(dbUri);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPassword);
        dataSource.setMaxActive(80);
        dataSource.setMaxIdle(80);
        dataSource.setInitialSize(5);
        dataSource.setMaxOpenPreparedStatements(0);

        // Set up connection pool
        GenericObjectPool pool = new GenericObjectPool();
        // setup factory and pooling DataSource
        DataSourceConnectionFactory factory = new DataSourceConnectionFactory(dataSource);
        @SuppressWarnings("unused")
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(factory, pool, null,
                null, false, true);
        DataSource poolingDataSource = new PoolingDataSource(pool);

        new Resource(JPAPersistenceManager.WIDGET_DATABASE_JNDI_DATASOURCE_NAME, poolingDataSource);
    } else if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JCR)) {
        logger.info("Configuring JCR persistence manager");

        // setup repository directory and derby logging properties
        File repositoryDirFile = new File("widgetRepository");
        if (!repositoryDirFile.exists()) {
            repositoryDirFile.mkdirs();
        }
        String derbyLog = repositoryDirFile.getAbsolutePath() + File.separator + "derby.log";
        System.setProperty("derby.stream.error.file", derbyLog);

        // setup Jackrabbit JCR repository JNDI resource
        String repositoryConfig = repositoryDirFile.getAbsolutePath() + File.separator + "repository.xml";
        Repository repository = new TransientRepository(repositoryConfig, repositoryDirFile.getAbsolutePath());
        new Resource(JCRPersistenceManager.WIDGET_REPOSITORY_JNDI_REPOSITORY_NAME, repository);
    }

    // configure embedded jetty web application handler
    server.addHandler(context);

    // configure embedded jetty authentication realm
    HashUserRealm authedRealm = new HashUserRealm("Authentication Required", "etc/jetty-realm.properties");
    server.setUserRealms(new UserRealm[] { authedRealm });

    logger.info("Configured Jetty server");
}

From source file:org.apigw.monitoring.config.PersistenceConfig.java

@Bean
public DataSource dataSource() {
    log.debug("Setting up datasource for with drive: {}, url: {}, username: {}", driver, url, username);
    BasicDataSource bdc = new BasicDataSource();
    bdc.setUsername(username);//from w  w w .ja v  a  2s. c o m
    bdc.setPassword(password);
    bdc.setUrl(url);
    bdc.setDriverClassName(driver);
    bdc.setValidationQuery(validationQuery);

    return bdc;
}

From source file:org.asimba.wa.integrationtest.server.AsimbaWaDerbyDb.java

private AsimbaWaDerbyDb() {
    String driverClassName = RunConfiguration.getInstance().getProperty("db.driverClass");
    String url = RunConfiguration.getInstance().getProperty("db.connectionString");
    String username = RunConfiguration.getInstance().getProperty("db.username");
    String password = RunConfiguration.getInstance().getProperty("db.password");

    BasicDataSource ds = null;
    ds = new BasicDataSource();
    ds.setDriverClassName(driverClassName);
    ds.setUrl(url);/*from w  w  w .  j  a  v a 2s .  co m*/
    ds.setUsername(username);
    ds.setPassword(password);

    _datasource = ds;
}

From source file:org.blocks4j.reconf.client.setup.DatabaseManager.java

private BasicDataSource createDataSource(DatabaseURL arg) {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(arg.getDriverClassName());
    ds.setUrl(arg.buildRuntimeURL());//from   ww w  .j a  v a 2 s .  co m
    ds.setUsername(arg.getLogin());
    ds.setPassword(arg.getPass());
    ds.setMaxActive(30);
    ds.setMinIdle(5);
    ds.setTestOnBorrow(true);
    ds.setMaxWait(5000);
    return ds;
}

From source file:org.bpmscript.test.hibernate.SpringSessionFactoryTestSupport.java

public void execute(ITestCallback<IServiceLookup> callback) throws Exception {

    //         final BasicDataSource dataSource = new BasicDataSource();
    //         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    //         dataSource.setUrl("jdbc:mysql://localhost:3306/bpmscript");
    //         dataSource.setUsername("bpmscript");
    //         dataSource.setPassword("sa");
    //        /*from w ww. ja  v  a 2s. co m*/
    //         Properties properties = new Properties();
    //         properties.setProperty("hibernate.hbm2ddl.auto", "create");
    //         properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    //         properties.setProperty("hibernate.show_sql", "false");

    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUrl("jdbc:h2:~/bpmscript");
    dataSource.setUsername("sa");
    dataSource.setPassword("");

    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create");
    properties.setProperty("hibernate.dialect", H2Dialect.class.getName());
    properties.setProperty("hibernate.show_sql", "false");

    //        final BasicDataSource dataSource = new BasicDataSource();
    //        dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
    //        dataSource.setUrl("jdbc:derby:test;create=true");
    //        dataSource.setUsername("sa");
    //        dataSource.setPassword("sa");
    //
    //        Properties properties = new Properties();
    //        properties.setProperty("hibernate.hbm2ddl.auto", "update");
    //        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
    //        properties.setProperty("hibernate.query.substitutions", "true 1, false 0");
    //        properties.setProperty("hibernate.show_sql", "false");

    ServiceLookup lookup = new ServiceLookup();
    final AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setLobHandler(new DefaultLobHandler());
    sessionFactoryBean.setHibernateProperties(properties);
    sessionFactoryBean.setAnnotatedClasses(classes);
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.afterPropertiesSet();

    SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean.getObject();
    lookup.addService("sessionFactory", sessionFactory);

    try {
        callback.execute(lookup);
    } finally {
        sessionFactory.close();
        sessionFactoryBean.destroy();
    }
}

From source file:org.bzewdu.tools.perftrend.data.oracledalHelper.java

/**
 *
 * @param connectURI/*  w  w w  . jav  a 2 s.c  o m*/
 * @return
 */
public static DataSource setupDataSource(final String connectURI) {
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    ds.setUsername(oracledalHelper.username);
    ds.setPassword(oracledalHelper.passwd);
    ds.setUrl(connectURI);
    return ds;
}

From source file:org.cambillaum.jpapersistor.persistence.configuration.PersistenceConfiguration.java

@Bean
public BasicDataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUrl("jdbc:hsqldb:mem:testdb");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    dataSource.setInitialSize(0);/* ww  w.j  a v  a2 s . c  o m*/
    dataSource.setMaxActive(15);
    dataSource.setMaxIdle(0);
    dataSource.setMinEvictableIdleTimeMillis(60000);
    return dataSource;
}

From source file:org.cbioportal.database.annotator.DataSourceConfiguration.java

public BasicDataSource dataSource() throws SQLException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUsername(username);/*from w  ww . java 2s  .c  o  m*/
    dataSource.setPassword(password);
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(connection_string);
    return dataSource;
}

From source file:org.cfr.capsicum.test.AbstractSimpleCayenneJUnitTests.java

protected static DataSource createDatasource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(EmbeddedDriver.class.getCanonicalName());
    ds.setUrl("jdbc:derby:memory:testdb;create=true");
    datasource = ds;//from   w  w  w .  j  a  v a  2 s.com
    return ds;
}