1 22 package org.jboss.resource.adapter.jdbc; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Ref ; 28 import java.sql.Blob ; 29 import java.sql.Clob ; 30 import java.sql.Array ; 31 import java.sql.ResultSetMetaData ; 32 import java.sql.ParameterMetaData ; 33 import java.sql.SQLWarning ; 34 import java.sql.Connection ; 35 import java.math.BigDecimal ; 36 import java.util.Calendar ; 37 38 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean; 39 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 40 41 49 public class CachedPreparedStatement implements PreparedStatement 50 { 51 private PreparedStatement ps; 52 private SynchronizedBoolean cached = new SynchronizedBoolean(true); 53 private SynchronizedInt inUse = new SynchronizedInt(1); 54 55 private int defaultMaxFieldSize; 56 private int defaultMaxRows; 57 private int defaultQueryTimeout; 58 private int defaultFetchDirection; 59 private int defaultFetchSize; 60 private int currentMaxFieldSize; 61 private int currentMaxRows; 62 private int currentQueryTimeout; 63 private int currentFetchDirection; 64 private int currentFetchSize; 65 66 public CachedPreparedStatement(PreparedStatement ps) throws SQLException 67 { 68 this.ps = ps; 69 70 defaultMaxFieldSize = ps.getMaxFieldSize(); 72 defaultMaxRows = ps.getMaxRows(); 73 defaultQueryTimeout = ps.getQueryTimeout(); 74 defaultFetchDirection = ps.getFetchDirection(); 75 defaultFetchSize = ps.getFetchSize(); 76 currentMaxFieldSize = defaultMaxFieldSize; 77 currentMaxRows = defaultMaxRows; 78 currentQueryTimeout = defaultQueryTimeout; 79 currentFetchDirection = defaultFetchDirection; 80 currentFetchSize = defaultFetchSize; 81 } 82 83 public PreparedStatement getUnderlyingPreparedStatement() 84 { 85 return ps; 86 } 87 88 public ResultSet executeQuery() throws SQLException 89 { 90 return ps.executeQuery(); 91 } 92 93 public int executeUpdate() throws SQLException 94 { 95 return ps.executeUpdate(); 96 } 97 98 public void setNull(int parameterIndex, int sqlType) throws SQLException 99 { 100 ps.setNull(parameterIndex, sqlType); 101 } 102 103 public void setBoolean(int parameterIndex, boolean x) throws SQLException 104 { 105 ps.setBoolean(parameterIndex, x); 106 } 107 108 public void setByte(int parameterIndex, byte x) throws SQLException 109 { 110 ps.setByte(parameterIndex, x); 111 } 112 113 public void setShort(int parameterIndex, short x) throws SQLException 114 { 115 ps.setShort(parameterIndex, x); 116 } 117 118 public void setInt(int parameterIndex, int x) throws SQLException 119 { 120 ps.setInt(parameterIndex, x); 121 } 122 123 public void setLong(int parameterIndex, long x) throws SQLException 124 { 125 ps.setLong(parameterIndex, x); 126 } 127 128 public void setFloat(int parameterIndex, float x) throws SQLException 129 { 130 ps.setFloat(parameterIndex, x); 131 } 132 133 public void setDouble(int parameterIndex, double x) throws SQLException 134 { 135 ps.setDouble(parameterIndex, x); 136 } 137 138 public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException 139 { 140 ps.setBigDecimal(parameterIndex, x); 141 } 142 143 public void setString(int parameterIndex, String x) throws SQLException 144 { 145 ps.setString(parameterIndex, x); 146 } 147 148 public void setBytes(int parameterIndex, byte x[]) throws SQLException 149 { 150 ps.setBytes(parameterIndex, x); 151 } 152 153 public void setDate(int parameterIndex, java.sql.Date x) 154 throws SQLException 155 { 156 ps.setDate(parameterIndex, x); 157 } 158 159 public void setTime(int parameterIndex, java.sql.Time x) 160 throws SQLException 161 { 162 ps.setTime(parameterIndex, x); 163 } 164 165 public void setTimestamp(int parameterIndex, java.sql.Timestamp x) 166 throws SQLException 167 { 168 ps.setTimestamp(parameterIndex, x); 169 } 170 171 public void setAsciiStream(int parameterIndex, java.io.InputStream x, int length) 172 throws SQLException 173 { 174 ps.setAsciiStream(parameterIndex, x, length); 175 } 176 177 180 public void setUnicodeStream(int parameterIndex, java.io.InputStream x, 181 int length) throws SQLException 182 { 183 ps.setUnicodeStream(parameterIndex, x, length); 184 } 185 186 public void setBinaryStream(int parameterIndex, java.io.InputStream x, 187 int length) throws SQLException 188 { 189 ps.setBinaryStream(parameterIndex, x, length); 190 } 191 192 public void clearParameters() throws SQLException 193 { 194 ps.clearParameters(); 195 } 196 197 public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) 198 throws SQLException 199 { 200 ps.setObject(parameterIndex, x, targetSqlType, scale); 201 } 202 203 public void setObject(int parameterIndex, Object x, int targetSqlType) 204 throws SQLException 205 { 206 ps.setObject(parameterIndex, x, targetSqlType); 207 } 208 209 public void setObject(int parameterIndex, Object x) throws SQLException 210 { 211 ps.setObject(parameterIndex, x); 212 } 213 214 public boolean execute() throws SQLException 215 { 216 return ps.execute(); 217 } 218 219 public void addBatch() throws SQLException 220 { 221 ps.addBatch(); 222 } 223 224 public void setCharacterStream(int parameterIndex, 225 java.io.Reader reader, 226 int length) throws SQLException 227 { 228 ps.setCharacterStream(parameterIndex, reader, length); 229 } 230 231 public void setRef(int i, Ref x) throws SQLException 232 { 233 ps.setRef(i, x); 234 } 235 236 public void setBlob(int i, Blob x) throws SQLException 237 { 238 ps.setBlob(i, x); 239 } 240 241 public void setClob(int i, Clob x) throws SQLException 242 { 243 ps.setClob(i, x); 244 } 245 246 public void setArray(int i, Array x) throws SQLException 247 { 248 ps.setArray(i, x); 249 } 250 251 public ResultSetMetaData getMetaData() throws SQLException 252 { 253 return ps.getMetaData(); 254 } 255 256 public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) 257 throws SQLException 258 { 259 ps.setDate(parameterIndex, x, cal); 260 } 261 262 public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) 263 throws SQLException 264 { 265 ps.setTime(parameterIndex, x, cal); 266 } 267 268 public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) 269 throws SQLException 270 { 271 ps.setTimestamp(parameterIndex, x, cal); 272 } 273 274 public void setNull(int paramIndex, int sqlType, String typeName) 275 throws SQLException 276 { 277 ps.setNull(paramIndex, sqlType, typeName); 278 } 279 280 public void setURL(int parameterIndex, java.net.URL x) throws SQLException 281 { 282 ps.setURL(parameterIndex, x); 283 } 284 285 public ParameterMetaData getParameterMetaData() throws SQLException 286 { 287 return ps.getParameterMetaData(); 288 } 289 290 public ResultSet executeQuery(String sql) throws SQLException 291 { 292 return ps.executeQuery(sql); 293 } 294 295 public int executeUpdate(String sql) throws SQLException 296 { 297 return ps.executeUpdate(sql); 298 } 299 300 public boolean isInUse() 301 { 302 return inUse.get() > 0; 303 } 304 305 public void inUse() 306 { 307 inUse.increment(); 308 } 309 310 public void agedOut() throws SQLException 311 { 312 cached.set(false); 313 if (inUse.get() == 0) 314 ps.close(); 315 } 316 317 public void close() throws SQLException 318 { 319 inUse.decrement(); 320 if (inUse.get() == 0) 321 { 322 if (cached.get() == false) 323 ps.close(); 324 else 325 { 326 if (defaultMaxFieldSize != currentMaxFieldSize) 328 { 329 ps.setMaxFieldSize(defaultMaxFieldSize); 330 currentMaxFieldSize = defaultMaxFieldSize; 331 } 332 if (defaultMaxRows != currentMaxRows) 333 { 334 ps.setMaxRows(defaultMaxRows); 335 currentMaxRows = defaultMaxRows; 336 } 337 if (defaultQueryTimeout != currentQueryTimeout) 338 { 339 ps.setQueryTimeout(defaultQueryTimeout); 340 currentQueryTimeout = defaultQueryTimeout; 341 } 342 if (defaultFetchDirection != currentFetchDirection) 343 { 344 ps.setFetchDirection(defaultFetchDirection); 345 currentFetchDirection = defaultFetchDirection; 346 } 347 if (defaultFetchSize != currentFetchSize) 348 { 349 ps.setFetchSize(defaultFetchSize); 350 currentFetchSize = defaultFetchSize; 351 } 352 } 353 } 354 } 355 356 public int getMaxFieldSize() throws SQLException 357 { 358 return ps.getMaxFieldSize(); 359 } 360 361 public void setMaxFieldSize(int max) throws SQLException 362 { 363 ps.setMaxFieldSize(max); 364 currentMaxFieldSize = max; 365 } 366 367 public int getMaxRows() throws SQLException 368 { 369 return ps.getMaxRows(); 370 } 371 372 public void setMaxRows(int max) throws SQLException 373 { 374 ps.setMaxRows(max); 375 currentMaxRows = max; 376 } 377 378 public void setEscapeProcessing(boolean enable) throws SQLException 379 { 380 ps.setEscapeProcessing(enable); 381 } 382 383 public int getQueryTimeout() throws SQLException 384 { 385 return ps.getQueryTimeout(); 386 } 387 388 public void setQueryTimeout(int seconds) throws SQLException 389 { 390 ps.setQueryTimeout(seconds); 391 currentQueryTimeout = seconds; 392 } 393 394 public void cancel() throws SQLException 395 { 396 ps.cancel(); 397 } 398 399 public SQLWarning getWarnings() throws SQLException 400 { 401 return ps.getWarnings(); 402 } 403 404 public void clearWarnings() throws SQLException 405 { 406 ps.clearWarnings(); 407 } 408 409 public void setCursorName(String name) throws SQLException 410 { 411 ps.setCursorName(name); 412 } 413 414 public boolean execute(String sql) throws SQLException 415 { 416 return ps.execute(sql); 417 } 418 419 public ResultSet getResultSet() throws SQLException 420 { 421 return ps.getResultSet(); 422 } 423 424 public int getUpdateCount() throws SQLException 425 { 426 return ps.getUpdateCount(); 427 } 428 429 public boolean getMoreResults() throws SQLException 430 { 431 return ps.getMoreResults(); 432 } 433 434 public void setFetchDirection(int direction) throws SQLException 435 { 436 ps.setFetchDirection(direction); 437 currentFetchDirection = direction; 438 } 439 440 public int getFetchDirection() throws SQLException 441 { 442 return ps.getFetchDirection(); 443 } 444 445 public void setFetchSize(int rows) throws SQLException 446 { 447 ps.setFetchSize(rows); 448 currentFetchSize = rows; 449 } 450 451 public int getFetchSize() throws SQLException 452 { 453 return ps.getFetchSize(); 454 } 455 456 public int getResultSetConcurrency() throws SQLException 457 { 458 return ps.getResultSetConcurrency(); 459 } 460 461 public int getResultSetType() throws SQLException 462 { 463 return ps.getResultSetType(); 464 } 465 466 public void addBatch(String sql) throws SQLException 467 { 468 ps.addBatch(sql); 469 } 470 471 public void clearBatch() throws SQLException 472 { 473 ps.clearBatch(); 474 } 475 476 public int[] executeBatch() throws SQLException 477 { 478 return ps.executeBatch(); 479 } 480 481 public Connection getConnection() throws SQLException 482 { 483 return ps.getConnection(); 484 } 485 486 public boolean getMoreResults(int current) throws SQLException 487 { 488 return ps.getMoreResults(current); 489 } 490 491 public ResultSet getGeneratedKeys() throws SQLException 492 { 493 return ps.getGeneratedKeys(); 494 } 495 496 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException 497 { 498 return ps.executeUpdate(sql, autoGeneratedKeys); 499 } 500 501 public int executeUpdate(String sql, int columnIndexes[]) throws SQLException 502 { 503 return ps.executeUpdate(sql, columnIndexes); 504 } 505 506 public int executeUpdate(String sql, String columnNames[]) throws SQLException 507 { 508 return ps.executeUpdate(sql, columnNames); 509 } 510 511 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException 512 { 513 return ps.execute(sql, autoGeneratedKeys); 514 } 515 516 public boolean execute(String sql, int columnIndexes[]) throws SQLException 517 { 518 return ps.execute(sql, columnIndexes); 519 } 520 521 public boolean execute(String sql, String columnNames[]) throws SQLException 522 { 523 return ps.execute(sql, columnNames); 524 } 525 526 public int getResultSetHoldability() throws SQLException 527 { 528 return ps.getResultSetHoldability(); 529 } 530 } 531 | Popular Tags |