List of usage examples for org.apache.commons.dbcp BasicDataSource setMaxIdle
public synchronized void setMaxIdle(int maxIdle)
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 *//*from w w w . j a v a 2 s . c om*/ 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.apache.taverna.provenance.api.ProvenanceAccess.java
/** * Initialises a named JNDI DataSource if not already set up externally. The * DataSource is named jdbc/taverna//ww w.j av a 2 s . c o m * * @param driverClassName * - the classname for the driver to be used. * @param jdbcUrl * - the jdbc connection url * @param username * - the username, if required (otherwise null) * @param password * - the password, if required (oteherwise null) * @param minIdle * - if the driver supports multiple connections, then the * minumum number of idle connections in the pool * @param maxIdle * - if the driver supports multiple connections, then the * maximum number of idle connections in the pool * @param maxActive * - if the driver supports multiple connections, then the * minumum number of connections in the pool */ public static void initDataSource(String driverClassName, String jdbcUrl, String username, String password, int minIdle, int maxIdle, int maxActive) { System.setProperty(INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory"); System.setProperty("org.osjava.sj.jndi.shared", "true"); BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driverClassName); ds.setDefaultTransactionIsolation(TRANSACTION_READ_UNCOMMITTED); ds.setMaxActive(maxActive); ds.setMinIdle(minIdle); ds.setMaxIdle(maxIdle); ds.setDefaultAutoCommit(true); if (username != null) ds.setUsername(username); if (password != null) ds.setPassword(password); ds.setUrl(jdbcUrl); try { new InitialContext().rebind("jdbc/taverna", ds); } catch (NamingException ex) { logger.error("Problem rebinding the jdbc context", ex); } }
From source file:org.apache.wookie.server.Start.java
private static void configureServer() throws Exception { // create embedded jetty instance logger.info("Configuring Jetty server"); server = new Server(port); // configure embedded jetty to handle wookie web application WebAppContext context = new WebAppContext(); context.setServer(server);/*from w ww . j a v a 2 s . c o m*/ context.setContextPath("/wookie"); context.setWar("build/webapp/wookie"); // enable and configure JNDI container resources context.setConfigurationClasses(new String[] { "org.mortbay.jetty.webapp.WebInfConfiguration", "org.mortbay.jetty.plus.webapp.EnvConfiguration", "org.mortbay.jetty.plus.webapp.Configuration", "org.mortbay.jetty.webapp.JettyWebXmlConfiguration", "org.mortbay.jetty.webapp.TagLibConfiguration" }); if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JPA)) { logger.info("Configuring JPA persistence manager"); // setup derby database directory and logging properties if (dbType.equals("derby") && dbUri.startsWith("jdbc:derby:")) { int dbUriArgsIndex = dbUri.indexOf(";", 11); if (dbUriArgsIndex == -1) { dbUriArgsIndex = dbUri.length(); } String databasePath = dbUri.substring(11, dbUriArgsIndex); int databaseDirIndex = databasePath.lastIndexOf(File.separatorChar); if ((databaseDirIndex == -1) && (File.separatorChar != '/')) { databaseDirIndex = databasePath.lastIndexOf('/'); } if (databaseDirIndex != -1) { String databaseDir = databasePath.substring(0, databaseDirIndex); File databaseDirFile = new File(databaseDir); if (!databaseDirFile.exists()) { databaseDirFile.mkdirs(); } String derbyLog = databaseDirFile.getAbsolutePath() + File.separator + "derby.log"; System.setProperty("derby.stream.error.file", derbyLog); } } // Setup a database connection resource using DBCP BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(dbDriver); dataSource.setUrl(dbUri); dataSource.setUsername(dbUser); dataSource.setPassword(dbPassword); dataSource.setMaxActive(80); dataSource.setMaxIdle(80); dataSource.setInitialSize(5); dataSource.setMaxOpenPreparedStatements(0); // Set up connection pool GenericObjectPool pool = new GenericObjectPool(); // setup factory and pooling DataSource DataSourceConnectionFactory factory = new DataSourceConnectionFactory(dataSource); @SuppressWarnings("unused") PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(factory, pool, null, null, false, true); DataSource poolingDataSource = new PoolingDataSource(pool); new Resource(JPAPersistenceManager.WIDGET_DATABASE_JNDI_DATASOURCE_NAME, poolingDataSource); } else if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JCR)) { logger.info("Configuring JCR persistence manager"); // setup repository directory and derby logging properties File repositoryDirFile = new File("widgetRepository"); if (!repositoryDirFile.exists()) { repositoryDirFile.mkdirs(); } String derbyLog = repositoryDirFile.getAbsolutePath() + File.separator + "derby.log"; System.setProperty("derby.stream.error.file", derbyLog); // setup Jackrabbit JCR repository JNDI resource String repositoryConfig = repositoryDirFile.getAbsolutePath() + File.separator + "repository.xml"; Repository repository = new TransientRepository(repositoryConfig, repositoryDirFile.getAbsolutePath()); new Resource(JCRPersistenceManager.WIDGET_REPOSITORY_JNDI_REPOSITORY_NAME, repository); } // configure embedded jetty web application handler server.addHandler(context); // configure embedded jetty authentication realm HashUserRealm authedRealm = new HashUserRealm("Authentication Required", "etc/jetty-realm.properties"); server.setUserRealms(new UserRealm[] { authedRealm }); logger.info("Configured Jetty server"); }
From source file:org.cambillaum.jpapersistor.persistence.configuration.PersistenceConfiguration.java
@Bean public BasicDataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); dataSource.setUrl("jdbc:hsqldb:mem:testdb"); dataSource.setUsername("sa"); dataSource.setPassword(""); dataSource.setInitialSize(0);/*from www.jav a2s.c o m*/ dataSource.setMaxActive(15); dataSource.setMaxIdle(0); dataSource.setMinEvictableIdleTimeMillis(60000); return dataSource; }
From source file:org.compass.core.lucene.engine.store.jdbc.DbcpDataSourceProvider.java
protected DataSource doCreateDataSource(String url, CompassSettings settings) throws CompassException { BasicDataSource dataSource = new BasicDataSource(); if (!externalAutoCommit) { dataSource.setDefaultAutoCommit(autoCommit); }/*w ww .j av a 2s .c o m*/ dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); if (settings.getSetting( LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.DEFAULT_TRANSACTION_ISOLATION) != null) { dataSource.setDefaultTransactionIsolation(settings.getSettingAsInt( LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.DEFAULT_TRANSACTION_ISOLATION, 0)); } if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.INITIAL_SIZE) != null) { dataSource.setInitialSize( settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.INITIAL_SIZE, 0)); } if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_ACTIVE) != null) { dataSource.setMaxActive( settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_ACTIVE, 0)); } if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_IDLE) != null) { dataSource.setMaxIdle( settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_IDLE, 0)); } if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MIN_IDLE) != null) { dataSource.setMinIdle( settings.getSettingAsInt(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MIN_IDLE, 0)); } if (settings.getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_WAIT) != null) { dataSource.setMaxWait( settings.getSettingAsLong(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_WAIT, 0)); } if (settings.getSetting( LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_OPEN_PREPARED_STATEMENTS) != null) { dataSource.setMaxOpenPreparedStatements(settings.getSettingAsInt( LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_OPEN_PREPARED_STATEMENTS, 0)); } if (settings .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.POOL_PREPARED_STATEMENTS) != null) { dataSource.setPoolPreparedStatements(settings.getSettingAsBoolean( LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.POOL_PREPARED_STATEMENTS, false)); } return dataSource; }
From source file:org.danann.cernunnos.sql.BasicDataSourceTemplate.java
public final void perform(TaskRequest req, TaskResponse res) { //Get the JDBC properties final String driverClassName = (String) this.driverPhrase.evaluate(req, res); final String url = (String) this.urlPhrase.evaluate(req, res); final String username = (String) this.usernamePhrase.evaluate(req, res); final String password = (String) this.passwordPhrase.evaluate(req, res); final String dataSourceInfo = "driverClassName='" + driverClassName + "', url='" + url + "', username='" + username + "'"; this.logger.debug("Creating DataSource for " + dataSourceInfo + "."); final BasicDataSource dataSource = new BasicDataSource(); try {/* w w w . j a v a 2 s . c o m*/ //Configure the pooling DataSource dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setPoolPreparedStatements(true); dataSource.setMaxActive(-1); dataSource.setMaxIdle(32); //Provide the DataSource on the response environment final String dataSourceAttrName = (String) this.attributeNamePhrase.evaluate(req, res); res.setAttribute(dataSourceAttrName, dataSource); this.logger.debug("Attached DataSource '" + dataSource + "' for " + dataSourceInfo + " to response under attribute '" + dataSourceAttrName + "'."); //Execute subtasks this.performWithDataSource(req, res, dataSource); } finally { try { //Cleanup after the subtasks dataSource.close(); this.logger.debug("Closed DataSource '" + dataSource + "' for " + dataSourceInfo + "."); } catch (SQLException e) { throw new RuntimeException( "Failed to close BasicDataSource '" + dataSource + "' for " + dataSourceInfo + ".", e); } } }
From source file:org.eclipse.dirigible.repository.datasource.DataSourceFacade.java
private DataSource createDirectDataSource(Properties properties) { String id = properties.getProperty(DataSourceFacade.PARAM_DB_ID); String name = properties.getProperty(DataSourceFacade.PARAM_DB_NAME); String url = properties.getProperty(DataSourceFacade.PARAM_DB_LOC); String driver = properties.getProperty(DataSourceFacade.PARAM_DB_DRIVER); String user = properties.getProperty(DataSourceFacade.PARAM_DB_USER); String password = properties.getProperty(DataSourceFacade.PARAM_DB_PASSWORD); String defaultAutoCommit = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_COMMIT); String maxActive = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_MAX_ACTIVE); String maxIdle = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_MAX_IDLE); String maxWait = properties.getProperty(DataSourceFacade.PARAM_DB_AUTO_MAX_WAIT); BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setDriverClassName(driver); basicDataSource.setUrl(url);//from w w w . j a v a 2 s. c om basicDataSource.setUsername(user); basicDataSource.setPassword(password); basicDataSource.setDefaultAutoCommit(Boolean.parseBoolean(defaultAutoCommit)); basicDataSource.setMaxActive(maxActive != null ? Integer.parseInt(maxActive) : 100); basicDataSource.setMaxIdle(maxIdle != null ? Integer.parseInt(maxIdle) : 30); basicDataSource.setMaxWait(maxWait != null ? Integer.parseInt(maxWait) : 10000); return basicDataSource; }
From source file:org.ff4j.test.utils.JdbcTestHelper.java
/** * Initialize DataSource with a pool of connections to HQL database. * * @return//w w w . j ava 2s . co 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.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 w w.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.geotools.referencing.factory.epsg.OracleDialectEpsgMediatorStarvationOnlineStressTest.java
protected DataSource connect(String user, String password, String url, Properties params) throws SQLException { //DataSource origional = super.connect( user, password, url, params ); BasicDataSource origional = new BasicDataSource(); origional.setDriverClassName("oracle.jdbc.driver.OracleDriver"); origional.setUsername(user);/*from ww w.j ava 2s . com*/ origional.setPassword(password); origional.setUrl(url); origional.setMaxActive(10); origional.setMaxIdle(1); return origional; }