List of usage examples for org.apache.commons.dbcp PoolingDataSource PoolingDataSource
public PoolingDataSource(ObjectPool pool)
From source file:com.jt.dbcp.example.ManualPoolingDataSourceExample.java
public static DataSource setupDataSource(String connectURI) { ///* w w w. j a v a 2 s.co m*/ // First, we'll need a ObjectPool that serves as the // actual pool of connections. // // We'll use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. // ObjectPool connectionPool = new GenericObjectPool(null); // // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, "root", "root"); // // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); // // Finally, we create the PoolingDriver itself, // passing in the object pool we created. // PoolingDataSource dataSource = new PoolingDataSource(connectionPool); return dataSource; }
From source file:com.toolsverse.etl.sql.connection.PooledAliasConnectionProvider.java
@Override protected Connection createConnection(Alias alias) throws Exception { java.sql.Driver driver = (java.sql.Driver) Class.forName(alias.getJdbcDriverClass()).newInstance(); DriverManager.registerDriver(driver); org.apache.commons.dbcp.ConnectionFactory connectionFactory = null; Properties props = Utils.getProperties(alias.getParams()); String userId = alias.getUserId(); String password = alias.getPassword(); String url = alias.getUrl();/*from w w w . ja va 2s. c o m*/ if (props.size() > 0) { if (!Utils.isNothing(userId)) { props.put("user", userId); if (!Utils.isNothing(password)) props.put("password", password); } connectionFactory = new DriverManagerConnectionFactory(url, props); } else connectionFactory = new DriverManagerConnectionFactory(url, userId, password); ObjectPool connectionPool = new GenericObjectPool(null, _config); @SuppressWarnings("unused") PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool); return poolingDataSource.getConnection(); }
From source file:com.iciql.test.IciqlSuite.java
/** * Open a new Db object. All connections are cached and re-used to eliminate * embedded database startup costs./*from w ww .j a va 2 s . c om*/ * * @return a fresh Db object */ public static Db openNewDb() { String testUrl = System.getProperty("iciql.url", DEFAULT_TEST_DB.url); String testUser = System.getProperty("iciql.user", DEFAULT_TEST_DB.username); String testPassword = System.getProperty("iciql.password", DEFAULT_TEST_DB.password); Db db = null; PoolingDataSource dataSource = dataSources.get(testUrl); if (dataSource == null) { ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(testUrl, testUser, testPassword); GenericObjectPool pool = new GenericObjectPool(); pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); PoolableConnectionFactory factory = new PoolableConnectionFactory(connectionFactory, pool, null, null, false, true); dataSource = new PoolingDataSource(pool); dataSources.put(testUrl, dataSource); connectionFactories.put(testUrl, factory); } db = Db.open(dataSource); // drop views db.dropView(ProductView.class); db.dropView(ProductViewInherited.class); db.dropView(ProductViewFromQuery.class); db.dropView(ProductViewInheritedComplex.class); // drop tables db.dropTable(BooleanModel.class); db.dropTable(ComplexObject.class); db.dropTable(Customer.class); db.dropTable(DefaultValuesModel.class); db.dropTable(EnumIdModel.class); db.dropTable(EnumOrdinalModel.class); db.dropTable(EnumStringModel.class); db.dropTable(Order.class); db.dropTable(PrimitivesModel.class); db.dropTable(Product.class); db.dropTable(ProductAnnotationOnly.class); db.dropTable(ProductInheritedAnnotation.class); db.dropTable(ProductMixedAnnotation.class); db.dropTable(SupportedTypes.class); db.dropTable(JoinTest.UserId.class); db.dropTable(JoinTest.UserNote.class); db.dropTable(EnumsTest.BadEnums.class); db.dropTable(MultipleBoolsModel.class); db.dropTable(ProductAnnotationOnlyWithForeignKey.class); db.dropTable(CategoryAnnotationOnly.class); return db; }
From source file:com.insprise.common.db.DefaultConnectionPool.java
/** * Sets up the connection pool. Call this at most once only! * * @throws ClassNotFoundException/* w ww . j ava 2 s . co m*/ */ private void setupPool() throws ClassNotFoundException { if (sourceType != SourceType.DRIVER_MANAGER) { throw new RuntimeException(); } connectionPool = new GenericObjectPool(null); setMaxActive(maxActive); setMaxIdle(maxIdle); setMaxWaittime(maxWaittime); // establish connection factory. ConnectionFactory connectionFactory = null; Class.forName(driverClass); connectionFactory = new DriverManagerConnectionFactory(url, properties); if (validationQuery == null) { log.warn("Validation SQL for database connection pool is null!"); } else { // log.info("Validation SQL for database connection pool is: " + validationQuery); } PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false, true); // force to execute validationQuery before borrowing connection from pool. connectionPool.setTestOnBorrow(true); dataSource = new PoolingDataSource(connectionPool); log.debug("Initialized " + toString()); }
From source file:net.agmodel.metbroker.driver.impl.JmaMsmJp.java
/** * set up JDBC parameter./*from ww w .j av a 2s .co m*/ * This driver uses postgresql's JDBC driver. * (non-Javadoc) * @see net.agmodel.metbroker.driver.MetDriver#init(net.agmodel.metbroker.driver.DriverConfig) */ public void init(DriverConfig driverConfig) throws DriverException { logger.debug("initialize JMA MSM JP data source"); try { Class.forName("org.postgresql.Driver"); int maxActive = Integer.parseInt(driverConfig.getParam("maxactive")); int maxWait = Integer.parseInt(driverConfig.getParam("maxwait")); int maxIdle = Integer.parseInt(driverConfig.getParam("maxidle")); String user = driverConfig.getParam("user"); String password = driverConfig.getParam("password"); String host = driverConfig.getParam("host"); int port = Integer.parseInt(driverConfig.getParam("port")); String db = driverConfig.getParam("db"); ObjectPool connectionPool = new GenericObjectPool(null, maxActive, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, false, false, 60000, 3, 10000, false); Properties p = new Properties(); p.put("user", user); p.put("password", password); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( "jdbc:postgresql://" + host + ":" + port + "/" + db, p); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); dataSource = new PoolingDataSource(connectionPool); logger.debug("JMA MSM JP DataSource created"); } catch (Exception e) { e.printStackTrace(); throw new DriverException(e.getMessage()); } }
From source file:net.agmodel.metbroker.driver.impl.JmaGsmJp.java
/** * set up JDBC parameter.//from ww w .j a v a 2 s . com * This driver uses postgresql's JDBC driver. * * (non-Javadoc) * @see net.agmodel.metbroker.driver.MetDriver#init(net.agmodel.metbroker.driver.DriverConfig) */ public void init(DriverConfig driverConfig) throws DriverException { logger.debug("initialize JMA GSM JP data source"); try { Class.forName("org.postgresql.Driver"); int maxActive = Integer.parseInt(driverConfig.getParam("maxactive")); int maxWait = Integer.parseInt(driverConfig.getParam("maxwait")); int maxIdle = Integer.parseInt(driverConfig.getParam("maxidle")); String user = driverConfig.getParam("user"); String password = driverConfig.getParam("password"); String host = driverConfig.getParam("host"); int port = Integer.parseInt(driverConfig.getParam("port")); String db = driverConfig.getParam("db"); ObjectPool connectionPool = new GenericObjectPool(null, maxActive, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, false, false, 60000, 3, 10000, false); Properties p = new Properties(); p.put("user", user); p.put("password", password); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( "jdbc:postgresql://" + host + ":" + port + "/" + db, p); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); dataSource = new PoolingDataSource(connectionPool); logger.debug("JMA GSM JP DataSource created"); } catch (Exception e) { e.printStackTrace(); throw new DriverException(e.getMessage()); } }
From source file:net.agmodel.metbroker.driver.impl.JmaGsmGl.java
/** * <PRE>/*from ww w .j a va 2 s . c o m*/ * set up JDBC parameter. * This driver uses postgresql's JDBC driver. * </PRE> * (non-Javadoc) * @see net.agmodel.metbroker.driver.MetDriver#init(net.agmodel.metbroker.driver.DriverConfig) */ public void init(DriverConfig driverConfig) throws DriverException { logger.debug("initialize JMA GSM GLobal data source"); try { Class.forName("org.postgresql.Driver"); int maxActive = Integer.parseInt(driverConfig.getParam("maxactive")); int maxWait = Integer.parseInt(driverConfig.getParam("maxwait")); int maxIdle = Integer.parseInt(driverConfig.getParam("maxidle")); String user = driverConfig.getParam("user"); String password = driverConfig.getParam("password"); String host = driverConfig.getParam("host"); int port = Integer.parseInt(driverConfig.getParam("port")); String db = driverConfig.getParam("db"); ObjectPool connectionPool = new GenericObjectPool(null, maxActive, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, false, false, 60000, 3, 10000, false); Properties p = new Properties(); p.put("user", user); p.put("password", password); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( "jdbc:postgresql://" + host + ":" + port + "/" + db, p); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); dataSource = new PoolingDataSource(connectionPool); logger.debug("JMA GSM Global DataSource created"); } catch (Exception e) { e.printStackTrace(); throw new DriverException(e.getMessage()); } }
From source file:dev.utils.db.dbcp.ManualPoolingDataSource.java
public DataSource setupDataSource(String connectURI) { ///*from w w w . j a va 2s. c om*/ // First, we'll need a ObjectPool that serves as the // actual pool of connections. // // We'll use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. // GenericObjectPool.Config config = new GenericObjectPool.Config(); config.maxActive = maxActive; // ex: 150; config.maxIdle = maxIdle; // ex: 100; config.minIdle = minIdle; //30; config.maxWait = maxWait; //1000; config.testOnBorrow = testOnBorrow; config.testOnReturn = testOnReturn; config.testWhileIdle = testWhileIdle; config.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; config.numTestsPerEvictionRun = numTestsPerEvictionRun; config.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; ObjectPool connectionPool = new GenericObjectPool(null, config); // // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // Properties connProps = new Properties(); connProps.setProperty("user", username); connProps.setProperty("password", password); //p.setProperty("useUnicode", "true"); //p.setProperty("characterEncoding", "UTF-8"); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, connProps); // // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, defaultReadOnly, defaultAutoCommit); // // Finally, we create the PoolingDriver itself, // passing in the object pool we created. // return new PoolingDataSource(connectionPool); }
From source file:net.ontopia.persistence.proxy.DBCPConnectionFactory.java
protected void initPool() { // Set up connection pool pool = new GenericObjectPool(null); // Read/Write by default boolean readonly = defaultReadOnly; // Auto-commit disabled by default boolean autocommit = readonly; log.debug("Creating new DBCP connection factory, readonly=" + readonly + ", autocommit=" + autocommit); // Set minimum pool size (default: 20) String _minsize = PropertyUtils.getProperty(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.MinimumSize", false); int minsize = (_minsize == null ? 20 : Integer.parseInt(_minsize)); log.debug("Setting ConnectionPool.MinimumSize '" + minsize + "'"); pool.setMaxIdle(minsize); // 0 = no limit // Set maximum pool size (default: Integer.MAX_VALUE) String _maxsize = PropertyUtils.getProperty(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.MaximumSize", false); int maxsize = (_maxsize == null ? 0 : Integer.parseInt(_maxsize)); log.debug("Setting ConnectionPool.MaximumSize '" + maxsize + "'"); pool.setMaxActive(maxsize); // 0 = no limit // Set user timeout (default: never) String _utimeout = PropertyUtils.getProperty(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.UserTimeout", false); int utimeout = (_utimeout == null ? -1 : Integer.parseInt(_utimeout)); pool.setMaxWait(utimeout); // -1 = never // Set soft maximum - emergency objects (default: true) boolean softmax = PropertyUtils.isTrue(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.SoftMaximum", true); log.debug("Setting ConnectionPool.SoftMaximum '" + softmax + "'"); if (softmax)/*ww w . j a v a2 s . c o m*/ pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); else pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); // allow the user to overwrite exhausted options // warning: when set to fail, make sure Maximum and Minimum are set correctly // warning: when set to block, make sure a propper usertimeout is set, or pool will block // forever String _whenExhaustedAction = PropertyUtils.getProperty(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.WhenExhaustedAction", false); if (EXHAUSED_BLOCK.equals(_whenExhaustedAction)) pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK); if (EXHAUSED_GROW.equals(_whenExhaustedAction)) pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW); if (EXHAUSED_FAIL.equals(_whenExhaustedAction)) pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL); if (pool.getWhenExhaustedAction() == GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK) log.debug("Pool is set to block on exhaused"); if (pool.getWhenExhaustedAction() == GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW) log.debug("Pool is set to grow on exhaused"); if (pool.getWhenExhaustedAction() == GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL) log.debug("Pool is set to fail on exhaused"); // Statement pool GenericKeyedObjectPoolFactory stmpool = null; if (PropertyUtils.isTrue(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.PoolStatements", true)) { log.debug("Using prepared statement pool: Yes"); stmpool = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive (per key) GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait 1, // maxIdle (per key) GenericKeyedObjectPool.DEFAULT_MAX_TOTAL); } else { log.debug("Using prepared statement pool: No"); } // Test on borrow pool.setTestOnBorrow(true); // Get validation query String vquery = PropertyUtils.getProperty(properties, "net.ontopia.topicmaps.impl.rdbms.ConnectionPool.ValidationQuery", false); if (vquery == null) vquery = "select seq_count from TM_ADMIN_SEQUENCE where seq_name = '<GLOBAL>'"; try { // Make sure driver is registered ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Class.forName(getDriver(), true, classLoader); // Create connection factory ConnectionFactory cfactory; if (getUserName() == null || getPassword() == null) { Properties props = new Properties(); props.putAll(properties); cfactory = new DriverManagerConnectionFactory(getConnectionString(), props); } else { cfactory = new DriverManagerConnectionFactory(getConnectionString(), getUserName(), getPassword()); } // Create data source this.pcfactory = new TraceablePoolableConnectionFactory(cfactory, pool, stmpool, vquery, readonly, autocommit); // Set default transaction isolation level pcfactory.setDefaultTransactionIsolation(defaultTransactionIsolation); this.datasource = new PoolingDataSource(pool); } catch (Exception e) { throw new OntopiaRuntimeException("Problems occurred when setting up DBCP connection pool.", e); } }
From source file:esg.common.db.DatabaseResource.java
public DatabaseResource setupDataSource(Properties props) { log.trace("Setting up data source... "); if (props == null) { log.error("Property object is [" + props + "]: Cannot setup up data source"); return this; }/* w w w .j ava 2s . c o m*/ //Ex: jdbc:postgresql://pcmdi3.llnl.gov:5432/esgcet String protocol = props.getProperty("db.protocol", "jdbc:postgresql:"); String host = props.getProperty("db.host", "localhost"); String port = props.getProperty("db.port", "5432"); String database = props.getProperty("db.database", "esgcet"); String user = props.getProperty("db.user", "dbsuper"); String password = props.getProperty("db.password"); //If the password is not directly available in the properties //object then try to read it via the code provided in the //ESGFProperties type...iff props is actually of the type //ESGFProperties. if (password == null) { try { password = ((esg.common.util.ESGFProperties) props).getDatabasePassword(); } catch (Throwable t) { log.error(" ****** DATABASE PASSWORD IS NOT SET!!! ******"); log.warn( "Check that password is set in the passed in property file or that property object is an ESGFProperties object!!"); t.printStackTrace(); } } String connectURI = protocol + "//" + host + ":" + port + "/" + database; log.info("Connection URI = " + connectURI); connectionPool = new GenericObjectPool(null); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, user, password); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); dataSource = new PoolingDataSource(connectionPool); return this; }