List of usage examples for org.apache.commons.dbcp.datasources SharedPoolDataSource getConnection
public Connection getConnection() throws SQLException
From source file:com.smartmarmot.orabbix.Configurator.java
private DBConn getConnection(String dbName) throws Exception { try {//w w w.j a va2 s . c o 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; } }