KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > PreparedStatementCacheItem


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.CacheListener;
33
34 import java.lang.ref.SoftReference JavaDoc;
35 import java.sql.PreparedStatement JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Represtents a prepared statement.
41  */

42 class PreparedStatementCacheItem implements CacheListener {
43   private final static Logger JavaDoc log = Log.open(PreparedStatementCacheItem.class);
44
45   private PreparedStatementKey _key;
46   private SoftReference JavaDoc<PreparedStatement JavaDoc> _pStmtRef;
47   private ManagedConnectionImpl _mConn;
48
49   private boolean _isActive;
50   private boolean _isRemoved;
51
52   PreparedStatementCacheItem(PreparedStatementKey key,
53                  PreparedStatement JavaDoc pStmt,
54                  ManagedConnectionImpl mConn)
55   {
56     if (pStmt == null)
57       throw new NullPointerException JavaDoc();
58
59     _key = key;
60     _pStmtRef = new SoftReference JavaDoc<PreparedStatement JavaDoc>(pStmt);
61     _mConn = mConn;
62   }
63
64   /**
65    * Activates the cache item.
66    */

67   UserPreparedStatement toActive(UserConnection conn)
68   {
69     SoftReference JavaDoc<PreparedStatement JavaDoc> ref = _pStmtRef;
70
71     if (ref == null)
72       return null;
73     
74     PreparedStatement JavaDoc pStmt = ref.get();
75
76     if (pStmt == null) {
77       _mConn.remove(_key);
78       return null;
79     }
80     
81     synchronized (this) {
82       if (_isActive)
83     return null;
84       _isActive = true;
85     }
86
87     return new UserPreparedStatement(conn, pStmt, this);
88   }
89
90   void toIdle()
91   {
92     boolean doClose = false;
93
94     synchronized (this) {
95       if (_isRemoved) {
96     _isRemoved = true;
97     doClose = _isActive;
98       }
99       _isActive = false;
100     }
101
102     if (doClose) {
103       try {
104     PreparedStatement JavaDoc pStmt = _pStmtRef.get();
105     _pStmtRef = null;
106
107     if (pStmt != null)
108       pStmt.close();
109       } catch (Throwable JavaDoc e) {
110     log.log(Level.FINE, e.toString(), e);
111       }
112     }
113   }
114
115   /**
116    * Returns true for a removed item.
117    */

118   boolean isRemoved()
119   {
120     return _isRemoved;
121   }
122
123   /**
124    * Called when removed from the cache.
125    */

126   public void removeEvent()
127   {
128     boolean doClose = false;
129
130     synchronized (this) {
131       if (! _isRemoved) {
132     _isRemoved = true;
133     doClose = ! _isActive;
134       }
135     }
136
137     if (doClose) {
138       try {
139     PreparedStatement JavaDoc pStmt = _pStmtRef.get();
140     _pStmtRef = null;
141
142     if (pStmt != null)
143       pStmt.close();
144       } catch (Throwable JavaDoc e) {
145     log.log(Level.FINE, e.toString(), e);
146       }
147     }
148   }
149
150   void destroy()
151   {
152     _isRemoved = true;
153     
154     SoftReference JavaDoc<PreparedStatement JavaDoc> ref = _pStmtRef;
155     _pStmtRef = null;
156
157     if (ref != null) {
158       PreparedStatement JavaDoc pStmt = ref.get();
159
160       if (pStmt != null) {
161     try {
162       pStmt.close();
163     } catch (Throwable JavaDoc e) {
164       log.log(Level.FINE, e.toString(), e);
165     }
166       }
167     }
168   }
169 }
170
Popular Tags