001: /*
002: * XAPool: Open Source XA JDBC Pool
003: * Copyright (C) 2003 Objectweb.org
004: * Initial Developer: Lutris Technologies Inc.
005: * Contact: xapool-public@lists.debian-sf.objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: */
022: package org.enhydra.jdbc.standard;
023:
024: import java.sql.SQLException;
025: import java.util.Hashtable;
026: import javax.sql.ConnectionPoolDataSource;
027: import javax.sql.PooledConnection;
028: import java.util.Enumeration;
029: import org.enhydra.jdbc.util.LRUCache;
030:
031: /**
032: * A data source used to create generic pooled connections (factory)
033: */
034: public class StandardConnectionPoolDataSource extends
035: StandardDataSource implements ConnectionPoolDataSource {
036:
037: private Hashtable masterPrepStmtCache;
038: int preparedStmtCacheSize; // size of prepared statement cache
039: public static final int DEFAULT_PREPAREDSTMTCACHESIZE = 16;
040:
041: /**
042: * Constructor.
043: */
044: public StandardConnectionPoolDataSource() {
045: super ();
046: masterPrepStmtCache = new Hashtable();
047: preparedStmtCacheSize = DEFAULT_PREPAREDSTMTCACHESIZE;
048: }
049:
050: /**
051: * Create a pooled connection using the default username and password.
052: */
053: public PooledConnection getPooledConnection() throws SQLException {
054: log
055: .debug("StandardConnectionPoolDataSource:getPooledConnection(0) return a pooled connection");
056: return getPooledConnection(user, password);
057: }
058:
059: /**
060: * Create a standard pooled connection using the supplied username and password.
061: */
062: synchronized public PooledConnection getPooledConnection(
063: String user, String password) throws SQLException {
064: log
065: .debug("StandardConnectionPoolDataSource:getPooledConnection(2) return a pooled connection");
066: StandardPooledConnection spc = new StandardPooledConnection(
067: this , user, password);
068: spc.setLogger(log);
069: return spc;
070: }
071:
072: public Hashtable getMasterPrepStmtCache() {
073: return masterPrepStmtCache;
074: }
075:
076: /**
077: * Gets the size of the prepared statement cache
078: */
079: public int getPreparedStmtCacheSize() {
080: return preparedStmtCacheSize;
081: }
082:
083: /**
084: * Sets the size of the prepared statement cache
085: */
086: public void setPreparedStmtCacheSize(int value) {
087: preparedStmtCacheSize = value;
088: if (preparedStmtCacheSize <= 0) {
089: masterPrepStmtCache.clear();
090: } else {
091: Enumeration enumeration = masterPrepStmtCache.elements();
092: while (enumeration.hasMoreElements()) {
093: ((LRUCache) enumeration.nextElement()).resize(value);
094: }
095: }
096: }
097:
098: public String toString() {
099: StringBuffer sb = new StringBuffer();
100: sb.append("StandardConnectionPoolDataSource:\n");
101: sb.append(" master prepared stmt cache size=<"
102: + this .masterPrepStmtCache.size() + ">\n");
103: sb.append(" prepared stmt cache size =<"
104: + this .preparedStmtCacheSize + ">\n");
105: sb.append(super.toString());
106:
107: return sb.toString();
108: }
109: }
|