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.nema.medical.mint.server.ServerConfig.java

@Bean(destroyMethod = "close")
public SessionFactory sessionFactory() throws Exception {
    if (sessionFactory == null) {
        final AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();

        if (StringUtils.isBlank(getConfigString("hibernate.connection.datasource"))) {
            // Not using JNDI data source
            final BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName(getConfigString("hibernate.connection.driver_class"));
            String url = getConfigString("hibernate.connection.url");
            url = url.replace("$MINT_HOME", mintHome().getPath());
            dataSource.setUrl(url);/*from  w  w w . java2  s .c om*/

            dataSource.setUsername(getConfigString("hibernate.connection.username"));
            dataSource.setPassword(getConfigString("hibernate.connection.password"));
            annotationSessionFactoryBean.setDataSource(dataSource);
        } else {
            // Using a JNDI dataSource
            final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
            jndiObjectFactoryBean.setExpectedType(DataSource.class);
            jndiObjectFactoryBean.setJndiName(getConfigString("hibernate.connection.datasource"));
            jndiObjectFactoryBean.afterPropertiesSet();
            annotationSessionFactoryBean.setDataSource((DataSource) jndiObjectFactoryBean.getObject());
        }

        final Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.connection.autocommit", Boolean.TRUE);

        final String dialect = getConfigString("hibernate.dialect");
        if (StringUtils.isNotBlank(dialect)) {
            hibernateProperties.put("hibernate.dialect", dialect);
        }

        final String hbm2dll = getConfigString("hibernate.hbm2ddl.auto");
        hibernateProperties.put("hibernate.hbm2ddl.auto", hbm2dll == null ? "verify" : hbm2dll);

        hibernateProperties.put("hibernate.show_sql",
                "true".equalsIgnoreCase(getConfigString("hibernate.show_sql")));

        hibernateProperties.put("hibernate.c3p0.max_statement", 50);
        hibernateProperties.put("hibernate.c3p0.maxPoolSize", 20);
        hibernateProperties.put("hibernate.c3p0.minPoolSize", 5);
        hibernateProperties.put("hibernate.c3p0.testConnectionOnCheckout", Boolean.FALSE);
        hibernateProperties.put("hibernate.c3p0.timeout", 600);
        annotationSessionFactoryBean.setHibernateProperties(hibernateProperties);

        annotationSessionFactoryBean.setPackagesToScan(getPackagesToScan());
        annotationSessionFactoryBean.afterPropertiesSet();

        sessionFactory = annotationSessionFactoryBean.getObject();
    }
    return sessionFactory;
}

From source file:org.ng200.openolympus.Application.java

@Bean
public DataSource dataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUsername("postgres");
    dataSource.setPassword(this.postgresPassword);
    dataSource.setUrl("jdbc:postgresql://" + this.postgresAddress + "/openolympus");
    return dataSource;
}

From source file:org.ngrinder.infra.config.Database.java

/**
 * Setup the database common features.//from w  w w  .j  a  va 2  s. c  o  m
 *
 * @param dataSource datasource
 */
protected void setupCommon(BasicDataSource dataSource) {
    dataSource.setDriverClassName(getJdbcDriverName());
    dataSource.setInitialSize(DB_INITIAL_SIZE);
    dataSource.setMaxActive(DB_MAX_ACTIVE);
    dataSource.setMinIdle(DB_MIN_IDLE);
    dataSource.setMaxWait(DB_MAX_WAIT);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setMaxOpenPreparedStatements(DB_MAX_OPEN_PREPARED_STATEMENTS);
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(true);
    dataSource.setTestOnReturn(true);
    dataSource.setValidationQuery("SELECT 1");
}

From source file:org.nimbustools.messaging.gt4_0_elastic.v2008_05_05.general.defaults.DefaultElasticPersistence.java

public DefaultElasticPersistence(Resource dbResource) throws IOException {
    if (dbResource == null) {
        throw new IllegalArgumentException("diskStoreResource may not be null");
    }/*from  w  w  w.  j  a  v  a2s .  com*/

    final String dbPath = dbResource.getFile().getAbsolutePath();

    //don't know how to feed this in with Spring and still have it fixup the
    // $NIMBUS_HOME in path
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.sqlite.JDBC");
    ds.setUrl("jdbc:sqlite://" + dbPath);

    this.dataSource = ds;
}

