001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software 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 software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jdbc;
023:
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026:
027: import org.jboss.logging.Logger;
028: import org.jboss.util.LRUCachePolicy;
029:
030: /**
031: * LRU cache for PreparedStatements. When ps ages out, close it.
032: *
033: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
034: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
035: * @author Scott.Stark@jboss.org
036: * @version $Revision: 57189 $
037: */
038: public class PreparedStatementCache extends LRUCachePolicy {
039: private final Logger log = Logger.getLogger(getClass());
040:
041: public static class Key {
042: public static final int PREPARED_STATEMENT = 1;
043: public static final int CALLABLE_STATEMENT = 2;
044: private final String sql;
045: private final int type;
046: private final int resultSetType;
047: private final int resultSetConcurrency;
048:
049: public Key(String sql, int type, int resultSetType,
050: int resultSetConcurrency) {
051: this .sql = sql;
052: this .type = type;
053: this .resultSetType = resultSetType;
054: this .resultSetConcurrency = resultSetConcurrency;
055: }
056:
057: public boolean equals(Object o) {
058: if (this == o)
059: return true;
060: if (o == null || o instanceof Key == false)
061: return false;
062:
063: final Key key = (Key) o;
064:
065: if (resultSetConcurrency != key.resultSetConcurrency)
066: return false;
067: if (resultSetType != key.resultSetType)
068: return false;
069: if (type != key.type)
070: return false;
071: return !(sql != null ? !sql.equals(key.sql)
072: : key.sql != null);
073:
074: }
075:
076: public int hashCode() {
077: int result;
078: result = (sql != null ? sql.hashCode() : 0);
079: result = 29 * result + type;
080: result = 29 * result + resultSetType;
081: result = 29 * result + resultSetConcurrency;
082: return result;
083: }
084:
085: public String toString() {
086: StringBuffer tmp = new StringBuffer(super .toString());
087: tmp.append('[');
088: tmp.append("sql=");
089: tmp.append(sql);
090: tmp.append(" type=");
091: tmp.append(type == PREPARED_STATEMENT ? "PS" : "CS");
092: tmp.append(" resultSetType=");
093: switch (resultSetType) {
094: case ResultSet.TYPE_FORWARD_ONLY: {
095: tmp.append("TYPE_FORWARD_ONLY");
096: break;
097: }
098: case ResultSet.TYPE_SCROLL_INSENSITIVE: {
099: tmp.append("TYPE_SCROLL_INSENSITIVE");
100: break;
101: }
102: case ResultSet.TYPE_SCROLL_SENSITIVE: {
103: tmp.append("TYPE_SCROLL_SENSITIVE");
104: break;
105: }
106: default:
107: tmp.append(resultSetType);
108: }
109: tmp.append(" resultSetConcurrency=");
110: switch (resultSetConcurrency) {
111: case ResultSet.CONCUR_READ_ONLY: {
112: tmp.append("CONCUR_READ_ONLY");
113: break;
114: }
115: case ResultSet.CONCUR_UPDATABLE: {
116: tmp.append("CONCUR_UPDATABLE");
117: break;
118: }
119: default:
120: tmp.append(resultSetConcurrency);
121: }
122: tmp.append(']');
123: return tmp.toString();
124: }
125: }
126:
127: public PreparedStatementCache(int max) {
128: super (2, max);
129: create();
130: }
131:
132: protected void ageOut(LRUCachePolicy.LRUCacheEntry entry) {
133: try {
134: CachedPreparedStatement ws = (CachedPreparedStatement) entry.m_object;
135: ws.agedOut();
136: } catch (SQLException e) {
137: log.debug("Failed closing cached statement", e);
138: } finally {
139: super.ageOut(entry);
140: }
141: }
142: }
|