package com.vci.server.base.dbutil; import java.io.FileInputStream; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import com.vci.common.log.ServerWithLog4j; public final class DBPoolInit { private String dbURL; // Database URL private Properties dbProperties; // Properties read from properties file private String driverClass; // Driver class name private String userName = ""; private String password = ""; private String fileName = "properties/sys.properties"; public DBPoolInit() { } public void InitProperties(String strProsFileName) { dbProperties = new Properties(); if ( !strProsFileName.equalsIgnoreCase("") ) fileName = strProsFileName; try { FileInputStream propertiesFileStream = new FileInputStream(fileName); dbProperties.load(propertiesFileStream); } catch(Exception e) { e.printStackTrace(); } } public DataSource getDBPool() throws ClassNotFoundException, SQLException, Exception { BasicDataSource ds = new BasicDataSource(); driverClass = (String)dbProperties.get("DB.DRIVER"); dbURL = (String)dbProperties.get("DB.URL"); userName = ((String)dbProperties.get("DB.USERNAME")).trim(); password = ((String)dbProperties.get("DB.PASSWORD")).trim();; String otherProperties = (String)dbProperties.get("DB.SUB_PROPERTIES"); if(driverClass == null || dbURL == null){ throw new Exception("Driver Class or Driver URL should not be null"); } ServerWithLog4j.logger.info("Registering driver : "+driverClass); if( (userName.length() == 0) && (password.length() == 0) && (otherProperties.length() == 0) ) { ServerWithLog4j.logger.info("UserName is null or password is null!"); } else { ds.setDriverClassName (driverClass); ds.setUsername (userName); ds.setPassword (password); ds.setUrl (dbURL); int maxactive = Integer.parseInt((String)dbProperties.get("PT.MAXACTIVE")); int maxidle = Integer.parseInt((String)dbProperties.get("PT.MAXIDLE")); int minidle = Integer.parseInt((String)dbProperties.get("PT.MINIDLE")); int maxwait = Integer.parseInt((String)dbProperties.get("PT.MAXWAIT")); ds.setMaxActive(maxactive); ds.setMaxIdle(maxidle); ds.setMinIdle(minidle); ds.setMaxWait(maxwait); ds.setDefaultAutoCommit(false); } ServerWithLog4j.logger.info(dbURL); ServerWithLog4j.logger.info("Database connect successful!"); return ds; } }