From source file:org.olap4j.test.TestContext.java

/**
 * Factory method for the {@link Tester}
 * object which determines which driver to test.
 *
 * @param testContext Test context//from   w  ww  .  j a  v a  2  s  .c  o  m
 * @param testProperties Properties that define the properties of the tester
 * @return a new Tester
 */
private static Tester createTester(TestContext testContext, Properties testProperties) {
    String helperClassName = testProperties.getProperty(Property.HELPER_CLASS_NAME.path);
    if (helperClassName == null) {
        helperClassName = "org.olap4j.XmlaTester";
        if (!testProperties.containsKey(TestContext.Property.XMLA_CATALOG_URL.path)) {
            testProperties.setProperty(TestContext.Property.XMLA_CATALOG_URL.path, "dummy_xmla_catalog_url");
        }
    }
    Tester tester;
    try {
        Class<?> clazz = Class.forName(helperClassName);
        final Constructor<?> constructor = clazz.getConstructor(TestContext.class);
        tester = (Tester) constructor.newInstance(testContext);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }

    // Apply a wrapper, if the "org.olap4j.test.wrapper" property is
    // specified.
    String wrapperName = testProperties.getProperty(Property.WRAPPER.path);
    Wrapper wrapper;
    if (wrapperName == null || wrapperName.equals("")) {
        wrapper = Wrapper.NONE;
    } else {
        try {
            wrapper = Enum.valueOf(Wrapper.class, wrapperName);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Unknown wrapper value '" + wrapperName + "'");
        }
    }
    switch (wrapper) {
    case NONE:
        break;
    case DBCP:
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(tester.getDriverClassName());
        dataSource.setUrl(tester.getURL());
        // need access to underlying connection so that we can call
        // olap4j-specific methods
        dataSource.setAccessToUnderlyingConnectionAllowed(true);
        tester = new DelegatingTester(tester) {
            public Connection createConnection() throws SQLException {
                return dataSource.getConnection();
            }

            public Wrapper getWrapper() {
                return Wrapper.DBCP;
            }
        };
        break;
    }
    return tester;
}

From source file:org.onecmdb.utils.wsdl.OneCMDBTransform.java

public IDataSource getDataSource() throws Exception {
    if (this.source != null) {
        Properties p = new Properties();
        FileInputStream in = new FileInputStream(this.source);
        boolean loaded = false;
        try {// ww  w .jav  a2s .  c om
            p.loadFromXML(in);
            loaded = true;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            in.close();
        }
        if (!loaded) {
            in = new FileInputStream(this.source);
            try {
                p.load(in);
            } finally {
                in.close();
            }
        }
        return (getDataSource(p));
    }

    if (this.jdbcSource != null) {
        Properties p = new Properties();
        FileInputStream in = new FileInputStream(this.jdbcSource);
        try {
            p.loadFromXML(in);
        } finally {
            in.close();
        }
        BasicDataSource jdbcSrc = new BasicDataSource();
        jdbcSrc.setUrl(p.getProperty("db.url"));
        jdbcSrc.setDriverClassName(p.getProperty("db.driverClass"));
        jdbcSrc.setUsername(p.getProperty("db.user"));
        jdbcSrc.setPassword(p.getProperty("db.password"));

        JDBCDataSourceWrapper src = new JDBCDataSourceWrapper();
        src.setDataSource(jdbcSrc);

        return (src);
    }
    // Else plain file...
    if (fileSource == null) {
        throw new IOException("No data source specified!");
    }
    if ("xml".equalsIgnoreCase(sourceType) || fileSource.endsWith(".xml")) {
        XMLDataSource dSource = new XMLDataSource();
        dSource.addURL(new URL(fileSource));
        return (dSource);
    }
    if ("csv".equalsIgnoreCase(sourceType) || fileSource.endsWith(".csv")) {
        CSVDataSource dSource = new CSVDataSource();
        dSource.addURL(new URL("file:" + fileSource));
        if (csvProperty != null) {
            HashMap<String, String> map = toMap(csvProperty, ",");
            String headerLines = map.get("headerLines");
            String delimiter = map.get("delimiter");
            String colTextDel = map.get("colTextDel");
            if (headerLines != null) {
                dSource.setHeaderLines(Long.parseLong(headerLines));
            }
            dSource.setColDelimiter(delimiter);
            dSource.setTextDelimiter(colTextDel);
        }
        return (dSource);
    }
    throw new IOException("Data source <" + fileSource + "> extension not supported!");
}

