001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.sql;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.CacheListener;
033:
034: import java.lang.ref.SoftReference;
035: import java.sql.PreparedStatement;
036: import java.util.logging.Level;
037: import java.util.logging.Logger;
038:
039: /**
040: * Represtents a prepared statement.
041: */
042: class PreparedStatementCacheItem implements CacheListener {
043: private final static Logger log = Log
044: .open(PreparedStatementCacheItem.class);
045:
046: private PreparedStatementKey _key;
047: private SoftReference<PreparedStatement> _pStmtRef;
048: private ManagedConnectionImpl _mConn;
049:
050: private boolean _isActive;
051: private boolean _isRemoved;
052:
053: PreparedStatementCacheItem(PreparedStatementKey key,
054: PreparedStatement pStmt, ManagedConnectionImpl mConn) {
055: if (pStmt == null)
056: throw new NullPointerException();
057:
058: _key = key;
059: _pStmtRef = new SoftReference<PreparedStatement>(pStmt);
060: _mConn = mConn;
061: }
062:
063: /**
064: * Activates the cache item.
065: */
066: UserPreparedStatement toActive(UserConnection conn) {
067: SoftReference<PreparedStatement> ref = _pStmtRef;
068:
069: if (ref == null)
070: return null;
071:
072: PreparedStatement pStmt = ref.get();
073:
074: if (pStmt == null) {
075: _mConn.remove(_key);
076: return null;
077: }
078:
079: synchronized (this ) {
080: if (_isActive)
081: return null;
082: _isActive = true;
083: }
084:
085: return new UserPreparedStatement(conn, pStmt, this );
086: }
087:
088: void toIdle() {
089: boolean doClose = false;
090:
091: synchronized (this ) {
092: if (_isRemoved) {
093: _isRemoved = true;
094: doClose = _isActive;
095: }
096: _isActive = false;
097: }
098:
099: if (doClose) {
100: try {
101: PreparedStatement pStmt = _pStmtRef.get();
102: _pStmtRef = null;
103:
104: if (pStmt != null)
105: pStmt.close();
106: } catch (Throwable e) {
107: log.log(Level.FINE, e.toString(), e);
108: }
109: }
110: }
111:
112: /**
113: * Returns true for a removed item.
114: */
115: boolean isRemoved() {
116: return _isRemoved;
117: }
118:
119: /**
120: * Called when removed from the cache.
121: */
122: public void removeEvent() {
123: boolean doClose = false;
124:
125: synchronized (this ) {
126: if (!_isRemoved) {
127: _isRemoved = true;
128: doClose = !_isActive;
129: }
130: }
131:
132: if (doClose) {
133: try {
134: PreparedStatement pStmt = _pStmtRef.get();
135: _pStmtRef = null;
136:
137: if (pStmt != null)
138: pStmt.close();
139: } catch (Throwable e) {
140: log.log(Level.FINE, e.toString(), e);
141: }
142: }
143: }
144:
145: void destroy() {
146: _isRemoved = true;
147:
148: SoftReference<PreparedStatement> ref = _pStmtRef;
149: _pStmtRef = null;
150:
151: if (ref != null) {
152: PreparedStatement pStmt = ref.get();
153:
154: if (pStmt != null) {
155: try {
156: pStmt.close();
157: } catch (Throwable e) {
158: log.log(Level.FINE, e.toString(), e);
159: }
160: }
161: }
162: }
163: }
|