/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.resource.adapter.jdbc; import java.sql.SQLException; import org.jboss.logging.Logger; import org.jboss.util.LRUCachePolicy; /** * LRU cache for PreparedStatements. When ps ages out, close it. * * @author Bill Burke * @author Adrian Brock * @author Scott.Stark@jboss.org * @version $Revision: 1.8 $ */ public class PreparedStatementCache extends LRUCachePolicy { public static class Key { public static final int PREPARED_STATEMENT = 1; public static final int CALLABLE_STATEMENT = 2; private final String sql; private final int type; private final int resultSetType; private final int resultSetConcurrency; public Key(String sql, int type, int resultSetType, int resultSetConcurrency) { this.sql = sql; this.type = type; this.resultSetType = resultSetType; this.resultSetConcurrency = resultSetConcurrency; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final Key key = (Key) o; if (resultSetConcurrency != key.resultSetConcurrency) return false; if (resultSetType != key.resultSetType) return false; if (type != key.type) return false; return !(sql != null ? !sql.equals(key.sql) : key.sql != null); } public int hashCode() { int result; result = (sql != null ? sql.hashCode() : 0); result = 29 * result + type; result = 29 * result + resultSetType; result = 29 * result + resultSetConcurrency; return result; } public String toString() { StringBuffer tmp = new StringBuffer(super.toString()); tmp.append('['); tmp.append("sql="); tmp.append(sql); tmp.append("type="); tmp.append(type==PREPARED_STATEMENT ? "PS" : "CS"); tmp.append("rstype="); tmp.append(resultSetType); tmp.append("rsconcur="); tmp.append(resultSetConcurrency); tmp.append(']'); return tmp.toString(); } } private final Logger log = Logger.getLogger(getClass()); public PreparedStatementCache(int max) { super(2, max); create(); } protected void ageOut(LRUCachePolicy.LRUCacheEntry entry) { try { CachedPreparedStatement ws = (CachedPreparedStatement) entry.m_object; ws.agedOut(); } catch (SQLException e) { log.debug("Failed closing cached statement", e); } finally { super.ageOut(entry); } } }