From source file:org.openbravo.erpCommon.modules.ImportModule.java

/**
 * Inserts in database the Vector<DynaBean> with its dependencies
 * //from  ww w . j a v a 2 s.  c  om
 * @param dModulesToInstall
 * @param dependencies1
 * @param newModule
 * @throws Exception
 */
private void insertDynaModulesInDB(Vector<DynaBean> dModulesToInstall, Vector<DynaBean> dependencies1,
        Vector<DynaBean> dbPrefix, boolean newModule) throws Exception {
    final Properties obProperties = new Properties();
    obProperties.load(new FileInputStream(obDir + "/config/Openbravo.properties"));

    final String url = obProperties.getProperty("bbdd.url")
            + (obProperties.getProperty("bbdd.rdbms").equals("POSTGRE")
                    ? "/" + obProperties.getProperty("bbdd.sid")
                    : "");

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(obProperties.getProperty("bbdd.driver"));
    ds.setUrl(url);
    ds.setUsername(obProperties.getProperty("bbdd.user"));
    ds.setPassword(obProperties.getProperty("bbdd.password"));

    final Connection conn = ds.getConnection();

    Integer seqNo = new Integer(ImportModuleData.selectSeqNo(pool));

    for (final DynaBean module : dModulesToInstall) {
        seqNo += 10;
        module.set("ISDEFAULT", "N");
        module.set("STATUS", "I");
        module.set("SEQNO", seqNo);
        module.set("UPDATE_AVAILABLE", null);
        module.set("UPGRADE_AVAILABLE", null);
        log4j.info("Inserting in DB info for module: " + module.get("NAME"));

        String moduleId = (String) module.get("AD_MODULE_ID");

        // Clean temporary tables
        ImportModuleData.cleanModuleInstall(pool, moduleId);
        ImportModuleData.cleanModuleDBPrefixInstall(pool, moduleId);
        ImportModuleData.cleanModuleDependencyInstall(pool, moduleId);

        String type = (String) module.get("TYPE");
        String applyConfigScript = "Y";
        if ("T".equals(type)) {
            if (newModule && V3_TEMPLATE_ID.equals(moduleId)) {
                // When installing V3 template do not apply its config script
                applyConfigScript = "N";
            } else {
                org.openbravo.model.ad.module.Module template = OBDal.getInstance()
                        .get(org.openbravo.model.ad.module.Module.class, moduleId);
                applyConfigScript = template == null ? "Y" : template.isApplyConfigurationScript() ? "Y" : "N";
            }
        }

        // Insert data in temporary tables
        ImportModuleData.insertModuleInstall(pool, moduleId, (String) module.get("NAME"),
                (String) module.get("VERSION"), (String) module.get("DESCRIPTION"), (String) module.get("HELP"),
                (String) module.get("URL"), type, (String) module.get("LICENSE"),
                (String) module.get("ISINDEVELOPMENT"), (String) module.get("ISDEFAULT"), seqNo.toString(),
                (String) module.get("JAVAPACKAGE"), (String) module.get("LICENSETYPE"),
                (String) module.get("AUTHOR"), (String) module.get("STATUS"),
                (String) module.get("UPDATE_AVAILABLE"), (String) module.get("ISTRANSLATIONREQUIRED"),
                (String) module.get("AD_LANGUAGE"), (String) module.get("HASCHARTOFACCOUNTS"),
                (String) module.get("ISTRANSLATIONMODULE"), (String) module.get("HASREFERENCEDATA"),
                (String) module.get("ISREGISTERED"), (String) module.get("UPDATEINFO"),
                (String) module.get("UPDATE_VER_ID"), (String) module.get("REFERENCEDATAINFO"),
                applyConfigScript);

        // Set installed for modules being updated
        ImportModuleData.setModuleUpdated(pool, (String) module.get("AD_MODULE_ID"));

        addLog("@ModuleInstalled@ " + module.get("NAME") + " - " + module.get("VERSION"), MSG_SUCCESS);
    }
    for (final DynaBean module : dependencies1) {
        ImportModuleData.insertModuleDependencyInstall(pool, (String) module.get("AD_MODULE_DEPENDENCY_ID"),
                (String) module.get("AD_MODULE_ID"), (String) module.get("AD_DEPENDENT_MODULE_ID"),
                (String) module.get("STARTVERSION"), (String) module.get("ENDVERSION"),
                (String) module.get("ISINCLUDED"), (String) module.get("DEPENDANT_MODULE_NAME"));
    }
    for (final DynaBean module : dbPrefix) {
        ImportModuleData.insertModuleDBPrefixInstall(pool, (String) module.get("AD_MODULE_DBPREFIX_ID"),
                (String) module.get("AD_MODULE_ID"), (String) module.get("NAME"));
    }

    conn.close();
}

