1 21 22 package uk.org.primrose.pool.core; 23 import java.sql.*; 24 25 public class PoolConnection implements Connection,java.io.Serializable { 26 Connection conn = null; 27 private PoolSqlMonitor psm = new PoolSqlMonitor(); 28 Pool pool; 29 30 boolean connectionCalled = false; 32 33 public PoolConnection(Pool pool) { 34 super(); 35 this.pool = pool; 36 this.psm.reset(); 37 } 38 39 public PoolConnection getConnection(String driverClass, String driverURL, String user, String password) { 40 conn = new NewConnection().get(driverClass, driverURL, user, password); 41 try { 42 if (conn == null || conn.isClosed()) { 43 return null; 44 } 45 } catch (SQLException sqle) { return null; } 46 return this; 47 } 48 49 public void setConnectionCalled(boolean connectionCalled) { 50 this.connectionCalled = connectionCalled; 51 this.psm.setCaller(5); 53 this.psm.setSql("TRANSITIONAL_CONNECTION_OPENED"); 54 pool.log("[PoolConnection] " +this + " Retrieving connection , CALLER is : " +this.psm.getCaller() , false); 55 56 } 57 58 public boolean getConnectionCalled() { 59 return this.connectionCalled; 60 } 61 62 65 public long getTimeInUse() { 66 return this.psm.getTimeInUse(); 67 } 68 69 72 public int getNumberOfCalls() { 73 return this.psm.getNumberOfCalls(); 74 } 75 76 79 public long getIdleTime() { 80 return this.psm.getIdleTime(); 81 } 82 83 86 public String getSql() { 87 return this.psm.getSql(); 88 } 89 90 93 public String getCaller() { 94 String caller = this.psm.getCaller(); 95 return caller; 96 97 } 98 99 106 public Connection getRealConnection() { 107 return this.conn; 108 } 109 110 public void close() { 111 close(true); 112 } 113 114 121 protected void close(boolean cleanAndDump) { 122 124 pool.log("[PoolConnection] close() called on " +this, false); 125 pool.log("[PoolConnection] " +this + " SQL was : " +this.psm.getSql() +", CALLER was : " +this.psm.getCaller() , false); 126 127 if (this.getConnectionCalled() == false) { 132 pool.log("[PoolConnection] " +this + " WARNING ! You have called close() on a connection twice ! This could cause problems !, CALLER (" +this.psm.getCaller() +") ", true); 133 return; 134 } 135 136 if (psm.getResultSet() != null) { 137 pool.log("[PoolConnection] " +this + " isResultSetClosed(" +psm.isResultSetClosed() +")", false); 138 if (!psm.isResultSetClosed()) { 139 pool.log("[PoolConnection] " +this + " Trying to close unclosed ResultSet SQL(" +this.psm.getSql() +"), CALLER (" +this.psm.getCaller() +") ...", false); 140 try { 141 psm.getResultSet().close(); 142 } catch (SQLException sqle) { 143 sqle.printStackTrace(pool.getLog()); 144 if (cleanAndDump) { 147 pool.dump(this, false); 148 } 149 } 150 } 151 } 152 153 if (psm.getStatement() != null) { 154 pool.log("[PoolConnection] " +this + " isStatementClosed(" +psm.isStatementClosed() +")", false); 155 if (!psm.isStatementClosed()) { 156 pool.log("[PoolConnection] " +this + " Trying to close unclosed " +psm.getStatement().getClass().getName() +" SQL(" +this.psm.getSql() +"), CALLER (" +this.psm.getCaller() +") ...", false); 157 try { 158 psm.getStatement().close(); 159 } catch (SQLException sqle) { 160 sqle.printStackTrace(pool.getLog()); 161 162 if (cleanAndDump) { 165 pool.dump(this, false); 166 } 167 } 168 } 169 } 170 171 if (cleanAndDump) { 172 this.connectionCalled = false; 173 this.psm.reset(); 174 pool.put(this); 175 } 176 } 178 179 public Statement createStatement() throws SQLException { 180 PoolStatement ps = new PoolStatement(conn.createStatement(), psm); 181 return ps; 182 183 } 184 185 public PreparedStatement prepareStatement(String sql) throws SQLException { 186 PoolPreparedStatement pps = new PoolPreparedStatement(conn.prepareStatement(sql), psm); 187 this.psm.setSql(sql); 188 return pps; 189 } 190 191 public CallableStatement prepareCall(String sql) throws SQLException { 192 PoolCallableStatement pcs = new PoolCallableStatement(conn.prepareCall(sql), psm); 193 this.psm.setSql(sql); 194 return pcs; 195 } 196 197 public String nativeSQL(String sql) throws SQLException { 198 this.psm.setSql(sql); 199 return conn.nativeSQL(sql); 200 } 201 202 public void setAutoCommit(boolean autoCommit) throws SQLException { 203 conn.setAutoCommit(autoCommit); 204 } 205 206 public boolean getAutoCommit() throws SQLException{ 207 return conn.getAutoCommit(); 208 } 209 210 public void commit() throws SQLException{ 211 conn.commit(); 212 } 213 214 public void rollback() throws SQLException{ 215 conn.rollback(); 216 } 217 218 public boolean isClosed() { 219 if (conn == null) return false; 220 try { 221 return conn.isClosed(); 222 } catch (SQLException sqle) { 223 sqle.printStackTrace(System.err); 224 return true; 225 } 226 } 227 228 229 public DatabaseMetaData getMetaData() throws SQLException{ 230 return conn.getMetaData(); 231 } 232 233 public void setReadOnly(boolean readOnly) throws SQLException{ 234 conn.setReadOnly(readOnly); 235 } 236 237 public boolean isReadOnly() throws SQLException{ 238 return conn.isReadOnly(); 239 } 240 241 public void setCatalog(String catalog) throws SQLException{ 242 conn.setCatalog(catalog); 243 } 244 245 246 public String getCatalog() throws SQLException{ 247 return conn.getCatalog(); 248 } 249 250 public void setTransactionIsolation(int level) throws SQLException{ 251 conn.setTransactionIsolation(level); 252 } 253 254 public int getTransactionIsolation() throws SQLException{ 255 return conn.getTransactionIsolation(); 256 } 257 258 public SQLWarning getWarnings() throws SQLException{ 259 return conn.getWarnings(); 260 } 261 262 public void clearWarnings() throws SQLException{ 263 conn.clearWarnings(); 264 } 265 266 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException{ 267 return conn.createStatement(resultSetType, resultSetConcurrency); 268 } 269 270 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException{ 271 return conn.prepareStatement(sql, resultSetType, resultSetConcurrency); 272 } 273 274 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException{ 275 return conn.prepareCall(sql, resultSetType, resultSetConcurrency); 276 } 277 278 public java.util.Map getTypeMap() throws SQLException{ 279 return conn.getTypeMap(); 280 } 281 282 public void setTypeMap(java.util.Map map) throws SQLException{ 283 conn.setTypeMap(map); 284 } 285 286 public void setHoldability(int holdability) throws SQLException{ 287 conn.setHoldability(holdability); 288 } 289 290 public int getHoldability() throws SQLException{ 291 return conn.getHoldability(); 292 } 293 294 public Savepoint setSavepoint() throws SQLException{ 295 return conn.setSavepoint(); 296 } 297 298 public Savepoint setSavepoint(String name) throws SQLException{ 299 return conn.setSavepoint(name); 300 } 301 302 public void rollback(Savepoint savepoint) throws SQLException{ 303 conn.rollback(savepoint); 304 } 305 306 307 public void releaseSavepoint(Savepoint savepoint) throws SQLException{ 308 conn.releaseSavepoint(savepoint); 309 } 310 311 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{ 312 return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); 313 } 314 315 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{ 316 return conn.prepareStatement(sql, resultSetType, resultSetConcurrency,resultSetHoldability); 317 } 318 319 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{ 320 return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 321 } 322 323 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException{ 324 return conn.prepareStatement(sql, autoGeneratedKeys); 325 } 326 327 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException{ 328 return conn.prepareStatement(sql, columnIndexes); 329 } 330 331 public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException{ 332 return conn.prepareStatement(sql, columnNames); 333 } 334 } 335 | Popular Tags |