List of usage examples for org.apache.commons.dbcp BasicDataSource setAccessToUnderlyingConnectionAllowed
public synchronized void setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property.
From source file:net.certifi.audittablegen.HsqldbDMR.java
/** * Generate a Hsqldb DataSource from Properties * * @param props// w w w . j a va2s . co m * @return BasicDataSource as DataSource */ static DataSource getRunTimeDataSource(Properties props) { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); dataSource.setUsername(props.getProperty("username")); dataSource.setPassword(props.getProperty("password")); dataSource.setUrl(props.getProperty("url")); dataSource.setMaxActive(10); dataSource.setMaxIdle(5); dataSource.setInitialSize(5); dataSource.setAccessToUnderlyingConnectionAllowed(true); dataSource.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS"); return dataSource; }
From source file:com.taobao.datax.plugins.common.DBSource.java
/** * A synchronized static method which create {@link DataSource}. NOTE: * Client must make sure all connections to the same database should share * the same value a suggestion: use genKey we provide below * /*from w w w. j av a 2 s . com*/ * @param key * key to query database source * * @param p * Properties file. * * @return true if create DataSource successfully, false if failed. * * @throws IllegalStateException * */ public static synchronized boolean register(String key, Properties p) { boolean succeed = false; if (!sourceInfoMap.containsKey(key)) { BasicDataSource dataSource = null; try { dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p); } catch (Exception e) { logger.error(String.format("Key [%s] register database pool failed .", key)); throw new IllegalStateException(e.getCause()); } if (null != dataSource) { dataSource.setAccessToUnderlyingConnectionAllowed(true); sourceInfoMap.put(key, dataSource); logger.info(String.format("Key [%s] register database pool successfully .", key)); succeed = true; } else { logger.error(String.format("Key [%s] register database pool failed .", key)); } } else { logger.error(String.format("Key [%s] already in database pool .", key)); } return succeed; }
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 ww w.j a va 2 s . 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.synapse.commons.datasource.factory.DataSourceFactory.java
/** * Factory method to create a DataSource based on provided information * which is encapsulated in the DataSourceInformation object. * * @param dataSourceInformation Information about DataSource * @return DataSource Instance if one can be created , * otherwise null or exception if provided details are not valid or enough to create * a DataSource//from ww w . ja va 2 s .c o m */ public static DataSource createDataSource(DataSourceInformation dataSourceInformation) { String dsType = dataSourceInformation.getType(); String driver = dataSourceInformation.getDriver(); if (driver == null || "".equals(driver)) { handleException("Database driver class name cannot be found."); } String url = dataSourceInformation.getUrl(); if (url == null || "".equals(url)) { handleException("Database connection URL cannot be found."); } String user = dataSourceInformation.getSecretInformation().getUser(); String password = dataSourceInformation.getSecretInformation().getResolvedSecret(); int defaultTransactionIsolation = dataSourceInformation.getDefaultTransactionIsolation(); if (DataSourceInformation.BASIC_DATA_SOURCE.equals(dsType)) { BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setDriverClassName(driver); basicDataSource.setUrl(url); if (user != null && !"".equals(user)) { basicDataSource.setUsername(user); } if (password != null && !"".equals(password)) { basicDataSource.setPassword(password); } basicDataSource.setMaxActive(dataSourceInformation.getMaxActive()); basicDataSource.setMaxIdle(dataSourceInformation.getMaxIdle()); basicDataSource.setMaxWait(dataSourceInformation.getMaxWait()); basicDataSource.setMinIdle(dataSourceInformation.getMinIdle()); basicDataSource.setDefaultAutoCommit(dataSourceInformation.isDefaultAutoCommit()); basicDataSource.setDefaultReadOnly(dataSourceInformation.isDefaultReadOnly()); basicDataSource.setTestOnBorrow(dataSourceInformation.isTestOnBorrow()); basicDataSource.setTestOnReturn(dataSourceInformation.isTestOnReturn()); basicDataSource.setTestWhileIdle(dataSourceInformation.isTestWhileIdle()); basicDataSource.setMinEvictableIdleTimeMillis(dataSourceInformation.getMinEvictableIdleTimeMillis()); basicDataSource .setTimeBetweenEvictionRunsMillis(dataSourceInformation.getTimeBetweenEvictionRunsMillis()); basicDataSource.setNumTestsPerEvictionRun(dataSourceInformation.getNumTestsPerEvictionRun()); basicDataSource.setMaxOpenPreparedStatements(dataSourceInformation.getMaxOpenPreparedStatements()); basicDataSource.setAccessToUnderlyingConnectionAllowed( dataSourceInformation.isAccessToUnderlyingConnectionAllowed()); basicDataSource.setInitialSize(dataSourceInformation.getInitialSize()); basicDataSource.setPoolPreparedStatements(dataSourceInformation.isPoolPreparedStatements()); if (defaultTransactionIsolation != -1) { basicDataSource.setDefaultTransactionIsolation(defaultTransactionIsolation); } String defaultCatalog = dataSourceInformation.getDefaultCatalog(); if (defaultCatalog != null && !"".equals(defaultCatalog)) { basicDataSource.setDefaultCatalog(defaultCatalog); } String validationQuery = dataSourceInformation.getValidationQuery(); if (validationQuery != null && !"".equals(validationQuery)) { basicDataSource.setValidationQuery(validationQuery); } return basicDataSource; } else if (DataSourceInformation.PER_USER_POOL_DATA_SOURCE.equals(dsType)) { DriverAdapterCPDS adapterCPDS = new DriverAdapterCPDS(); try { adapterCPDS.setDriver(driver); } catch (ClassNotFoundException e) { handleException("Error setting driver : " + driver + " in DriverAdapterCPDS", e); } adapterCPDS.setUrl(url); if (user != null && !"".equals(user)) { adapterCPDS.setUser(user); } if (password != null && !"".equals(password)) { adapterCPDS.setPassword(password); } adapterCPDS.setPoolPreparedStatements(dataSourceInformation.isPoolPreparedStatements()); adapterCPDS.setMaxIdle(dataSourceInformation.getMaxIdle()); PerUserPoolDataSource perUserPoolDataSource = new PerUserPoolDataSource(); perUserPoolDataSource.setConnectionPoolDataSource(adapterCPDS); perUserPoolDataSource.setDefaultMaxActive(dataSourceInformation.getMaxActive()); perUserPoolDataSource.setDefaultMaxIdle(dataSourceInformation.getMaxIdle()); perUserPoolDataSource.setDefaultMaxWait((int) dataSourceInformation.getMaxWait()); perUserPoolDataSource.setDefaultAutoCommit(dataSourceInformation.isDefaultAutoCommit()); perUserPoolDataSource.setDefaultReadOnly(dataSourceInformation.isDefaultReadOnly()); perUserPoolDataSource.setTestOnBorrow(dataSourceInformation.isTestOnBorrow()); perUserPoolDataSource.setTestOnReturn(dataSourceInformation.isTestOnReturn()); perUserPoolDataSource.setTestWhileIdle(dataSourceInformation.isTestWhileIdle()); perUserPoolDataSource .setMinEvictableIdleTimeMillis((int) dataSourceInformation.getMinEvictableIdleTimeMillis()); perUserPoolDataSource.setTimeBetweenEvictionRunsMillis( (int) dataSourceInformation.getTimeBetweenEvictionRunsMillis()); perUserPoolDataSource.setNumTestsPerEvictionRun(dataSourceInformation.getNumTestsPerEvictionRun()); if (defaultTransactionIsolation != -1) { perUserPoolDataSource.setDefaultTransactionIsolation(defaultTransactionIsolation); } String validationQuery = dataSourceInformation.getValidationQuery(); if (validationQuery != null && !"".equals(validationQuery)) { perUserPoolDataSource.setValidationQuery(validationQuery); } return perUserPoolDataSource; } else { handleException("Unsupported DataSource : " + dsType); } return null; }
From source file:org.geosde.core.jdbc.data.datasource.DataSourceUtil.java
/** * Builds up a default DBCP DataSource that easy to use connection factories * can use to setup a connection pool./*from ww w .j ava 2 s. co m*/ * * @param url * the jdbc url * @param driverName * the jdbc driver full qualified class name * @param username * @param password * @param maxActive maximum number of concurrent connections in the pool * @param minIdle minimum number of concurrent connections in the pool * @param validationQuery * the validation query to be used for connection liveliness on * borrow, or null, if no check is to be performed * @param cachePreparedStatements * wheter to cache prepared statements or not * @return * @throws DataSourceException */ public static ManageableDataSource buildDefaultDataSource(String url, String driverName, String username, String password, int maxActive, int minIdle, String validationQuery, boolean cachePreparedStatements, int removeAbandonedTimeout) throws DataSourceException { // basics BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setAccessToUnderlyingConnectionAllowed(true); // pool size dataSource.setMaxActive(maxActive); dataSource.setMinIdle(minIdle); // pool eviction settings dataSource.setMinEvictableIdleTimeMillis(1000 * 20); dataSource.setTimeBetweenEvictionRunsMillis(1000 * 10); // connection validation if (validationQuery != null) { dataSource.setTestOnBorrow(true); dataSource.setValidationQuery(validationQuery); } // prepared statement cache if (cachePreparedStatements) { dataSource.setPoolPreparedStatements(true); dataSource.setMaxOpenPreparedStatements(10); } // remove abandoned connections (I know it's deprecated, but we do want // something shaving off lost connections. Let's give them 5 minutes of // continuous usage if (removeAbandonedTimeout > 0) { dataSource.setRemoveAbandoned(true); dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); dataSource.setLogAbandoned(true); } Connection conn = null; try { conn = dataSource.getConnection(); } catch (Exception e) { throw new DataSourceException("Connection test failed ", e); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { } } return new DBCPDataSource(dataSource); }
From source file:org.geosde.core.jdbc.data.datasource.DBCPDataSourceFactory.java
public DataSource createNewDataSource(Map params) throws IOException { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName((String) DRIVERCLASS.lookUp(params)); dataSource.setUrl((String) JDBC_URL.lookUp(params)); dataSource.setUsername((String) USERNAME.lookUp(params)); dataSource.setPassword((String) PASSWORD.lookUp(params)); dataSource.setAccessToUnderlyingConnectionAllowed(true); dataSource.setMaxActive(((Integer) MAXACTIVE.lookUp(params)).intValue()); dataSource.setMaxIdle(((Integer) MAXIDLE.lookUp(params)).intValue()); // check the data source is properly setup by trying to gather a connection out of it Connection conn = null;/*from www . ja v a 2 s .co m*/ try { conn = dataSource.getConnection(); } catch (SQLException e) { throw new DataSourceException("Connection pool improperly set up: " + e.getMessage(), e); } finally { // close the connection at once if (conn != null) try { conn.close(); } catch (SQLException e) { } } return dataSource; }
From source file:org.geosde.core.jdbc.JDBCDataStoreFactory.java
/** * DataSource access allowing SQL use: intended to allow client code to query available schemas. * <p>//w w w . j a va 2 s. c o 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 (on embedded dbs it can be optional) String user = (String) USER.lookUp(params); if (user != null) { 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()); } Boolean testWhileIdle = (Boolean) TEST_WHILE_IDLE.lookUp(params); if (testWhileIdle != null) { dataSource.setTestWhileIdle(testWhileIdle); } Integer timeBetweenEvictorRuns = (Integer) TIME_BETWEEN_EVICTOR_RUNS.lookUp(params); if (timeBetweenEvictorRuns != null && timeBetweenEvictorRuns > 0) { dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictorRuns * 1000l); } Integer minEvictableTime = (Integer) MIN_EVICTABLE_TIME.lookUp(params); if (minEvictableTime != null) { dataSource.setMinEvictableIdleTimeMillis(minEvictableTime * 1000l); } Integer evictorTestsPerRun = (Integer) EVICTOR_TESTS_PER_RUN.lookUp(params); if (evictorTestsPerRun != null) { dataSource.setNumTestsPerEvictionRun(evictorTestsPerRun); } // some datastores might need this dataSource.setAccessToUnderlyingConnectionAllowed(true); return dataSource; }
From source file:org.geotoolkit.coverage.postgresql.PGCoverageStoreFactory.java
/** * Creates the datasource for the coverage store. *//*from w ww .j av a 2 s .c om*/ private DataSource createDataSource(final ParameterValueGroup params) throws IOException { //create a datasource final BasicDataSource dataSource = new BasicDataSource(); // 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()); } // might need this dataSource.setAccessToUnderlyingConnectionAllowed(true); return new DBCPDataSource(dataSource); }
From source file:org.geotoolkit.data.om.SOSDatabaseFeatureStoreFactory.java
@Override public FeatureStore open(final ParameterValueGroup params) throws DataStoreException { checkCanProcessWithError(params);//from ww w . j a v a 2 s . com try { //create a datasource final BasicDataSource dataSource = new BasicDataSource(); // some default data source behaviour dataSource.setPoolPreparedStatements(true); // driver final String driver = getDriverClassName(params); dataSource.setDriverClassName(driver); final boolean isPostgres = driver.startsWith("org.postgresql"); // 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(PASSWD.getName().toString()).getValue(); if (passwd != null) { dataSource.setPassword(passwd); } // some datastores might need this dataSource.setAccessToUnderlyingConnectionAllowed(true); final ManageableDataSource source = new DBCPDataSource(dataSource); return new SOSDatabaseFeatureStore(params, source, isPostgres); } catch (IOException ex) { throw new DataStoreException(ex); } }
From source file:org.geotoolkit.data.sml.SMLFeatureStoreFactory.java
@Override public FeatureStore open(final ParameterValueGroup params) throws DataStoreException { checkCanProcessWithError(params);/* w ww. j a v a2s . com*/ try { //create a datasource final BasicDataSource dataSource = new BasicDataSource(); // some default data source behaviour dataSource.setPoolPreparedStatements(true); // driver dataSource.setDriverClassName(getDriverClassName(params)); // 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(PASSWD.getName().toString()).getValue(); if (passwd != null) { dataSource.setPassword(passwd); } // some datastores might need this dataSource.setAccessToUnderlyingConnectionAllowed(true); final ManageableDataSource source = new DBCPDataSource(dataSource); return new SMLFeatureStore(params, source); } catch (IOException ex) { throw new DataStoreException(ex); } }