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

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

Introduction

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

Prototype

public synchronized void setAccessToUnderlyingConnectionAllowed(boolean allow) 

Source Link

Document

Sets the value of the accessToUnderlyingConnectionAllowed property.

Usage

From source file:org.geotoolkit.db.AbstractJDBCFeatureStoreFactory.java

/**
 * Create a datasource using given parameters.
 *///from  w w w  .  ja  va 2s  .com
protected DataSource createDataSource(final ParameterValueGroup params) throws IOException {
    //create a datasource
    final BasicDataSource dataSource = new BasicDataSource();

    // some default data source behaviour
    dataSource.setPoolPreparedStatements(false);

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    final String user = (String) params.parameter(USER.getName().toString()).getValue();
    dataSource.setUsername(user);

    // password
    final String passwd = (String) params.parameter(PASSWORD.getName().toString()).getValue();
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    final Integer maxWait = (Integer) params.parameter(MAXWAIT.getName().toString()).getValue();
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    final Integer minConn = (Integer) params.parameter(MINCONN.getName().toString()).getValue();
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    final Integer maxConn = (Integer) params.parameter(MAXCONN.getName().toString()).getValue();
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    final Boolean validate = (Boolean) params.parameter(VALIDATECONN.getName().toString()).getValue();
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // allow manipulating connections for possible tuning.
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    return new DBCPDataSource(dataSource);
}

From source file:org.geotools.data.jdbc.ds.UnWrapperTest.java

public void testDBCPUnwrapper() throws SQLException, IOException {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test_mem");
    ds.setAccessToUnderlyingConnectionAllowed(true);

    Connection conn = ds.getConnection();
    UnWrapper uw = DataSourceFinder.getUnWrapper(conn);
    assertNotNull(uw);//w w  w.ja v  a  2s .  c o  m
    assertTrue(uw.canUnwrap(conn));
    Connection unwrapped = uw.unwrap(conn);
    assertNotNull(unwrapped);
    assertTrue(unwrapped instanceof org.h2.jdbc.JdbcConnection);

    Statement st = conn.createStatement();
    uw = DataSourceFinder.getUnWrapper(st);
    assertNotNull(uw);
    assertTrue(uw.canUnwrap(st));
    Statement uwst = uw.unwrap(st);
    assertNotNull(uwst);
    assertTrue(uwst instanceof org.h2.jdbc.JdbcStatement);
    st.close();

    PreparedStatement ps = conn.prepareStatement("select curtime()");
    uw = DataSourceFinder.getUnWrapper(ps);
    assertNotNull(uw);
    assertTrue(uw.canUnwrap(ps));
    PreparedStatement uwps = (PreparedStatement) uw.unwrap(ps);
    assertNotNull(uwps);
    assertTrue(uwps instanceof org.h2.jdbc.JdbcPreparedStatement);
    ps.close();

    conn.close();
    ds.close();
}

From source file:org.geotools.jdbc.JDBCDataStoreFactory.java

/**
 * DataSource access allowing SQL use: intended to allow client code to query available schemas.
 * <p>/*ww  w.j a  v  a 2  s.  co  m*/
 * This DataSource is the clients responsibility to close() when they are finished using it.
 * </p> 
 * @param params Map of connection parameter.
 * @return DataSource for SQL use
 * @throws IOException
 */
public BasicDataSource createDataSource(Map params) throws IOException {
    //create a datasource
    BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    String user = (String) USER.lookUp(params);
    dataSource.setUsername(user);

    // password
    String passwd = (String) PASSWD.lookUp(params);
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    Integer maxWait = (Integer) MAXWAIT.lookUp(params);
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    Integer minConn = (Integer) MINCONN.lookUp(params);
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    Integer maxConn = (Integer) MAXCONN.lookUp(params);
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    Boolean validate = (Boolean) VALIDATECONN.lookUp(params);
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // some datastores might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    return dataSource;
}

From source file:org.geotools.jdbc.JDBCTestSetup.java

/**
 * Creates a data source by reading properties from a file called 'db.properties', 
 * located paralell to the test setup instance.
 *///  w w  w  .jav a  2 s .co m
protected DataSource createDataSource() throws IOException {
    Properties db = fixture;

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(db.getProperty("driver"));
    dataSource.setUrl(db.getProperty("url"));

    if (db.containsKey("user")) {
        dataSource.setUsername(db.getProperty("user"));
    } else if (db.containsKey("username")) {
        dataSource.setUsername(db.getProperty("username"));
    }
    if (db.containsKey("password")) {
        dataSource.setPassword(db.getProperty("password"));
    }

    dataSource.setPoolPreparedStatements(true);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setMinIdle(1);
    dataSource.setMaxActive(4);
    // if we cannot get a connection within 5 seconds give up
    dataSource.setMaxWait(5000);

    initializeDataSource(dataSource, db);

    // return a closeable data source (DisposableDataSource interface)
    // so that the connection pool will be tore down on datastore dispose
    return new DBCPDataSource(dataSource);
}

