List of usage examples for org.apache.commons.dbcp BasicDataSource BasicDataSource
BasicDataSource
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 va2 s.c o m * 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.freewheelschedule.freewheel.config.ServerConfig.java
@Bean public BasicDataSource freewheelDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(hibernateUrl);/*from w w w. j a v a 2s .com*/ dataSource.setUsername(hibernateUser); dataSource.setPassword(hibernatePassword); return dataSource; }
From source file:org.georchestra.mapfishapp.addons.notes.NoteBackend.java
/** * Create a new instance of NoteBackend and create a BasicDataSource configured with jdbc URL. Link to database will * not be tested until store() method is called. * * @param id string to identify this backend, must be unique across backends * @param table name of table to store notes, may contains schema * @param srid numeric identifier of projection linked to coordinates. Example : 4326 for EPSG:4326. * @param jdbcUrl jdbc URL used to connect to database. Example : jdbc:postgresql://localhost:5432/georchestra?user=www-data&password=www-data *///from w w w .ja v a 2 s. c om public NoteBackend(String id, String table, int srid, String jdbcUrl) { this.id = id; this.table = table; this.srid = srid; this.jdbcUrl = jdbcUrl; this.basicDataSource = new BasicDataSource(); this.basicDataSource.setDriverClassName("org.postgresql.Driver"); this.basicDataSource.setTestOnBorrow(true); this.basicDataSource.setPoolPreparedStatements(true); this.basicDataSource.setMaxOpenPreparedStatements(-1); this.basicDataSource.setDefaultReadOnly(false); this.basicDataSource.setDefaultAutoCommit(true); this.basicDataSource.setUrl(jdbcUrl); }
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 om*/ * * @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 w ww. j a va 2 s . c o 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.data.datasource.DBCPDataSourceFactory.java
public boolean isAvailable() { try {/* w w w . jav a 2 s. co m*/ new BasicDataSource(); } catch (Exception e) { return false; } return true; }
From source file:org.geosde.core.jdbc.JDBCDataStoreFactory.java
/** * DataSource access allowing SQL use: intended to allow client code to query available schemas. * <p>/* www .j a v a 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 (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.geoserver.jdbcconfig.internal.JdbcConfigTestSupport.java
public void setUp() throws Exception { ConfigDatabase.LOGGER.setLevel(Level.FINER); // just to avoid hundreds of warnings in the logs about extension lookups with no app // context set WebApplicationContext applicationContext = Mockito.mock(WebApplicationContext.class); new GeoServerExtensions().setApplicationContext(applicationContext); when(applicationContext.getBeansOfType((Class) anyObject())).thenReturn(Collections.EMPTY_MAP); when(applicationContext.getBeanNamesForType((Class) anyObject())).thenReturn(new String[] {}); ////from w ww.j a va2 s. c om final File testDbDir = new File("target", "jdbcconfig"); FileUtils.deleteDirectory(testDbDir); testDbDir.mkdirs(); dataSource = new BasicDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(connectionUrl); dataSource.setUsername("postgres"); dataSource.setPassword("geo123"); dataSource.setMinIdle(3); dataSource.setMaxActive(10); try { Connection connection = dataSource.getConnection(); connection.close(); } catch (Exception e) { throw new RuntimeException(e); } try { dropDb(dataSource); } catch (Exception ignored) { } initDb(dataSource); XStreamInfoSerialBinding binding = new XStreamInfoSerialBinding(new XStreamPersisterFactory()); catalog = new CatalogImpl(); configDb = new ConfigDatabase(dataSource, binding); configDb.setCatalog(catalog); configDb.initDb(null); }
From source file:org.geoserver.jdbcconfig.JDBCConfigTestSupport.java
public void setUp() throws Exception { ConfigDatabase.LOGGER.setLevel(Level.FINER); resourceLoader = new GeoServerResourceLoader(createTempDir()); GeoserverDataDirectory.loader = resourceLoader; // just to avoid hundreds of warnings in the logs about extension lookups with no app // context set appContext = createNiceMock(WebApplicationContext.class); new GeoServerExtensions().setApplicationContext(appContext); configureAppContext(appContext);/* w w w . j a v a 2 s. c om*/ replay(appContext); // final File testDbDir = new File("target", "jdbcconfig"); // FileUtils.deleteDirectory(testDbDir); // testDbDir.mkdirs(); dataSource = new BasicDataSource(); dataSource.setDriverClassName(driver); dataSource .setUrl(connectionUrl.replace("${DATA_DIR}", resourceLoader.getBaseDirectory().getAbsolutePath())); dataSource.setUsername(dbUser); dataSource.setPassword(dbPasswd); dataSource.setMinIdle(3); dataSource.setMaxActive(10); try { Connection connection = dataSource.getConnection(); connection.close(); } catch (Exception e) { throw new RuntimeException(e); } try { dropDb(dataSource); } catch (Exception ignored) { } initDb(dataSource); XStreamInfoSerialBinding binding = new XStreamInfoSerialBinding(new XStreamPersisterFactory()); catalog = new CatalogImpl(); configDb = new ConfigDatabase(dataSource, binding); configDb.setCatalog(catalog); configDb.initDb(null); }
From source file:org.geoserver.security.jdbc.AbstractJDBCService.java
/** * initialize a {@link DataSource} form a * {@link JdbcSecurityServiceConfig} object * /*from w ww . j av a2 s. co m*/ * @param config * @throws IOException */ public void initializeDSFromConfig(SecurityNamedServiceConfig namedConfig) throws IOException { JDBCSecurityServiceConfig config = (JDBCSecurityServiceConfig) namedConfig; if (config.isJndi()) { String jndiName = config.getJndiName(); try { Context initialContext = new InitialContext(); datasource = (DataSource) initialContext.lookup(jndiName); } catch (NamingException e) { throw new IOException(e); } } else { BasicDataSource bds = new BasicDataSource(); bds.setDriverClassName(config.getDriverClassName()); bds.setUrl(config.getConnectURL()); bds.setUsername(config.getUserName()); bds.setPassword(config.getPassword()); bds.setDefaultAutoCommit(false); bds.setDefaultTransactionIsolation(DEFAULT_ISOLATION_LEVEL); bds.setMaxActive(10); datasource = bds; } }