1 21 22 package org.apache.derby.iapi.jdbc; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.PreparedStatement ; 27 import java.sql.CallableStatement ; 28 import java.sql.DatabaseMetaData ; 29 import java.sql.SQLException ; 30 import java.sql.SQLWarning ; 31 32 import org.apache.derby.impl.jdbc.EmbedSQLWarning; 33 import org.apache.derby.impl.jdbc.Util; 34 35 import java.io.ObjectOutput ; 36 import java.io.ObjectInput ; 37 38 import java.lang.reflect.*; 39 40 import org.apache.derby.iapi.reference.JDBC30Translation; 41 import org.apache.derby.iapi.error.PublicAPI; 42 import org.apache.derby.iapi.error.StandardException; 43 import org.apache.derby.shared.common.reference.SQLState; 44 45 49 public class BrokeredConnection implements EngineConnection 50 { 51 52 int stateHoldability = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; 54 55 final BrokeredConnectionControl control; 56 private boolean isClosed; 57 private String connString; 58 59 63 private int stateIsolationLevel; 64 private boolean stateReadOnly; 65 private boolean stateAutoCommit; 66 67 73 public BrokeredConnection(BrokeredConnectionControl control) 74 { 75 this.control = control; 76 } 77 78 public final void setAutoCommit(boolean autoCommit) throws SQLException 79 { 80 try { 81 control.checkAutoCommit(autoCommit); 82 83 getRealConnection().setAutoCommit(autoCommit); 84 85 stateAutoCommit = autoCommit; 86 } catch (SQLException sqle) { 87 notifyException(sqle); 88 throw sqle; 89 } 90 } 91 public final boolean getAutoCommit() throws SQLException 92 { 93 try { 94 return getRealConnection().getAutoCommit(); 95 } catch (SQLException sqle) { 96 notifyException(sqle); 97 throw sqle; 98 } 99 } 100 public final Statement createStatement() throws SQLException 101 { 102 try { 103 return control.wrapStatement(getRealConnection().createStatement()); 104 } catch (SQLException sqle) { 105 notifyException(sqle); 106 throw sqle; 107 } 108 } 109 110 public final PreparedStatement prepareStatement(String sql) 111 throws SQLException 112 { 113 try { 114 return control.wrapStatement(getRealConnection().prepareStatement(sql), sql, null); 115 } catch (SQLException sqle) { 116 notifyException(sqle); 117 throw sqle; 118 } 119 } 120 121 public final CallableStatement prepareCall(String sql) throws SQLException 122 { 123 try { 124 return control.wrapStatement(getRealConnection().prepareCall(sql), sql); 125 } catch (SQLException sqle) { 126 notifyException(sqle); 127 throw sqle; 128 } 129 } 130 131 public final String nativeSQL(String sql) throws SQLException 132 { 133 try { 134 return getRealConnection().nativeSQL(sql); 135 } catch (SQLException sqle) { 136 notifyException(sqle); 137 throw sqle; 138 } 139 } 140 141 public final void commit() throws SQLException 142 { 143 try { 144 control.checkCommit(); 145 getRealConnection().commit(); 146 } catch (SQLException sqle) { 147 notifyException(sqle); 148 throw sqle; 149 } 150 } 151 152 public final void rollback() throws SQLException 153 { 154 try { 155 control.checkRollback(); 156 getRealConnection().rollback(); 157 } catch (SQLException sqle) { 158 notifyException(sqle); 159 throw sqle; 160 } 161 } 162 163 public final void close() throws SQLException 164 { 165 if (isClosed) 166 return; 167 168 try { 169 if (!control.closingConnection()) { 170 isClosed = true; 171 return; 172 } 173 174 isClosed = true; 175 176 177 getRealConnection().close(); 178 } catch (SQLException sqle) { 179 notifyException(sqle); 180 throw sqle; 181 } 182 } 183 184 public final boolean isClosed() throws SQLException 185 { 186 if (isClosed) 187 return true; 188 try { 189 boolean realIsClosed = getRealConnection().isClosed(); 190 if (realIsClosed) { 191 control.closingConnection(); 192 isClosed = true; 193 } 194 return realIsClosed; 195 } catch (SQLException sqle) { 196 notifyException(sqle); 197 throw sqle; 198 } 199 } 200 201 public final SQLWarning getWarnings() throws SQLException 202 { 203 try { 204 return getRealConnection().getWarnings(); 205 } catch (SQLException sqle) { 206 notifyException(sqle); 207 throw sqle; 208 } 209 } 210 211 public final void clearWarnings() throws SQLException 212 { 213 try { 214 getRealConnection().clearWarnings(); 215 } catch (SQLException sqle) { 216 notifyException(sqle); 217 throw sqle; 218 } 219 } 220 221 public final DatabaseMetaData getMetaData() throws SQLException 222 { 223 try { 224 return getRealConnection().getMetaData(); 225 } catch (SQLException sqle) { 226 notifyException(sqle); 227 throw sqle; 228 } 229 } 230 231 public final void setReadOnly(boolean readOnly) throws SQLException 232 { 233 try { 234 getRealConnection().setReadOnly(readOnly); 235 stateReadOnly = readOnly; 236 } catch (SQLException sqle) { 237 notifyException(sqle); 238 throw sqle; 239 } 240 } 241 242 public final boolean isReadOnly() throws SQLException 243 { 244 try { 245 return getRealConnection().isReadOnly(); 246 } catch (SQLException sqle) { 247 notifyException(sqle); 248 throw sqle; 249 } 250 } 251 252 public final void setCatalog(String catalog) throws SQLException 253 { 254 try { 255 getRealConnection().setCatalog(catalog); 256 } catch (SQLException sqle) { 257 notifyException(sqle); 258 throw sqle; 259 } 260 } 261 262 public final String getCatalog() throws SQLException 263 { 264 try { 265 return getRealConnection().getCatalog(); 266 } catch (SQLException sqle) { 267 notifyException(sqle); 268 throw sqle; 269 } 270 } 271 272 public final void setTransactionIsolation(int level) throws SQLException 273 { 274 try { 275 getRealConnection().setTransactionIsolation(level); 276 stateIsolationLevel = level; 277 } catch (SQLException sqle) { 278 notifyException(sqle); 279 throw sqle; 280 } 281 } 282 283 public final int getTransactionIsolation() throws SQLException 284 { 285 try { 286 return getRealConnection().getTransactionIsolation(); 287 } catch (SQLException sqle) { 288 notifyException(sqle); 289 throw sqle; 290 } 291 } 292 293 public final Statement createStatement(int resultSetType, int resultSetConcurrency) 294 throws SQLException 295 { 296 try 297 { 298 return control.wrapStatement(getRealConnection(). 299 createStatement(resultSetType, resultSetConcurrency)); 300 } 301 catch (SQLException se) 302 { 303 notifyException(se); 304 throw se; 305 } 306 } 307 308 309 public final PreparedStatement prepareStatement(String sql, int resultSetType, 310 int resultSetConcurrency) 311 throws SQLException 312 { 313 try 314 { 315 return control.wrapStatement(getRealConnection(). 316 prepareStatement(sql, resultSetType, resultSetConcurrency), sql, null); 317 } 318 catch (SQLException se) 319 { 320 notifyException(se); 321 throw se; 322 } 323 } 324 325 public final CallableStatement prepareCall(String sql, int resultSetType, 326 int resultSetConcurrency) throws SQLException 327 { 328 try 329 { 330 return control.wrapStatement(getRealConnection(). 331 prepareCall(sql, resultSetType, resultSetConcurrency), sql); 332 } 333 catch (SQLException se) 334 { 335 notifyException(se); 336 throw se; 337 } 338 } 339 340 public java.util.Map getTypeMap() throws SQLException 341 { 342 try 343 { 344 return getRealConnection().getTypeMap(); 345 } 346 catch (SQLException se) 347 { 348 notifyException(se); 349 throw se; 350 } 351 } 352 353 public final void setTypeMap(java.util.Map map) throws SQLException 354 { 355 try 356 { 357 getRealConnection().setTypeMap(map); 358 } 359 catch (SQLException se) 360 { 361 notifyException(se); 362 throw se; 363 } 364 } 365 366 372 377 final EngineConnection getRealConnection() throws SQLException { 378 if (isClosed) 379 throw Util.noCurrentConnection(); 380 381 return control.getRealConnection(); 382 } 383 384 final void notifyException(SQLException sqle) { 385 if (!isClosed) 386 control.notifyException(sqle); 387 } 388 389 393 public void syncState() throws SQLException { 394 EngineConnection conn = getRealConnection(); 395 396 stateIsolationLevel = conn.getTransactionIsolation(); 397 stateReadOnly = conn.isReadOnly(); 398 stateAutoCommit = conn.getAutoCommit(); 399 stateHoldability = conn.getHoldability(); 400 } 401 402 408 public void getIsolationUptoDate() throws SQLException { 409 if (control.isIsolationLevelSetUsingSQLorJDBC()) { 410 stateIsolationLevel = getRealConnection().getTransactionIsolation(); 411 control.resetIsolationLevelFlag(); 412 } 413 } 414 424 public void setState(boolean complete) throws SQLException { 425 Class [] CONN_PARAM = { Integer.TYPE }; 426 Object [] CONN_ARG = { new Integer (stateHoldability)}; 427 428 Connection conn = getRealConnection(); 429 430 if (complete) { 431 conn.setTransactionIsolation(stateIsolationLevel); 432 conn.setReadOnly(stateReadOnly); 433 conn.setAutoCommit(stateAutoCommit); 434 try { 440 Method sh = conn.getClass().getMethod("setHoldability", CONN_PARAM); 441 sh.invoke(conn, CONN_ARG); 442 } catch( Exception e) 443 { 444 throw PublicAPI.wrapStandardException( StandardException.plainWrapException( e)); 445 } 446 } 447 } 448 449 public BrokeredStatement newBrokeredStatement(BrokeredStatementControl statementControl) throws SQLException { 450 return new BrokeredStatement(statementControl, getJDBCLevel()); 451 } 452 public BrokeredPreparedStatement newBrokeredStatement(BrokeredStatementControl statementControl, String sql, Object generatedKeys) throws SQLException { 453 return new BrokeredPreparedStatement(statementControl, getJDBCLevel(), sql); 454 } 455 public BrokeredCallableStatement newBrokeredStatement(BrokeredStatementControl statementControl, String sql) throws SQLException { 456 return new BrokeredCallableStatement(statementControl, getJDBCLevel(), sql); 457 } 458 459 465 public final void setDrdaID(String drdaID) 466 { 467 try { 468 getRealConnection().setDrdaID(drdaID); 469 } catch (SQLException sqle) 470 { 471 } 474 } 475 476 484 public final void setPrepareIsolation(int level) throws SQLException 485 { 486 getRealConnection().setPrepareIsolation(level); 487 } 488 489 497 public final int getPrepareIsolation() throws SQLException 498 { 499 return getRealConnection().getPrepareIsolation(); 500 } 501 502 506 public final void addWarning(SQLWarning w) throws SQLException 507 { 508 getRealConnection().addWarning(w); 509 } 510 511 517 protected final void checkIfClosed() throws SQLException { 518 if (isClosed()) { 519 throw Util.noCurrentConnection(); 520 } 521 } 522 523 529 public String toString() 530 { 531 if ( connString == null ) 532 { 533 String wrappedString; 534 try 535 { 536 wrappedString = getRealConnection().toString(); 537 } 538 catch ( SQLException e ) 539 { 540 wrappedString = "<none>"; 541 } 542 543 connString = this.getClass().getName() + "@" + this.hashCode() + 544 ", Wrapped Connection = " + wrappedString; 545 } 546 547 return connString; 548 } 549 550 int getJDBCLevel() { return 2;} 551 552 555 556 559 public final PreparedStatement prepareStatement(String sql, 560 int resultSetType, int resultSetConcurrency, 561 int resultSetHoldability) throws SQLException { 562 try { 563 resultSetHoldability = statementHoldabilityCheck(resultSetHoldability); 564 565 return control.wrapStatement( 566 getRealConnection().prepareStatement(sql, resultSetType, 567 resultSetConcurrency, resultSetHoldability), sql, null); 568 } 569 catch (SQLException se) 570 { 571 notifyException(se); 572 throw se; 573 } 574 } 575 576 580 public final int getHoldability() throws SQLException { 581 try { 582 return getRealConnection().getHoldability(); 583 } 584 catch (SQLException se) 585 { 586 notifyException(se); 587 throw se; 588 } 589 } 590 591 594 595 604 final int statementHoldabilityCheck(int resultSetHoldability) 605 throws SQLException 606 { 607 int holdability = control.checkHoldCursors(resultSetHoldability, true); 608 if (holdability != resultSetHoldability) { 609 SQLWarning w = 610 EmbedSQLWarning.newEmbedSQLWarning(SQLState.HOLDABLE_RESULT_SET_NOT_AVAILABLE); 611 612 addWarning(w); 613 } 614 615 return holdability; 616 617 } 618 } 619 | Popular Tags |