1 19 package craftsman.spy; 20 21 import java.sql.CallableStatement ; 22 import java.sql.Connection ; 23 import java.sql.DatabaseMetaData ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.SQLWarning ; 28 import java.sql.Savepoint ; 29 import java.sql.Statement ; 30 import java.util.Map ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 40 public class SpyConnection implements Connection { 41 44 protected Log log = LogFactory.getLog(this.getClass().getName()); 45 46 47 50 private Connection real = null; 51 52 53 56 private int id = 0; 57 58 59 65 protected SpyConnection ( Connection con, String conIdSql) { 66 real = con; 67 id = hashCode(); 68 69 if ( conIdSql!=null) { 70 try { 71 Statement s = con.createStatement(); 72 ResultSet r = s.executeQuery(conIdSql); 73 if ( r.next()) { 74 id = r.getInt(1); 75 } 76 r.close(); 77 s.close(); 78 } catch ( SQLException e) { 79 e.printStackTrace(); 80 } 81 } 82 } 83 84 85 90 protected int getId() { 91 return id; 92 } 93 94 95 98 public int getHoldability() throws SQLException { 99 return real.getHoldability(); 100 } 101 102 103 106 public int getTransactionIsolation() throws SQLException { 107 return real.getTransactionIsolation(); 108 } 109 110 111 114 public void clearWarnings() throws SQLException { 115 real.clearWarnings(); 116 } 117 118 119 122 public void close() throws SQLException { 123 real.close(); 124 } 125 126 127 130 public void commit() throws SQLException { 131 long end, start = System.currentTimeMillis(); 132 133 134 try { 135 real.commit(); 136 end = System.currentTimeMillis(); 137 if ( log.isInfoEnabled()) log.info(getId()+":commit succeed ("+(end-start)+" ms)"); 138 } catch ( SQLException e) { 139 end = System.currentTimeMillis(); 140 if ( log.isErrorEnabled()) log.error(getId()+":commit failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 141 throw e; 142 } 143 } 144 145 146 149 public void rollback() throws SQLException { 150 long end, start = System.currentTimeMillis(); 151 152 153 try { 154 real.rollback(); 155 end = System.currentTimeMillis(); 156 if ( log.isInfoEnabled()) log.info(getId()+":rollback succeed ("+(end-start)+" ms)"); 157 } catch ( SQLException e) { 158 end = System.currentTimeMillis(); 159 if ( log.isErrorEnabled()) log.error(getId()+":rollback failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 160 throw e; 161 } 162 } 163 164 165 168 public boolean getAutoCommit() throws SQLException { 169 return real.getAutoCommit(); 170 } 171 172 173 176 public boolean isClosed() throws SQLException { 177 return real.isClosed(); 178 } 179 180 181 184 public boolean isReadOnly() throws SQLException { 185 return real.isReadOnly(); 186 } 187 188 189 192 public void setHoldability(int holdability) throws SQLException { 193 real.setHoldability(holdability); 194 } 195 196 197 200 public void setTransactionIsolation(int level) throws SQLException { 201 real.setTransactionIsolation(level); 202 } 203 204 205 208 public void setAutoCommit(boolean autoCommit) throws SQLException { 209 long end, start = System.currentTimeMillis(); 210 211 212 try { 213 real.setAutoCommit(autoCommit); 214 end = System.currentTimeMillis(); 215 if ( log.isInfoEnabled()) log.info(getId()+":autocommit("+autoCommit+") succeed ("+(end-start)+" ms)"); 216 } catch ( SQLException e) { 217 end = System.currentTimeMillis(); 218 if ( log.isErrorEnabled()) log.error(getId()+":autocommit("+autoCommit+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 219 throw e; 220 } 221 } 222 223 224 227 public void setReadOnly(boolean readOnly) throws SQLException { 228 real.setReadOnly(readOnly); 229 } 230 231 232 235 public String getCatalog() throws SQLException { 236 return real.getCatalog(); 237 } 238 239 240 243 public void setCatalog(String catalog) throws SQLException { 244 real.setCatalog(catalog); 245 } 246 247 248 251 public DatabaseMetaData getMetaData() throws SQLException { 252 return real.getMetaData(); 253 } 254 255 256 259 public SQLWarning getWarnings() throws SQLException { 260 SQLWarning current, sw = real.getWarnings(); 261 262 263 if ( (current = sw)!=null) { 264 do { 265 if ( log.isInfoEnabled()) log.info(getId()+":sql warning state="+current.getSQLState()+",code="+current.getErrorCode()+",message="+current.getMessage()); 266 } while ( (current = current.getNextWarning())!=null); 267 } 268 269 return sw; 270 } 271 272 273 276 public Savepoint setSavepoint() throws SQLException { 277 long end, start = System.currentTimeMillis(); 278 Savepoint savepoint = null; 279 280 281 try { 282 savepoint = real.setSavepoint(); 283 end = System.currentTimeMillis(); 284 if ( log.isInfoEnabled()) log.info(getId()+":savepoint("+savepoint.getSavepointName()+") succeed ("+(end-start)+" ms)"); 285 } catch ( SQLException e) { 286 end = System.currentTimeMillis(); 287 if ( log.isErrorEnabled()) log.error(getId()+":savepoint("+"unnamed"+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 288 throw e; 289 } 290 291 return savepoint; 292 } 293 294 295 298 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 299 long end, start = System.currentTimeMillis(); 300 301 302 try { 303 real.releaseSavepoint(savepoint); 304 end = System.currentTimeMillis(); 305 if ( log.isInfoEnabled()) log.info(getId()+":savepoint("+savepoint.getSavepointName()+") succeed ("+(end-start)+" ms)"); 306 } catch ( SQLException e) { 307 end = System.currentTimeMillis(); 308 if ( log.isErrorEnabled()) log.error(getId()+":release savepoint("+savepoint.getSavepointName()+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 309 throw e; 310 } 311 } 312 313 314 317 public void rollback(Savepoint savepoint) throws SQLException { 318 real.rollback(savepoint); 319 } 320 321 322 325 public Statement createStatement() throws SQLException { 326 return new SpyStatement ( this, real.createStatement()); 327 } 328 329 330 333 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 334 return new SpyStatement ( this, real.createStatement(resultSetType,resultSetConcurrency)); 335 } 336 337 338 341 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 342 return new SpyStatement ( this, real.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability)); 343 } 344 345 346 349 public Map getTypeMap() throws SQLException { 350 return real.getTypeMap(); 351 } 352 353 354 357 public void setTypeMap(Map map) throws SQLException { 358 real.setTypeMap(map); 359 } 360 361 362 365 public String nativeSQL(String sql) throws SQLException { 366 return real.nativeSQL(sql); 367 } 368 369 370 373 public CallableStatement prepareCall(String sql) throws SQLException { 374 return new SpyCallableStatement ( this, real.prepareCall(sql), sql); 375 } 376 377 378 381 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 382 return new SpyCallableStatement ( this, real.prepareCall(sql,resultSetType,resultSetConcurrency), sql); 383 } 384 385 386 389 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 390 return new SpyCallableStatement ( this, real.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability), sql); 391 } 392 393 394 397 public PreparedStatement prepareStatement(String sql) throws SQLException { 398 return new SpyPreparedStatement ( this, real.prepareStatement(sql), sql); 399 } 400 401 402 405 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 406 return new SpyPreparedStatement ( this, real.prepareStatement(sql,autoGeneratedKeys), sql); 407 } 408 409 410 413 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 414 return new SpyPreparedStatement ( this, real.prepareStatement(sql,resultSetType,resultSetConcurrency), sql); 415 } 416 417 418 421 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 422 return new SpyPreparedStatement ( this, real.prepareStatement(sql,resultSetType,resultSetConcurrency,resultSetHoldability), sql); 423 } 424 425 426 429 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 430 return new SpyPreparedStatement ( this, real.prepareStatement(sql,columnIndexes), sql); 431 } 432 433 434 437 public Savepoint setSavepoint(String name) throws SQLException { 438 long end ,start = System.currentTimeMillis(); 439 Savepoint savepoint = null; 440 441 442 try { 443 savepoint = real.setSavepoint(name); 444 end = System.currentTimeMillis(); 445 if ( log.isInfoEnabled()) log.info(getId()+":savepoint("+name+") succeed ("+(end-start)+" ms)"); 446 } catch ( SQLException e) { 447 end = System.currentTimeMillis(); 448 if ( log.isErrorEnabled()) log.error(getId()+":savepoint("+name+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e); 449 throw e; 450 } 451 452 return savepoint; 453 } 454 455 456 459 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException { 460 return new SpyPreparedStatement ( this, real.prepareStatement(sql,columnNames), sql); 461 } 462 } 463 | Popular Tags |