List of usage examples for org.apache.commons.dbcp BasicDataSource setPoolPreparedStatements
public synchronized void setPoolPreparedStatements(boolean poolingStatements)
Sets whether to pool statements or not.
Note: this method currently has no effect once the pool has been initialized.
From source file:org.ff4j.test.utils.JdbcTestHelper.java
/** * Initialize DataSource with a pool of connections to HQL database. * * @return//from ww w .j a va 2 s . com * current data source */ public static DataSource createInMemoryHQLDataSource() { // Init DataSource BasicDataSource dbcpDataSource = new BasicDataSource(); dbcpDataSource.setDriverClassName("org.hsqldb.jdbcDriver"); dbcpDataSource.setUsername("sa"); dbcpDataSource.setPassword(""); dbcpDataSource.setUrl("jdbc:hsqldb:mem:."); dbcpDataSource.setMaxActive(3); dbcpDataSource.setMaxIdle(2); dbcpDataSource.setInitialSize(2); dbcpDataSource.setValidationQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS;"); dbcpDataSource.setPoolPreparedStatements(true); return dbcpDataSource; }
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 w w w. j ava 2 s. c o 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
/** * Creates the datasource for the data store. * <p>// w w w . j ava 2 s . co m * This method creates a {@link BasicDataSource} instance and populates it * as follows: * <ul> * <li>poolPreparedStatements -> false * <li>driverClassName -> {@link #getDriverClassName()} * <li>url -> 'jdbc:<{@link #getDatabaseID()}>://<{@link #HOST}>/<{@link #DATABASE}>' * <li>username -> <{@link #USER}> * <li>password -> <{@link #PASSWD}> * </ul> * If different behaviour is needed, this method should be extended or * overridden. * </p> */ protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException { BasicDataSource dataSource = createDataSource(params); // some default data source behaviour if (dialect instanceof PreparedStatementSQLDialect) { dataSource.setPoolPreparedStatements(true); // check if the dialect exposes the max prepared statements param Map<String, Serializable> testMap = new HashMap<String, Serializable>(); setupParameters(testMap); if (testMap.containsKey(MAX_OPEN_PREPARED_STATEMENTS.key)) { Integer maxPreparedStatements = (Integer) MAX_OPEN_PREPARED_STATEMENTS.lookUp(params); // limit prepared statements if (maxPreparedStatements != null && maxPreparedStatements > 0) dataSource.setMaxOpenPreparedStatements(maxPreparedStatements); // disable statement caching fully if necessary if (maxPreparedStatements != null && maxPreparedStatements < 0) dataSource.setPoolPreparedStatements(false); } } return new DBCPDataSource(dataSource); }
From source file:org.geotoolkit.data.om.SOSDatabaseFeatureStoreFactory.java
@Override public FeatureStore open(final ParameterValueGroup params) throws DataStoreException { checkCanProcessWithError(params);// w w w . j ava2 s .c om 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);//from ww w . ja v a2 s . c o m 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); } }
From source file:org.geotoolkit.db.AbstractJDBCFeatureStoreFactory.java
/** * Create a datasource using given parameters. *//*ww w . j a v a2 s.c om*/ 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.h2.H2DataStoreFactory.java
protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException { String database = (String) DATABASE.lookUp(params); String host = (String) HOST.lookUp(params); BasicDataSource dataSource = new BasicDataSource(); if (host != null && !host.equals("")) { Integer port = (Integer) PORT.lookUp(params); if (port != null && !port.equals("")) { dataSource.setUrl("jdbc:h2:tcp://" + host + ":" + port + "/" + database); } else {//www . j a va 2s . c o m dataSource.setUrl("jdbc:h2:tcp://" + host + "/" + database); } } else if (baseDirectory == null) { //use current working directory dataSource.setUrl("jdbc:h2:" + database); } else { //use directory specified if the patch is relative String location; if (!new File(database).isAbsolute()) { location = new File(baseDirectory, database).getAbsolutePath(); } else { location = database; } dataSource.setUrl("jdbc:h2:file:" + location); } String username = (String) USER.lookUp(params); if (username != null) { dataSource.setUsername(username); } String password = (String) PASSWD.lookUp(params); if (password != null) { dataSource.setPassword(password); } dataSource.setDriverClassName("org.h2.Driver"); dataSource.setPoolPreparedStatements(false); return new DBCPDataSource(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. *//* ww w . j ava2s . c o 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
private DataSource getDataSource(JDBCConfiguration config) throws ConfigurationException { try {// www .ja v a2s . c om DataSource ds = null; if (config.getJNDISource() != null) { InitialContext context = new InitialContext(); ds = (DataSource) context.lookup(config.getJNDISource()); } else if (config.getConnectionPool() != null) { ConnectionPoolConfiguration cp = config.getConnectionPool(); BasicDataSource bds = new BasicDataSource(); bds.setDriverClassName(cp.getDriver()); bds.setUrl(cp.getUrl()); bds.setUsername(cp.getUsername()); bds.setPassword(cp.getPassword()); bds.setPoolPreparedStatements(true); bds.setMaxOpenPreparedStatements(cp.getMaxOpenPreparedStatements()); bds.setMinIdle(cp.getMinConnections()); bds.setMaxActive(cp.getMaxConnections()); bds.setMaxWait(cp.getConnectionTimeout() * 1000); bds.setValidationQuery(cp.getValidationQuery()); ds = bds; } // verify the datasource works Connection c = null; try { c = ds.getConnection(); } catch (SQLException e) { throw new ConfigurationException("Failed to get a database connection: " + e.getMessage(), e); } finally { if (c != null) { try { c.close(); } catch (SQLException e) { // nothing we can do about it, but at least let the admin know log.debug("An error occurred while closing the test JDBC connection: " + e.getMessage(), e); } } } return ds; } catch (NamingException e) { throw new ConfigurationException("Failed to locate the data source in JNDI", e); } }
From source file:org.geowebcache.diskquota.jdbc.JDBCQuotaStoreFactory.java
/** * Prepares a simple data source for the embedded H2 * /* ww w . j a v a 2s . co 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; }