List of usage examples for org.apache.commons.pool.impl StackKeyedObjectPoolFactory StackKeyedObjectPoolFactory
public StackKeyedObjectPoolFactory()
From source file:com.foxelbox.foxbukkit.database.DatabaseConnectionPool.java
private DatabaseConnectionPool() { try {//from ww w . j av a 2 s. c om Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { System.err.println("Error loading JBBC MySQL: " + e.toString()); } GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setMaxActive(10); connectionPool.setMaxIdle(5); connectionPool.setTestOnBorrow(true); connectionPool.setTestOnReturn(true); connectionPool.setTestWhileIdle(true); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( FoxBukkit.instance.configuration.getValue("database-uri", "jdbc:mysql://localhost:3306/foxbukkit_database"), FoxBukkit.instance.configuration.getValue("database-user", "root"), FoxBukkit.instance.configuration.getValue("database-password", "password")); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, new StackKeyedObjectPoolFactory(), "SELECT 1", false, true); poolableConnectionFactory.setValidationQueryTimeout(3); dataSource = new PoolingDataSource(connectionPool); try { initialize(); } catch (SQLException exc) { System.err.println("Error initializing MySQL Database"); exc.printStackTrace(); } }
From source file:com.cloud.utils.db.TransactionLegacy.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public static void initDataSource(Properties dbProps) { try {/*from w w w. j a v a2s .c om*/ if (dbProps.size() == 0) return; s_dbHAEnabled = Boolean.valueOf(dbProps.getProperty("db.ha.enabled")); s_logger.info("Is Data Base High Availiability enabled? Ans : " + s_dbHAEnabled); String loadBalanceStrategy = dbProps.getProperty("db.ha.loadBalanceStrategy"); // FIXME: If params are missing...default them???? final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive")); final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle")); final long cloudMaxWait = Long.parseLong(dbProps.getProperty("db.cloud.maxWait")); final String cloudUsername = dbProps.getProperty("db.cloud.username"); final String cloudPassword = dbProps.getProperty("db.cloud.password"); final String cloudHost = dbProps.getProperty("db.cloud.host"); final String cloudDriver = dbProps.getProperty("db.cloud.driver"); final int cloudPort = Integer.parseInt(dbProps.getProperty("db.cloud.port")); final String cloudDbName = dbProps.getProperty("db.cloud.name"); final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect")); final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery"); final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level"); int isolationLevel = Connection.TRANSACTION_READ_COMMITTED; if (cloudIsolationLevel == null) { isolationLevel = Connection.TRANSACTION_READ_COMMITTED; } else if (cloudIsolationLevel.equalsIgnoreCase("readcommitted")) { isolationLevel = Connection.TRANSACTION_READ_COMMITTED; } else if (cloudIsolationLevel.equalsIgnoreCase("repeatableread")) { isolationLevel = Connection.TRANSACTION_REPEATABLE_READ; } else if (cloudIsolationLevel.equalsIgnoreCase("serializable")) { isolationLevel = Connection.TRANSACTION_SERIALIZABLE; } else if (cloudIsolationLevel.equalsIgnoreCase("readuncommitted")) { isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED; } else { s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ". Using read uncommitted"); } final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow")); final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle")); final long cloudTimeBtwEvictionRunsMillis = Long .parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis")); final long cloudMinEvcitableIdleTimeMillis = Long .parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis")); final boolean cloudPoolPreparedStatements = Boolean .parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements")); final String url = dbProps.getProperty("db.cloud.url.params"); String cloudDbHAParams = null; String cloudSlaves = null; if (s_dbHAEnabled) { cloudDbHAParams = getDBHAParams("cloud", dbProps); cloudSlaves = dbProps.getProperty("db.cloud.slaves"); s_logger.info("The slaves configured for Cloud Data base is/are : " + cloudSlaves); } final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL")); if (useSSL) { System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore")); System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword")); System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore")); System.setProperty("javax.net.ssl.trustStorePassword", dbProps.getProperty("db.cloud.trustStorePassword")); } final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle); final String cloudConnectionUri = cloudDriver + "://" + cloudHost + (s_dbHAEnabled ? "," + cloudSlaves : "") + ":" + cloudPort + "/" + cloudDbName + "?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : "") + (s_dbHAEnabled ? "&" + cloudDbHAParams : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : ""); DriverLoader.loadDriver(cloudDriver); final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory(cloudConnectionUri, cloudUsername, cloudPassword); final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null); final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory( cloudConnectionFactory, cloudConnectionPool, poolableObjFactory, cloudValidationQuery, false, false, isolationLevel); // Default Data Source for CloudStack s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool()); // Configure the usage db final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive")); final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle")); final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait")); final String usageUsername = dbProps.getProperty("db.usage.username"); final String usagePassword = dbProps.getProperty("db.usage.password"); final String usageHost = dbProps.getProperty("db.usage.host"); final String usageDriver = dbProps.getProperty("db.usage.driver"); final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port")); final String usageDbName = dbProps.getProperty("db.usage.name"); final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect")); final String usageUrl = dbProps.getProperty("db.usage.url.params"); final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, usageMaxWait, usageMaxIdle); final String usageConnectionUri = usageDriver + "://" + usageHost + (s_dbHAEnabled ? "," + dbProps.getProperty("db.cloud.slaves") : "") + ":" + usagePort + "/" + usageDbName + "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : "") + (s_dbHAEnabled ? "&" + getDBHAParams("usage", dbProps) : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : ""); DriverLoader.loadDriver(usageDriver); final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory(usageConnectionUri, usageUsername, usagePassword); final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory( usageConnectionFactory, usageConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false); // Data Source for usage server s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool()); try { // Configure the simulator db final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive")); final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle")); final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait")); final String simulatorUsername = dbProps.getProperty("db.simulator.username"); final String simulatorPassword = dbProps.getProperty("db.simulator.password"); final String simulatorHost = dbProps.getProperty("db.simulator.host"); final String simulatorDriver = dbProps.getProperty("db.simulator.driver"); final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port")); final String simulatorDbName = dbProps.getProperty("db.simulator.name"); final boolean simulatorAutoReconnect = Boolean .parseBoolean(dbProps.getProperty("db.simulator.autoReconnect")); final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, simulatorMaxWait, simulatorMaxIdle); final String simulatorConnectionUri = simulatorDriver + "://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName + "?autoReconnect=" + simulatorAutoReconnect; DriverLoader.loadDriver(simulatorDriver); final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory( simulatorConnectionUri, simulatorUsername, simulatorPassword); final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory( simulatorConnectionFactory, simulatorConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false); s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool()); } catch (Exception e) { s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS"); } } catch (final Exception e) { s_ds = getDefaultDataSource("cloud"); s_usageDS = getDefaultDataSource("cloud_usage"); s_simulatorDS = getDefaultDataSource("cloud_simulator"); s_logger.warn( "Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration", e); } }
From source file:org.jaffa.persistence.engines.jdbcengine.datasource.DbcpDataSourceConnectionFactory.java
private void createDbcpDataSource() throws SQLException { try {/*from w w w . j av a2s .c o m*/ synchronized (dataSourceLock) { if (c_dataSource == null) { // First we load the underlying JDBC driver. Class.forName(getDriverClass()); // Next, we'll need a ObjectPool that serves as the actual pool of connections. // We'll use a GenericObjectPool instance GenericObjectPool connectionPool = new GenericObjectPool(null); if (getMaximumConnections() != null) connectionPool.setMaxActive(getMaximumConnections()); if (getMinimumConnections() != null) connectionPool.setMaxIdle(getMinimumConnections()); if (getMaxWait() != null) connectionPool.setMaxWait(getMaxWait()); if (getTestOnBorrow() != null) connectionPool.setTestOnBorrow(getTestOnBorrow()); if (getTestOnReturn() != null) connectionPool.setTestOnReturn(getTestOnReturn()); // Next, we'll create a ConnectionFactory that the pool will use to create Connections. // We'll use the DriverManagerConnectionFactory ConnectionFactory connectionFactory = null; if (m_driverProperties == null || m_driverProperties.isEmpty()) connectionFactory = new DriverManagerConnectionFactory(getUrl(), getUser(), getPassword()); else connectionFactory = new DriverManagerConnectionFactory(getUrl(), getDriverProperties()); // Now we'll create the PoolableConnectionFactory, which wraps the "real" Connections created by the ConnectionFactory with the classes that implement the pooling functionality. KeyedObjectPoolFactory stmtPoolFactory = new StackKeyedObjectPoolFactory(); String validationQuery = getValidationQuery(); boolean defaultReadOnly = false; boolean defaultAutoCommit = false; PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory, connectionPool, stmtPoolFactory, validationQuery, defaultReadOnly, defaultAutoCommit); // Finally, we create the PoolingDriver itself, passing in the object pool we created. c_dataSource = new PoolingDataSource(connectionPool); // This will allow us to access the underlying Connection objects, required by the JdbcSecurityPlugin ((PoolingDataSource) c_dataSource).setAccessToUnderlyingConnectionAllowed(true); if (log.isDebugEnabled()) log.debug("Created the Dbcp DataSource"); } } } catch (Exception e) { String str = "Error in creating the Dbcp DataSource"; log.error(str, e); throw new SQLException(str); } }
From source file:org.openbravo.database.ConnectionProviderImpl.java
public void addNewPool(String dbDriver, String dbServer, String dbLogin, String dbPassword, int minConns, int maxConns, double maxConnTime, String dbSessionConfig, String rdbms, String name) throws Exception { log4j.debug("Loading underlying JDBC driver."); try {//from w w w . j a v a 2 s.c om Class.forName(dbDriver); } catch (ClassNotFoundException e) { throw new Exception(e); } log4j.debug("Done."); GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); connectionPool.setMaxActive(maxConns); connectionPool.setTestOnBorrow(false); connectionPool.setTestOnReturn(false); connectionPool.setTestWhileIdle(false); KeyedObjectPoolFactory keyedObject = new StackKeyedObjectPoolFactory(); ConnectionFactory connectionFactory = new OpenbravoDriverManagerConnectionFactory(dbServer, dbLogin, dbPassword, dbSessionConfig, rdbms); @SuppressWarnings("unused") // required by dbcp PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, keyedObject, null, false, true); Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); driver.registerPool(contextName + "_" + name, connectionPool); if (this.defaultPoolName == null || this.defaultPoolName.equals("")) { this.defaultPoolName = name; this.bbdd = dbServer; this.rdbms = rdbms; } }