KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > PreparedStatementCache


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jdbc;
23
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 import org.jboss.logging.Logger;
28 import org.jboss.util.LRUCachePolicy;
29
30 /**
31  * LRU cache for PreparedStatements. When ps ages out, close it.
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 40406 $
37  */

38 public class PreparedStatementCache extends LRUCachePolicy
39 {
40    private final Logger log = Logger.getLogger(getClass());
41
42    public static class Key
43    {
44       public static final int PREPARED_STATEMENT = 1;
45       public static final int CALLABLE_STATEMENT = 2;
46       private final String JavaDoc sql;
47       private final int type;
48       private final int resultSetType;
49       private final int resultSetConcurrency;
50
51       public Key(String JavaDoc sql, int type, int resultSetType, int resultSetConcurrency)
52       {
53          this.sql = sql;
54          this.type = type;
55          this.resultSetType = resultSetType;
56          this.resultSetConcurrency = resultSetConcurrency;
57       }
58
59       public boolean equals(Object JavaDoc o)
60       {
61          if (this == o) return true;
62          if (o == null || o instanceof Key == false) return false;
63
64          final Key key = (Key) o;
65
66          if (resultSetConcurrency != key.resultSetConcurrency) return false;
67          if (resultSetType != key.resultSetType) return false;
68          if (type != key.type) return false;
69          return !(sql != null ? !sql.equals(key.sql) : key.sql != null);
70
71       }
72
73       public int hashCode()
74       {
75          int result;
76          result = (sql != null ? sql.hashCode() : 0);
77          result = 29 * result + type;
78          result = 29 * result + resultSetType;
79          result = 29 * result + resultSetConcurrency;
80          return result;
81       }
82       public String JavaDoc toString()
83       {
84          StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(super.toString());
85          tmp.append('[');
86          tmp.append("sql=");
87          tmp.append(sql);
88          tmp.append(" type=");
89          tmp.append(type==PREPARED_STATEMENT ? "PS" : "CS");
90          tmp.append(" resultSetType=");
91          switch (resultSetType)
92          {
93             case ResultSet.TYPE_FORWARD_ONLY:
94             {
95                tmp.append("TYPE_FORWARD_ONLY");
96                break;
97             }
98             case ResultSet.TYPE_SCROLL_INSENSITIVE:
99             {
100                tmp.append("TYPE_SCROLL_INSENSITIVE");
101                break;
102             }
103             case ResultSet.TYPE_SCROLL_SENSITIVE:
104             {
105                tmp.append("TYPE_SCROLL_SENSITIVE");
106                break;
107             }
108             default:
109                tmp.append(resultSetType);
110          }
111          tmp.append(" resultSetConcurrency=");
112          switch (resultSetConcurrency)
113          {
114             case ResultSet.CONCUR_READ_ONLY:
115             {
116                tmp.append("CONCUR_READ_ONLY");
117                break;
118             }
119             case ResultSet.CONCUR_UPDATABLE:
120             {
121                tmp.append("CONCUR_UPDATABLE");
122                break;
123             }
124             default:
125                tmp.append(resultSetConcurrency);
126          }
127          tmp.append(']');
128          return tmp.toString();
129       }
130    }
131
132    public PreparedStatementCache(int max)
133    {
134       super(2, max);
135       create();
136    }
137
138    protected void ageOut(LRUCachePolicy.LRUCacheEntry entry)
139    {
140       try
141       {
142          CachedPreparedStatement ws = (CachedPreparedStatement) entry.m_object;
143          ws.agedOut();
144       }
145       catch (SQLException JavaDoc e)
146       {
147          log.debug("Failed closing cached statement", e);
148       }
149       finally
150       {
151          super.ageOut(entry);
152       }
153    }
154 }
155
Popular Tags