List of usage examples for org.apache.commons.dbcp.datasources SharedPoolDataSource setMaxIdle
public void setMaxIdle(int maxIdle)
From source file:com.silverpeas.wysiwyg.dynamicvalue.pool.ConnectionPoolWithJDBC.java
/** * Initializes the datasource. This datasource is based on the SharedPoolDataSource class, a dbcp * implementation/*from w w w.jav a 2 s. co m*/ */ private synchronized static void initializeDatasource() { SilverTrace.debug("wysiwyg", ConnectionPoolWithJDBC.class.toString(), " Datasource initialization : starting ..."); if (ds == null) { // check if the information for the pool creation is present. if (poolInfo == null) { SilverTrace.error("wysiwig", ConnectionPoolWithJDBC.class.toString(), "wysiwig.CONNECTION_INIALIZATION_FAILED"); throw new TechnicalException(ConnectionPoolWithJDBC.class.toString() + " : An error occurred during the connection initialization. The Pool information must be set"); } SilverTrace.debug("wysiwyg", ConnectionPoolWithJDBC.class.toString(), " Datasource initialization : poolInfo detail :: " + poolInfo.toString()); // driver registration DriverAdapterCPDS cpds = new DriverAdapterCPDS(); try { cpds.setDriver(poolInfo.getDriver()); } catch (ClassNotFoundException e) { SilverTrace.error("wysiwig", ConnectionPoolWithJDBC.class.toString(), "wysiwig.DRIVER_MISSING"); throw new TechnicalException(ConnectionPoolWithJDBC.class.toString() + " : An error occurred during the connection initializatoin. The JDBC driver isn't in the classpath", e); } cpds.setUrl(poolInfo.getUrl()); cpds.setUser(poolInfo.getUser()); cpds.setPassword(poolInfo.getPassword()); // datasource object creation SharedPoolDataSource tds = new SharedPoolDataSource(); tds.setConnectionPoolDataSource(cpds); tds.setMaxActive(poolInfo.getMaxActive()); tds.setMaxWait(poolInfo.getMaxWait()); tds.setMaxIdle(poolInfo.getMaxIdle()); tds.setTimeBetweenEvictionRunsMillis(poolInfo.getTimeBetweenEvictionRunsMillis()); tds.setNumTestsPerEvictionRun(poolInfo.getNumTestsPerEvictionRun()); tds.setMinEvictableIdleTimeMillis(poolInfo.getMinEvictableIdleTimeMillis()); ds = tds; SilverTrace.debug("wysiwyg", ConnectionPoolWithJDBC.class.toString(), " Datasource initialization : ending ..."); } }
From source file:com.stehno.sanctuary.core.Sanctuary.java
private static DataSource createDataSource() throws ClassNotFoundException { SharedPoolDataSource poolDataSource = new SharedPoolDataSource(); DriverAdapterCPDS cpds = new DriverAdapterCPDS(); cpds.setDriver("org.h2.Driver"); cpds.setUrl("jdbc:h2:/home/cjstehno/h2/sanctuary"); cpds.setUser("sa"); cpds.setPassword(""); poolDataSource.setConnectionPoolDataSource(cpds); poolDataSource.setMaxActive(10);//from w w w.jav a2 s . co m poolDataSource.setMaxIdle(5); poolDataSource.setMinEvictableIdleTimeMillis(1000); poolDataSource.setTestWhileIdle(true); poolDataSource.setValidationQuery("select 1"); return poolDataSource; }
From source file:hk.hku.cecid.piazza.commons.dao.ds.SimpleDSDAOFactory.java
/** * Initializes this DAOFactory./* w w w . ja va 2 s. c om*/ */ public void initFactory() throws DAOException { try { String driver = null; String url = null; String username = null; String password = null; boolean isPooling = true; int maxIdle = 0; int maxActive = 10; int maxWait = 50; boolean testOnBorrow = false; boolean testOnReturn = false; boolean testWhileIdle = false; String validationQuery = null; try { driver = getParameter("driver"); url = getParameter("url"); username = getParameter("username", null); password = getParameter("password", null); maxIdle = StringUtilities.parseInt(getParameter("maxIdle", null), 0); maxActive = StringUtilities.parseInt(getParameter("maxActive", null), 0); maxWait = StringUtilities.parseInt(getParameter("maxWait", null), -1); validationQuery = StringUtilities.trim(getParameter("validationQuery", null)); if (validationQuery != null) { testOnBorrow = StringUtilities.parseBoolean(getParameter("testOnBorrow", "false")); testOnReturn = StringUtilities.parseBoolean(getParameter("testOnReturn", "false")); testWhileIdle = StringUtilities.parseBoolean(getParameter("testWhileIdle", "false")); } } catch (Exception e) { throw new DAOException("Invalid parameter for SimpleDSDAOFactory."); } if (getParameter("pooling", null) != null) { if (!getParameter("pooling").equalsIgnoreCase("true") && !getParameter("pooling").equalsIgnoreCase("false")) { throw new DAOException("Invalid parameter for SimpleDSDAOFactory."); } isPooling = StringUtilities.parseBoolean(getParameter("pooling", "true")); } DataSource datasource; if (isPooling) { DriverAdapterCPDS cpds = new DriverAdapterCPDS(); cpds.setDriver(driver); cpds.setUrl(url); cpds.setUser(username); cpds.setPassword(password); SharedPoolDataSource sds = new SharedPoolDataSource(); sds.setConnectionPoolDataSource(cpds); sds.setMaxIdle(maxIdle); sds.setMaxActive(maxActive); sds.setMaxWait(maxWait); sds.setTestOnBorrow(testOnBorrow); sds.setTestOnReturn(testOnReturn); sds.setTestWhileIdle(testWhileIdle); sds.setValidationQuery(validationQuery); datasource = sds; } else { datasource = new SimpleDataSource(driver, url, username, password); } setDataSource(datasource); } catch (Exception e) { throw new DAOException("Cannot initialize SimpleDSDAOFactory!", e); } }
From source file:com.smartmarmot.orabbix.Configurator.java
private DBConn getConnection(String dbName) throws Exception { try {//from w w w . j a v a 2 s.co m verifyConfig(); SmartLogger.logThis(Level.DEBUG, "getConnection for database " + dbName); String url = ""; try { url = new String(_props.getProperty(dbName + "." + Constants.CONN_URL)); } catch (Exception ex) { SmartLogger.logThis(Level.ERROR, "Error on Configurator getConnection while getting " + dbName + "." + Constants.CONN_URL + " " + ex.getMessage()); } String uname = ""; try { uname = new String(_props.getProperty(dbName + "." + Constants.CONN_USERNAME)); } catch (Exception ex) { try { SmartLogger.logThis(Level.DEBUG, "Error on Configurator getConnection while getting " + dbName + "." + Constants.CONN_USERNAME + " " + ex.getMessage()); uname = new String(_props.getProperty(Constants.CONN_DEFAULT_USERNAME)); } catch (Exception ex1) { SmartLogger.logThis(Level.ERROR, "Error on Configurator getConnection while getting " + Constants.CONN_DEFAULT_USERNAME + " " + ex1.getMessage()); } } String password = ""; try { password = new String(_props.getProperty(dbName + "." + Constants.CONN_PASSWORD)); } catch (Exception ex) { try { SmartLogger.logThis(Level.DEBUG, "Error on Configurator getConnection while getting " + dbName + "." + Constants.CONN_PASSWORD + " " + ex.getMessage()); password = new String(_props.getProperty(Constants.CONN_DEFAULT_PASSWORD)); } catch (Exception ex1) { SmartLogger.logThis(Level.ERROR, "Error on Configurator getConnection while getting " + dbName + "." + Constants.CONN_PASSWORD + " " + ex.getMessage()); } } DriverAdapterCPDS cpds = new DriverAdapterCPDS(); cpds.setDriver(Constants.ORACLE_DRIVER); cpds.setUrl(url.toString()); cpds.setUser(uname.toString()); cpds.setPassword(password.toString()); SharedPoolDataSource tds = new SharedPoolDataSource(); tds.setConnectionPoolDataSource(cpds); // tds.setMaxActive(5); Integer maxActive = new Integer(5); try { maxActive = new Integer(_props.getProperty(dbName + "." + Constants.CONN_MAX_ACTIVE)); } catch (Exception ex) { SmartLogger.logThis(Level.DEBUG, "Note: " + dbName + "." + Constants.CONN_MAX_ACTIVE + " " + ex.getMessage()); try { maxActive = new Integer( _props.getProperty(Constants.DATABASES_LIST + "." + Constants.CONN_MAX_ACTIVE)); } catch (Exception e) { SmartLogger.logThis(Level.WARN, "Note: " + Constants.DATABASES_LIST + "." + Constants.CONN_MAX_ACTIVE + " " + e.getMessage()); SmartLogger.logThis(Level.WARN, "Warning I will use default value " + maxActive); } } tds.setMaxActive(maxActive.intValue()); Integer maxWait = new Integer(100); try { maxWait = new Integer(_props.getProperty(dbName + "." + Constants.CONN_MAX_WAIT)); } catch (Exception ex) { SmartLogger.logThis(Level.DEBUG, "Note: " + dbName + "." + Constants.CONN_MAX_WAIT + " " + ex.getMessage()); try { maxWait = new Integer( _props.getProperty(Constants.DATABASES_LIST + "." + Constants.CONN_MAX_WAIT)); } catch (Exception e) { SmartLogger.logThis(Level.WARN, "Note: " + Constants.DATABASES_LIST + "." + Constants.CONN_MAX_WAIT + " " + e.getMessage()); SmartLogger.logThis(Level.WARN, "Warning I will use default value " + maxWait); } } tds.setMaxWait(maxWait.intValue()); Integer maxIdle = new Integer(1); try { maxIdle = new Integer(_props.getProperty(dbName + "." + Constants.CONN_MAX_IDLE)); } catch (Exception ex) { SmartLogger.logThis(Level.DEBUG, "Note: " + dbName + "." + Constants.CONN_MAX_IDLE + " " + ex.getMessage()); try { maxIdle = new Integer( _props.getProperty(Constants.DATABASES_LIST + "." + Constants.CONN_MAX_IDLE)); } catch (Exception e) { SmartLogger.logThis(Level.WARN, "Note: " + Constants.DATABASES_LIST + "." + Constants.CONN_MAX_IDLE + " " + e.getMessage()); SmartLogger.logThis(Level.WARN, "Warning I will use default value " + maxIdle); } } tds.setMaxIdle(maxIdle.intValue()); SmartLogger.logThis(Level.INFO, "DB Pool created: " + tds); SmartLogger.logThis(Level.INFO, "URL=" + url.toString()); SmartLogger.logThis(Level.INFO, "maxPoolSize=" + tds.getMaxActive()); SmartLogger.logThis(Level.INFO, "maxIdleSize=" + tds.getMaxIdle()); SmartLogger.logThis(Level.INFO, "maxIdleTime=" + tds.getMinEvictableIdleTimeMillis() + "ms"); SmartLogger.logThis(Level.INFO, "poolTimeout=" + tds.getMaxWait()); SmartLogger.logThis(Level.INFO, "timeBetweenEvictionRunsMillis=" + tds.getTimeBetweenEvictionRunsMillis()); SmartLogger.logThis(Level.INFO, "numTestsPerEvictionRun=" + tds.getNumTestsPerEvictionRun()); tds.setValidationQuery(Constants.ORACLE_VALIDATION_QUERY); Connection con = null; con = tds.getConnection(); PreparedStatement p_stmt = null; p_stmt = con.prepareStatement(Constants.ORACLE_WHOAMI_QUERY); ResultSet rs = null; rs = p_stmt.executeQuery(); String tempStr = new String(""); ResultSetMetaData rsmd = rs.getMetaData(); int numColumns = rsmd.getColumnCount(); while (rs.next()) { for (int r = 1; r < numColumns + 1; r++) { tempStr = tempStr + rs.getObject(r).toString().trim(); } } SmartLogger.logThis(Level.INFO, "Connected as " + tempStr); con.close(); con = null; con = tds.getConnection(); p_stmt = con.prepareStatement(Constants.ORACLE_DBNAME_QUERY); rs = p_stmt.executeQuery(); rsmd = rs.getMetaData(); numColumns = rsmd.getColumnCount(); tempStr = ""; while (rs.next()) { for (int r = 1; r < numColumns + 1; r++) { tempStr = tempStr + rs.getObject(r).toString().trim(); } } SmartLogger.logThis(Level.INFO, "--------- on Database -> " + tempStr); con.close(); con = null; DBConn mydbconn = new DBConn(tds, dbName.toString()); return mydbconn; } catch (Exception ex) { SmartLogger.logThis(Level.ERROR, "Error on Configurator for database " + dbName + " -->" + ex.getMessage()); return null; } }