From source file:org.openbravo.service.system.SystemValidationTask.java

private Platform getPlatform() {
    final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(props.getProperty("bbdd.driver"));
    if (props.getProperty("bbdd.rdbms").equals("POSTGRE")) {
        ds.setUrl(props.getProperty("bbdd.url") + "/" + props.getProperty("bbdd.sid"));
    } else {/* w w  w. j ava  2  s  .co  m*/
        ds.setUrl(props.getProperty("bbdd.url"));
    }
    ds.setUsername(props.getProperty("bbdd.user"));
    ds.setPassword(props.getProperty("bbdd.password"));
    return PlatformFactory.createNewPlatformInstance(ds);
}

From source file:org.openbravo.test.system.SystemValidatorTest.java

private Database createDatabaseObject(Module module) {
    final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(props.getProperty("bbdd.driver"));
    if (props.getProperty("bbdd.rdbms").equals("POSTGRE")) {
        ds.setUrl(props.getProperty("bbdd.url") + "/" + props.getProperty("bbdd.sid"));
    } else {/*from   ww w  . ja va2 s .  co  m*/
        ds.setUrl(props.getProperty("bbdd.url"));
    }
    ds.setUsername(props.getProperty("bbdd.user"));
    ds.setPassword(props.getProperty("bbdd.password"));
    Platform platform = PlatformFactory.createNewPlatformInstance(ds);
    platform.getModelLoader().setOnlyLoadTableColumns(true);

    if (module != null) {
        final String dbPrefix = module.getModuleDBPrefixList().get(0).getName();
        final ExcludeFilter filter = DBSMOBUtil.getInstance()
                .getExcludeFilter(new File(props.getProperty("source.path")));
        filter.addPrefix(dbPrefix);

        return platform.loadModelFromDatabase(filter, dbPrefix, true, module.getId());
    }

    return platform.loadModelFromDatabase(null);
}

From source file:org.openlogics.gears.jdbc.TransactionInterceptorTest.java

@Before
public void setup() {
    final BasicDataSource basicDataSource = new BasicDataSource();
    try {/*from   w  ww.  j a va  2  s. c o  m*/
        basicDataSource.setUrl("jdbc:h2:mem:parametrostest");
        basicDataSource.setDriverClassName("org.h2.Driver");
        Connection connection = basicDataSource.getConnection();
        URL sql = getResource(TestStub.class, "students.sql");
        try {
            connection.createStatement().execute(Resources.toString(sql, US_ASCII));
            connection.close();

            URL resource = getResource(TestStub.class, "students.xml");
            FlatXmlDataSet build = new FlatXmlDataSetBuilder().build(resource);
            DataSourceDatabaseTester databaseTester = new DataSourceDatabaseTester(basicDataSource);
            databaseTester.setDataSet(build);
            databaseTester.onSetup();
        } catch (SQLException x) {

        }
    } catch (Exception x) {
        x.printStackTrace();
    }

    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override
        protected void configure() {
            TransactionInterceptor tob = new TransactionInterceptor();
            //requestInjection(tob);

            bindInterceptor(Matchers.any(), Matchers.annotatedWith(TransactionObservable.class), tob);

            //inject datastore
            bind(DataStore.class).toInstance(new JdbcDataStore(basicDataSource));
        }
    });

    injector.injectMembers(this);
    injector.injectMembers(fooDao);
}