1 4 5 package org.smartlib.pool.core; 6 7 import java.sql.*; 8 import java.util.*; 9 10 17 18 public class SmartConnection implements Connection , 19 Close , ConnectionMonitor { 20 21 private Connection conn = null; 22 private long lastAccessedTime = 0; 23 private long connectionObtainedTime = 0; 24 25 private ArrayList references; 26 private boolean autoClose = false; 27 private Debugger debug; 28 29 private boolean isCurrentlyInTransaction; 30 31 private Pool poolReference; 33 34 private boolean isClosed = false; 35 36 private boolean isForcedClosed = false; 37 private long forcedCloseTime = 0; 38 39 private String owner ; 40 41 SmartConnection(Connection conn , Pool poolReference , String owner 43 , boolean autoClose ) { 44 45 if (conn == null) 46 throw new IllegalArgumentException ( 47 "Connection Object is null/closed"); 48 this.conn = conn; 49 this.poolReference = poolReference; 50 this.owner = owner; 51 lastAccessedTime = System.currentTimeMillis(); 52 connectionObtainedTime = lastAccessedTime; 53 this.autoClose = autoClose; 54 if (autoClose) 55 references = new ArrayList(); 56 debug = new Debugger("SmartConnection" , true); 57 58 } 59 60 SmartConnection(Connection conn , Pool poolReference) { 61 62 this(conn , poolReference , "N/A" , false); 63 64 } 65 66 67 68 private void preProcess() throws SQLException { 69 70 if (isForcedClosed) 71 throw new StaleConnectionException("SmartPool had withdrawn the connection since it was idle for more than the specified time. Connection withdrawn at " + new java.util.Date (forcedCloseTime)); 72 if (isClosed) 73 throw new SQLException("Connection already closed"); 74 lastAccessedTime = System.currentTimeMillis(); 75 76 } 77 78 public long getLastAccessedTime() { 79 80 return lastAccessedTime; 81 82 } 83 84 void setLastAccessedTime() { 85 86 lastAccessedTime = System.currentTimeMillis(); 87 88 } 89 90 91 public boolean isCurrentlyInTransaction() { 92 93 return isCurrentlyInTransaction; 94 95 } 96 97 public String getPoolName() { 98 return ((PoolMonitor)poolReference).getName(); 99 } 100 101 public long getConnectionObtainedTime() { 103 104 return connectionObtainedTime; 105 106 } 107 108 public String getOwner() { 109 110 return owner; 111 112 } 113 114 public Statement createStatement() throws SQLException { 115 116 preProcess(); 117 SmartStatement stmt = new SmartStatement(conn.createStatement() , this); 118 if (autoClose) 119 references.add(stmt); 120 return stmt; 121 122 } 123 124 public PreparedStatement prepareStatement(String sql) throws SQLException { 125 126 preProcess(); 127 SmartPreparedStatement pstmt = new SmartPreparedStatement( 128 conn.prepareStatement(sql) , this); 129 if (autoClose) 130 references.add(pstmt); 131 return pstmt; 132 133 } 134 135 public CallableStatement prepareCall(String sql) throws SQLException { 136 137 preProcess(); 138 SmartCallableStatement cstmt = new SmartCallableStatement( 139 conn.prepareCall(sql) , this ); 140 if (autoClose) 141 references.add(cstmt); 142 return cstmt; 143 144 } 145 146 public String nativeSQL(String sql) throws SQLException { 147 148 preProcess(); 149 return conn.nativeSQL(sql); 150 151 } 152 153 public void setAutoCommit(boolean autoCommit) throws SQLException { 154 155 preProcess(); 156 isCurrentlyInTransaction = autoCommit; 157 conn.setAutoCommit(autoCommit); 158 159 } 160 161 public boolean getAutoCommit() throws SQLException { 162 163 preProcess(); 164 return conn.getAutoCommit(); 165 166 } 167 168 public void commit() throws SQLException { 169 170 preProcess(); 171 conn.commit(); 172 173 } 174 175 public void rollback() throws SQLException { 176 177 preProcess(); 178 conn.rollback(); 179 180 } 181 182 188 void forcedClose() { 189 190 try { 191 synchronized (this) { 194 forcedCloseTime = System.currentTimeMillis(); 195 debug.print("Forcefully closing connection for owner" + owner); 196 close(); 197 isForcedClosed = true; 198 } 199 } 200 catch (SQLException sqe) { 201 debug.writeException(sqe); 202 } 203 204 } 205 206 public void close() throws SQLException { 207 208 preProcess(); 209 if (autoClose) 211 closeAll(); 212 if (!conn.getAutoCommit() && ((ConnectionPool)poolReference). 214 getConfigMonitor().getConnectionLoaderClass() != null ) { 215 conn.rollback(); 216 conn.setAutoCommit(true); 217 } 218 debug.print("Connection Released for:" + getOwner()); 219 poolReference.returnConnection(this); 220 isClosed = true; 221 222 } 223 224 225 226 227 private void closeAll() throws SQLException { 228 229 for (int i=0 ; i<references.size() ; i++ ) { 230 Close close = (Close)references.get(i); 231 debug.print("Closing " + close); 232 if (!close.isClosed()) 233 close.close(); 234 } 235 236 } 237 238 public boolean isClosed() throws SQLException { 239 240 return isClosed; 241 242 } 243 244 public DatabaseMetaData getMetaData() throws SQLException { 245 246 preProcess(); 247 return conn.getMetaData(); 248 249 } 250 251 public void setReadOnly(boolean readOnly) throws SQLException { 252 253 preProcess(); 254 conn.setReadOnly(readOnly); 255 256 } 257 258 public boolean isReadOnly() throws SQLException { 259 260 preProcess(); 261 return conn.isReadOnly(); 262 263 } 264 265 public void setCatalog(String catalog) throws SQLException { 266 267 preProcess(); 268 conn.setCatalog(catalog); 269 270 } 271 272 public String getCatalog() throws SQLException { 273 274 preProcess(); 275 return conn.getCatalog(); 276 277 } 278 279 280 public void setTransactionIsolation(int level) throws SQLException { 281 282 preProcess(); 283 conn.setTransactionIsolation(level); 284 285 } 286 287 public int getTransactionIsolation() throws SQLException { 288 289 preProcess(); 290 return conn.getTransactionIsolation(); 291 292 } 293 294 public SQLWarning getWarnings() throws SQLException { 295 296 preProcess(); 297 return conn.getWarnings(); 298 299 } 300 301 public void clearWarnings() throws SQLException { 302 303 preProcess(); 304 conn.clearWarnings(); 305 306 } 307 308 309 public Statement createStatement(int resultSetType, 310 int resultSetConcurrency) throws SQLException { 311 312 preProcess(); 313 SmartStatement stmt = new SmartStatement( 314 conn.createStatement(resultSetType , resultSetConcurrency) 315 ,this); 316 if (autoClose) 317 references.add(stmt); 318 return stmt; 319 320 } 321 322 public PreparedStatement prepareStatement(String sql, int resultSetType, 323 int resultSetConcurrency) throws SQLException { 324 325 preProcess(); 326 SmartPreparedStatement pstmt = new SmartPreparedStatement( 327 conn.prepareStatement(sql,resultSetType,resultSetConcurrency) , this); 328 if (autoClose) 329 references.add(pstmt); 330 return pstmt; 331 332 } 333 334 public CallableStatement prepareCall(String sql, int resultSetType, 335 int resultSetConcurrency) throws SQLException { 336 337 preProcess(); 338 SmartCallableStatement cstmt = new SmartCallableStatement( 339 conn.prepareCall(sql , resultSetType, 340 resultSetConcurrency) , this ); 341 if (autoClose) 342 references.add(cstmt); 343 return cstmt; 344 345 } 346 347 public java.util.Map getTypeMap() throws SQLException { 348 349 preProcess(); 350 return conn.getTypeMap(); 351 352 } 353 354 public void setTypeMap(Map<String , Class <?>> map) throws SQLException { 355 } 357 358 public void setHoldability(int holdability) throws SQLException { 359 } 361 362 public int getHoldability() throws SQLException { 363 return 0; } 365 366 public Savepoint setSavepoint() throws SQLException { 367 return null; } 369 370 public Savepoint setSavepoint(String name) throws SQLException { 371 return null; } 373 374 public void rollback(Savepoint savepoint) throws SQLException { 375 } 377 378 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 379 } 381 382 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 383 return null; } 385 386 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 387 return null; } 389 390 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 391 return null; } 393 394 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 395 return null; } 397 398 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException { 399 return null; } 401 402 public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException { 403 return null; } 405 406 407 Connection returnConnection() { 409 410 return conn; 411 412 } 413 414 public String toString() { 415 416 return conn.toString(); 417 418 } 419 420 } 421 | Popular Tags |