package oracle.otnsamples.jdbc;
import oracle.jdbc.pool.OracleConnectionCacheManager;
import oracle.jdbc.pool.OracleDataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
public class ConnCacheBean {
private static final String CACHE_NAME = "CacheSample";
private static int MAX_LIMIT = 15;
private static ConnCacheBean thisInstance = null;
private OracleConnectionCacheManager connMgr = null;
private OracleDataSource ods = null;
private ConnCacheBean() throws Exception {
if (ods == null) {
initializeConnectionCache( );
}
}
public static ConnCacheBean getInstance() throws Exception {
if (thisInstance == null) {
thisInstance = new ConnCacheBean();
}
return thisInstance;
}
public int getActiveSize() throws Exception {
try {
return connMgr.getNumberOfActiveConnections(CACHE_NAME);
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
throw new Exception("SQL Error while getting the no of active " +
" connections"
);
}
}
public int getAvailableConnections() throws Exception {
return connMgr.getNumberOfAvailableConnections(CACHE_NAME) ;
}
public int getCacheSize() throws Exception {
return connMgr.getNumberOfActiveConnections(CACHE_NAME) +
connMgr.getNumberOfAvailableConnections(CACHE_NAME);
}
public OracleDataSource getDataSource() {
return ods;
}
public int getCacheMaxLimit() {
return MAX_LIMIT;
}
public void setCacheProperties(Properties properties) throws SQLException {
connMgr.reinitializeCache(CACHE_NAME, properties);
String maxLimit = properties.getProperty("MaxLimit") ;
if ( maxLimit != null && maxLimit.trim().length() > 0 ) {
MAX_LIMIT = new Integer(maxLimit).intValue();
}
}
public Properties getCacheProperties() throws SQLException {
return ods.getConnectionCacheProperties() ;
}
public void closeConnCache() throws SQLException {
if (ods != null) {
ods.close();
}
}
private void initializeConnectionCache() throws Exception {
if (ods == null) {
try {
this.initializeConnectionCacheDataSrc();
connMgr =
OracleConnectionCacheManager.getConnectionCacheManagerInstance();
Properties properties = new Properties();
properties.setProperty("MinLimit", "1");
properties.setProperty("MaxLimit", "15");
properties.setProperty("InitialLimit", "10");
connMgr.createCache(CACHE_NAME, ods, properties);
} catch (java.sql.SQLException ex) {
throw new Exception("SQL Error while Instantiating Connection Cache : \n" +
ex.toString()
);
} catch (java.lang.Exception ex) {
throw new Exception("Exception : \n" + ex.toString());
}
}
}
private void initializeConnectionCacheDataSrc()
throws Exception {
try {
ods = new OracleDataSource();
this.configDSConnection();
ods.setConnectionCachingEnabled(true);
ods.setConnectionCacheName(CACHE_NAME);
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
throw new Exception("SQL Errors = " + sqlEx.toString());
}catch (Exception ex) {
ex.printStackTrace();
throw new Exception("Generic Errors = " + ex.toString());
}
}
private void configDSConnection() {
try {
Properties prop = this.loadParams("Connection");
ods.setServerName(prop.getProperty("HostName"));
ods.setServiceName( prop.getProperty("SID"));
ods.setPortNumber(new Integer( prop.getProperty("Port")).intValue());
ods.setDriverType("thin");
ods.setUser( prop.getProperty("UserName") );
ods.setPassword( prop.getProperty("Password"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
private Properties loadParams(String file) throws IOException {
Properties prop = new Properties();
ResourceBundle bundle = ResourceBundle.getBundle(file);
Enumeration enum = bundle.getKeys();
String key = null;
while (enum.hasMoreElements()) {
key = (String) enum.nextElement();
prop.put(key, bundle.getObject(key));
}
return prop;
}
}
|