List of usage examples for org.apache.commons.dbcp BasicDataSource setTestOnBorrow
public synchronized void setTestOnBorrow(boolean testOnBorrow)
From source file:org.apache.synapse.config.xml.AbstractDBMediatorFactory.java
/** * Create a custom DataSource using the specified properties and Apache DBCP * @param pool the toplevel 'pool' element that holds DataSource information * @param mediator the mediator to store properties for serialization * @return a DataSource created using specified properties *///w w w . j a v a2s.co m private DataSource createCustomDataSource(OMElement pool, AbstractDBMediator mediator) { BasicDataSource ds = new BasicDataSource(); // load the minimum required properties ds.setDriverClassName(getValue(pool, DRIVER_Q)); ds.setUsername(getValue(pool, USER_Q)); ds.setPassword(getValue(pool, PASS_Q)); ds.setUrl(getValue(pool, URL_Q)); //save loaded properties for later mediator.addDataSourceProperty(DRIVER_Q, getValue(pool, DRIVER_Q)); mediator.addDataSourceProperty(URL_Q, getValue(pool, URL_Q)); mediator.addDataSourceProperty(USER_Q, getValue(pool, USER_Q)); mediator.addDataSourceProperty(PASS_Q, getValue(pool, PASS_Q)); Iterator props = pool.getChildrenWithName(PROP_Q); while (props.hasNext()) { OMElement prop = (OMElement) props.next(); String name = prop.getAttribute(ATT_NAME).getAttributeValue(); String value = prop.getAttribute(ATT_VALUE).getAttributeValue(); // save property for later mediator.addDataSourceProperty(name, value); if ("autocommit".equals(name)) { if ("true".equals(value)) { ds.setDefaultAutoCommit(true); } else if ("false".equals(value)) { ds.setDefaultAutoCommit(false); } } else if ("isolation".equals(name)) { try { if ("Connection.TRANSACTION_NONE".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE); } else if ("Connection.TRANSACTION_READ_COMMITTED".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } else if ("Connection.TRANSACTION_READ_UNCOMMITTED".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); } else if ("Connection.TRANSACTION_REPEATABLE_READ".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); } else if ("Connection.TRANSACTION_SERIALIZABLE".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); } } catch (NumberFormatException ignore) { } } else if ("initialsize".equals(name)) { try { ds.setInitialSize(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxactive".equals(name)) { try { ds.setMaxActive(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxidle".equals(name)) { try { ds.setMaxIdle(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxopenstatements".equals(name)) { try { ds.setMaxOpenPreparedStatements(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxwait".equals(name)) { try { ds.setMaxWait(Long.parseLong(value)); } catch (NumberFormatException ignore) { } } else if ("minidle".equals(name)) { try { ds.setMinIdle(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("poolstatements".equals(name)) { if ("true".equals(value)) { ds.setPoolPreparedStatements(true); } else if ("false".equals(value)) { ds.setPoolPreparedStatements(false); } } else if ("testonborrow".equals(name)) { if ("true".equals(value)) { ds.setTestOnBorrow(true); } else if ("false".equals(value)) { ds.setTestOnBorrow(false); } } else if ("testonreturn".equals(name)) { if ("true".equals(value)) { ds.setTestOnReturn(true); } else if ("false".equals(value)) { ds.setTestOnReturn(false); } } else if ("testwhileidle".equals(name)) { if ("true".equals(value)) { ds.setTestWhileIdle(true); } else if ("false".equals(value)) { ds.setTestWhileIdle(false); } } else if ("validationquery".equals(name)) { ds.setValidationQuery(value); } } return 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());// www. ja v a 2 s . c om 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.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.//ww w . j a va2 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.JDBCDataStoreFactory.java
/** * DataSource access allowing SQL use: intended to allow client code to query available schemas. * <p>//from w w w. j ava2 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 (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 ww w .jav a 2 s .co m*/ 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.db.AbstractJDBCFeatureStoreFactory.java
/** * Create a datasource using given parameters. *//*w w w. j a v a 2s.c o m*/ 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.jdbc.JDBCDataStoreFactory.java
/** * DataSource access allowing SQL use: intended to allow client code to query available schemas. * <p>//from www .ja va 2s .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 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.jxstar.dao.pool.PooledConnection.java
/** * ???// www. j a va2s.co m * @param dsName * @return */ private DataSource createSelfDataSource(DataSourceConfig dsConfig) { String dsName = dsConfig.getDataSourceName(); BasicDataSource ds = (BasicDataSource) _myDataSourceMap.get(dsName); if (ds != null) return ds; ds = new BasicDataSource(); //??? int iTranLevel = getTranLevelConstant(dsConfig.getTranLevel()); int maxnum = Integer.parseInt(dsConfig.getMaxConNum()); ds.setDriverClassName(dsConfig.getDriverClass()); ds.setUrl(dsConfig.getJdbcUrl()); ds.setUsername(dsConfig.getUserName()); ds.setPassword(dsConfig.getPassWord()); ds.setMaxIdle(maxnum); ds.setMaxActive(maxnum); ds.setMaxWait(Long.parseLong(dsConfig.getMaxWaitTime())); ds.setDefaultAutoCommit(false); ds.setDefaultTransactionIsolation(iTranLevel); //????SystemVarserver.xml? String validTest = dsConfig.getValidTest(); String validQuery = dsConfig.getValidQuery(); if (validTest.equalsIgnoreCase("true") && validQuery.length() > 0) { _log.showDebug("pool test use query..."); ds.setTestOnBorrow(true); ds.setValidationQuery(validQuery); ds.setValidationQueryTimeout(3); } //?mysql??? //????? if (dsConfig.getValidIdle().equalsIgnoreCase("true")) { _log.showDebug("pool idle valid thread started..."); ds.setMinIdle(5); ds.setTestWhileIdle(true); //1030?5 ds.setMinEvictableIdleTimeMillis(30 * 60 * 1000);//30 minus ds.setTimeBetweenEvictionRunsMillis(10 * 60 * 1000);//10 minus } //??? _myDataSourceMap.put(dsName, ds); return ds; }
From source file:org.nebula.service.core.DynamicDataSource.java
public void onChange(String dbUrl) { logger.info("Change target dbUrl to: " + dbUrl); Object oldDataSource = this.datasources.remove(LOOKUP_KEY); if (oldDataSource != null) { try {//from w w w. ja v a 2s.c o m ((BasicDataSource) oldDataSource).close(); } catch (SQLException e) { //ignore } } BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setMaxActive(jdbcMaxActive); dataSource.setMaxIdle(jdbcMaxIdle); dataSource.setInitialSize(jdbcInitialSize); dataSource.setUrl(dbUrl); dataSource.setUsername(jdbcUsername); dataSource.setPassword(jdbcPassword); dataSource.setTestOnBorrow(true); dataSource.setValidationQuery("SELECT 1"); this.datasources.put(LOOKUP_KEY, dataSource); }
From source file:org.ngrinder.infra.config.Database.java
/** * Setup the database common features./* w w w .j av a2 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"); }