package org.enhydra.jdbc.standard;
import java.sql.SQLException;
import java.util.Hashtable;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import java.util.Enumeration;
import org.enhydra.jdbc.util.LRUCache;
public class StandardConnectionPoolDataSource
extends StandardDataSource
implements ConnectionPoolDataSource {
private Hashtable masterPrepStmtCache;
int preparedStmtCacheSize;
public static final int DEFAULT_PREPAREDSTMTCACHESIZE = 16;
public StandardConnectionPoolDataSource() {
super();
masterPrepStmtCache = new Hashtable();
preparedStmtCacheSize = DEFAULT_PREPAREDSTMTCACHESIZE;
}
public PooledConnection getPooledConnection() throws SQLException {
log.debug(
"StandardConnectionPoolDataSource:getPooledConnection(0) return a pooled connection");
return getPooledConnection(user, password);
}
synchronized public PooledConnection getPooledConnection(
String user,
String password)
throws SQLException {
log.debug(
"StandardConnectionPoolDataSource:getPooledConnection(2) return a pooled connection");
StandardPooledConnection spc =
new StandardPooledConnection(this, user, password);
spc.setLogger(log);
return spc;
}
public Hashtable getMasterPrepStmtCache() {
return masterPrepStmtCache;
}
public int getPreparedStmtCacheSize() {
return preparedStmtCacheSize;
}
public void setPreparedStmtCacheSize(int value) {
preparedStmtCacheSize = value;
if (preparedStmtCacheSize <= 0) {
masterPrepStmtCache.clear();
} else {
Enumeration enum = masterPrepStmtCache.elements();
while (enum.hasMoreElements()) {
((LRUCache) enum.nextElement()).resize(value);
}
}
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("StandardConnectionPoolDataSource:\n");
sb.append(" master prepared stmt cache size=<"+this.masterPrepStmtCache.size()+">\n");
sb.append(" prepared stmt cache size =<"+this.preparedStmtCacheSize+">\n");
sb.append(super.toString());
return sb.toString();
}
}