From source file:org.geowebcache.diskquota.jdbc.JDBCQuotaStoreFactory.java

/**
 * Prepares a simple data source for the embedded H2
 * /*from  ww w .  j a  v  a2 s. c o m*/
 * @param cacheDirFinder
 * @return
 * @throws ConfigurationException
 */
private DataSource getH2DataSource(DefaultStorageFinder cacheDirFinder) throws ConfigurationException {
    File storeDirectory = new File(cacheDirFinder.getDefaultPath(), "diskquota_page_store_h2");
    storeDirectory.mkdirs();

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName("org.h2.Driver");
    String database = new File(storeDirectory, "diskquota").getAbsolutePath();
    dataSource.setUrl("jdbc:h2:" + database);
    dataSource.setUsername("sa");
    dataSource.setPoolPreparedStatements(true);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setMinIdle(1);
    dataSource.setMaxActive(-1); // boundless
    dataSource.setMaxWait(5000);
    return dataSource;
}

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  ww  w  .j  a  va  2 s.c om*/
 * @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.unitils.database.core.DataSourceWrapperFactory.java

protected DataSource createDataSource(DatabaseConfiguration databaseConfiguration) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setDriverClassName(databaseConfiguration.getDriverClassName());
    dataSource.setUsername(databaseConfiguration.getUserName());
    dataSource.setPassword(databaseConfiguration.getPassword());
    dataSource.setUrl(databaseConfiguration.getUrl());
    return dataSource;
}

From source file:org.unitils.database.DatabaseUnitils.java

/**
 * This method gets a {@link Connection} from the {@link DataSource} and checks if it is a {@link oracle.jdbc.driver.OracleConnection}.
 * There is a bug with commons-dbcp 1.4: if you want to create a {@link oracle.sql.BLOB} or a {@link java.sql.Clob} than you must get the inner {@link Connection} but you get another {@link Connection}.
 * This is fixed in this method./*w ww. ja  v a  2 s .  c o m*/
 * @param connection
 * @param dataSource
 * @return
 */
public static Connection getGoodConnection(Connection connection, DataSource dataSource) {
    if (dataSource instanceof BasicDataSource) {
        BasicDataSource tempDataSource = (BasicDataSource) dataSource;
        if (tempDataSource.getDriverClassName().toLowerCase().contains("oracle")
                && connection instanceof DelegatingConnection) {
            boolean canAccess = tempDataSource.isAccessToUnderlyingConnectionAllowed();
            if (!canAccess) {
                tempDataSource.setAccessToUnderlyingConnectionAllowed(true);
            }

            DelegatingConnection tempConnection = (DelegatingConnection) connection;
            Connection innermostDelegate = tempConnection.getInnermostDelegate();
            if (!canAccess) {
                tempDataSource.setAccessToUnderlyingConnectionAllowed(false);
            }
            return innermostDelegate;
        }
    }
    return connection;
}

From source file:xbird.util.jdbc.datasource.DbcpDataSourceProvider.java

public DataSource setupDataSource(String connectURI) {
    // creates DataSource
    BasicDataSource ds = new BasicDataSource();

    // for debugging.
    if (Settings.isLoggingEnabled) {
        ds.setAccessToUnderlyingConnectionAllowed(true);
    }/*from ww  w  . ja v a  2 s  .com*/

    ds.setDriverClassName(DriverClassNameResolver.resolve(Settings.get("xbird.db.kind")));

    // sets up DataSource
    ds.setUrl(connectURI);
    final String dbuser = Settings.get("xbird.db.user");
    final String dbpasswd = Settings.get("xbird.db.passwd");
    if (dbuser != null && dbuser.length() != 0) {
        ds.setUsername(dbuser);
        ds.setPassword(dbpasswd);
    }

    // addtinal settings.
    final String maxactive = Settings.get("xbird.db.pool.maxactive");
    if (maxactive != null)
        ds.setMaxActive(Integer.parseInt(maxactive));

    final String maxidle = Settings.get("xbird.db.pool.maxidle");
    if (maxidle != null)
        ds.setMaxIdle(Integer.parseInt(maxidle));

    final String maxwait = Settings.get("xbird.db.pool.maxwait");
    ds.setMaxWait(maxwait == null ? DEFAULT_MAXWAIT : Integer.parseInt(maxwait));

    ds.setDefaultAutoCommit(true);
    //ds.setDefaultReadOnly(false);

    final String initialsize = Settings.get("xbird.db.pool.initialsize");
    ds.setInitialSize(initialsize == null ? DEFAULT_INITIAL_POLLSIZE : Integer.parseInt(initialsize));

    // sets up for PreparedStatements.
    ds.setPoolPreparedStatements(true);

    final String maxOpenPreparedStatements = Settings.get("xbird.db.pool.statement.cache_size");
    ds.setMaxOpenPreparedStatements(maxOpenPreparedStatements == null ? MAX_OPEN_PREPARED_STATEMENTS
            : Integer.parseInt(maxOpenPreparedStatements));

    return ds;
}