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

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

Introduction

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

Prototype

public void addConnectionProperty(String name, String value) 

Source Link

Document

Add a custom connection property to the set that will be passed to our JDBC driver.

Usage

From source file:org.geoserver.spatialite.SpatiaLiteDataStoreFactory.java

static void addConnectionProperties(BasicDataSource dataSource) {
    SQLiteConfig config = new SQLiteConfig();
    config.setSharedCache(true);//  w ww  . java 2 s  . c o  m
    config.enableLoadExtension(true);
    config.enableSpatiaLite(true);

    for (Map.Entry e : config.toProperties().entrySet()) {
        dataSource.addConnectionProperty((String) e.getKey(), (String) e.getValue());
    }
}

From source file:org.globus.workspace.testing.NimbusTestBase.java

private void shutdownDB() throws Exception {
    logger.info("Shutting down DB..");
    BasicDataSource ds = (BasicDataSource) applicationContext.getBean(DATA_SOURCE_BEAN_NAME);
    ds.addConnectionProperty("shutdown", "true");

    for (int i = 0; i < 1000; i++) {
        try {//from w  w w.  ja va  2 s .  c  om
            ds.getConnection();
            Thread.sleep(10);
        } catch (SQLException e) {
            if (e.getSQLState().equals("08006") || e.getSQLState().equals("XJ004")) {
                logger.info("DB succesfully shutdown. ('" + e.getSQLState() + "')");
                return;
            } else {
                logger.info("DB not shutdown yet ('" + e.getSQLState() + "')");
            }
        }
    }

    throw new Exception("Could not shutdown DB!");
}

From source file:org.nuxeo.runtime.datasource.DataSourceFactory.java

@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> env) throws Exception {
    Reference ref = (Reference) obj;
    if (!DataSource.class.getName().equals(ref.getClassName())) {
        return null;
    }//w  w  w  .  j av  a2s  . co  m

    TransactionManager transactionManager;
    try {
        transactionManager = TransactionHelper.lookupTransactionManager();
    } catch (NamingException e) {
        transactionManager = null;
    }

    boolean xa = ref.get(BasicManagedDataSourceFactory.PROP_XADATASOURCE) != null;
    log.info(String.format("Creating pooled %s datasource: %s/%s", xa ? "XA" : "non-XA",
            nameCtx.getNameInNamespace(), name));

    if (xa && transactionManager == null) {
        throw new RuntimeException(
                "Cannot configure XA datasource " + name + " without an available transaction manager");
    }

    // extract properties from Reference
    Map<String, String> properties = new HashMap<String, String>();
    Enumeration<RefAddr> refAddrs = ref.getAll();
    while (refAddrs.hasMoreElements()) {
        RefAddr ra = refAddrs.nextElement();
        String key = ra.getType();
        String value = ra.getContent().toString();
        if (key.startsWith(DataSourceDescriptor.PROP_PREFIX)) {
            key = key.substring(DataSourceDescriptor.PROP_PREFIX.length());
            properties.put(key, value);
        }
    }

    DataSource ds;
    if (!xa) {
        // fetch url from properties
        for (Entry<String, String> en : properties.entrySet()) {
            // often misspelled, thus the ignore case
            if (URL_LOWER.equalsIgnoreCase(en.getKey())) {
                ref.add(new StringRefAddr(URL_LOWER, en.getValue()));
            }
        }
        ObjectFactory factory = new BasicDataSourceFactory();
        ds = (DataSource) factory.getObjectInstance(ref, name, nameCtx, env);
        BasicDataSource bds = (BasicDataSource) ds;

        // set properties
        for (Entry<String, String> en : properties.entrySet()) {
            String key = en.getKey();
            if (URL_LOWER.equalsIgnoreCase(key)) {
                continue;
            }
            bds.addConnectionProperty(key, en.getValue());
        }
    } else {
        ObjectFactory factory = new BasicManagedDataSourceFactory();
        ds = (DataSource) factory.getObjectInstance(obj, name, nameCtx, env);
        if (ds == null) {
            return null;
        }
        BasicManagedDataSource bmds = (BasicManagedDataSource) ds;

        // set transaction manager
        bmds.setTransactionManager(transactionManager);

        // set properties
        XADataSource xaDataSource = bmds.getXaDataSourceInstance();
        if (xaDataSource == null) {
            return null;
        }
        for (Entry<String, String> en : properties.entrySet()) {
            String key = en.getKey();
            // proper JavaBean convention for initial cap
            if (Character.isLowerCase(key.charAt(1))) {
                key = Character.toLowerCase(key.charAt(0)) + key.substring(1);
            }
            String value = en.getValue();
            boolean ok = false;
            try {
                BeanUtils.setProperty(xaDataSource, key, value);
                ok = true;
            } catch (Exception e) {
                if (URL_LOWER.equals(key)) {
                    // commonly misspelled
                    try {
                        BeanUtils.setProperty(xaDataSource, URL_UPPER, value);
                        ok = true;
                    } catch (Exception ee) {
                        // log error below
                    }
                }
            }
            if (!ok) {
                log.error(String.format("Cannot set %s = %s on %s", key, value,
                        xaDataSource.getClass().getName()));
            }
        }
    }
    return ds;
}

From source file:org.seedstack.seed.persistence.jdbc.internal.datasource.DbcpDataSourceProvider.java

@Override
public DataSource provide(String driverClass, String url, String user, String password,
        Properties dataSourceProperties) {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(driverClass);
    basicDataSource.setUrl(url);//from   ww w.  j  av a  2s  .co  m
    basicDataSource.setUsername(user);
    basicDataSource.setPassword(password);
    for (Object key : dataSourceProperties.keySet()) {
        basicDataSource.addConnectionProperty((String) key, dataSourceProperties.getProperty((String) key));
    }
    return basicDataSource;
}