1 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 ; 35 import java.sql.PreparedStatement ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 39 42 class PreparedStatementCacheItem implements CacheListener { 43 private final static Logger log = Log.open(PreparedStatementCacheItem.class); 44 45 private PreparedStatementKey _key; 46 private SoftReference <PreparedStatement > _pStmtRef; 47 private ManagedConnectionImpl _mConn; 48 49 private boolean _isActive; 50 private boolean _isRemoved; 51 52 PreparedStatementCacheItem(PreparedStatementKey key, 53 PreparedStatement pStmt, 54 ManagedConnectionImpl mConn) 55 { 56 if (pStmt == null) 57 throw new NullPointerException (); 58 59 _key = key; 60 _pStmtRef = new SoftReference <PreparedStatement >(pStmt); 61 _mConn = mConn; 62 } 63 64 67 UserPreparedStatement toActive(UserConnection conn) 68 { 69 SoftReference <PreparedStatement > ref = _pStmtRef; 70 71 if (ref == null) 72 return null; 73 74 PreparedStatement 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 pStmt = _pStmtRef.get(); 105 _pStmtRef = null; 106 107 if (pStmt != null) 108 pStmt.close(); 109 } catch (Throwable e) { 110 log.log(Level.FINE, e.toString(), e); 111 } 112 } 113 } 114 115 118 boolean isRemoved() 119 { 120 return _isRemoved; 121 } 122 123 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 pStmt = _pStmtRef.get(); 140 _pStmtRef = null; 141 142 if (pStmt != null) 143 pStmt.close(); 144 } catch (Throwable e) { 145 log.log(Level.FINE, e.toString(), e); 146 } 147 } 148 } 149 150 void destroy() 151 { 152 _isRemoved = true; 153 154 SoftReference <PreparedStatement > ref = _pStmtRef; 155 _pStmtRef = null; 156 157 if (ref != null) { 158 PreparedStatement pStmt = ref.get(); 159 160 if (pStmt != null) { 161 try { 162 pStmt.close(); 163 } catch (Throwable e) { 164 log.log(Level.FINE, e.toString(), e); 165 } 166 } 167 } 168 } 169 } 170 | Popular Tags |