1 21 22 package org.apache.derby.jdbc; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import org.apache.derby.iapi.reference.Property; 26 import org.apache.derby.iapi.error.ExceptionSeverity; 27 import org.apache.derby.iapi.reference.JDBC30Translation; 28 29 30 import org.apache.derby.impl.jdbc.Util; 31 import org.apache.derby.impl.jdbc.EmbedConnection; 32 import org.apache.derby.iapi.jdbc.BrokeredConnection; 33 import org.apache.derby.iapi.jdbc.BrokeredConnectionControl; 34 import org.apache.derby.iapi.jdbc.EngineConnection; 35 import org.apache.derby.impl.jdbc.EmbedPreparedStatement; 36 import org.apache.derby.impl.jdbc.EmbedCallableStatement; 37 38 39 import java.sql.Connection ; 40 import java.sql.SQLException ; 41 import java.sql.Statement ; 42 import java.sql.PreparedStatement ; 43 import java.sql.CallableStatement ; 44 45 import java.util.Vector ; 46 import java.util.Enumeration ; 47 48 49 import javax.sql.DataSource ; 50 import javax.sql.PooledConnection ; 51 import javax.sql.ConnectionEventListener ; 52 import javax.sql.ConnectionEvent ; 53 54 66 class EmbedPooledConnection implements javax.sql.PooledConnection , BrokeredConnectionControl 67 { 68 69 70 private static int idCounter = 0; 71 72 73 private int connectionId; 74 75 76 private String connString; 77 78 private Vector eventListener; 80 EmbedConnection realConnection; 81 int defaultIsolationLevel; 82 private boolean defaultReadOnly; 83 BrokeredConnection currentConnectionHandle; 84 85 final ReferenceableDataSource dataSource; 87 private final String username; 88 private final String password; 89 92 private final boolean requestPassword; 93 94 protected boolean isActive; 95 96 private synchronized int nextId() 97 { 98 return idCounter++; 99 } 100 101 EmbedPooledConnection(ReferenceableDataSource ds, String u, String p, boolean requestPassword) throws SQLException 102 { 103 connectionId = nextId(); 104 105 dataSource = ds; 106 username = u; 107 password = p; 108 this.requestPassword = requestPassword; 109 isActive = true; 110 111 openRealConnection(); 113 114 } 115 116 String getUsername() 117 { 118 if (username == null || username.equals("")) 119 return Property.DEFAULT_USER_NAME; 120 else 121 return username; 122 } 123 124 String getPassword() 125 { 126 if (password == null) 127 return ""; 128 else 129 return password; 130 } 131 132 133 140 public synchronized Connection getConnection() throws SQLException 141 { 142 checkActive(); 143 144 closeCurrentConnectionHandle(); 147 148 149 if (realConnection == null) 153 { 154 openRealConnection(); 156 } 157 else 158 { 159 resetRealConnection(); 160 } 161 162 Connection c = getNewCurrentConnectionHandle(); 165 return c; 166 } 167 168 final void openRealConnection() throws SQLException { 169 Connection rc = dataSource.getConnection(username, password, requestPassword); 171 172 this.realConnection = (EmbedConnection) rc; 173 defaultIsolationLevel = rc.getTransactionIsolation(); 174 defaultReadOnly = rc.isReadOnly(); 175 if (currentConnectionHandle != null) 176 realConnection.setApplicationConnection(currentConnectionHandle); 177 } 178 179 final Connection getNewCurrentConnectionHandle() { 180 Connection applicationConnection = currentConnectionHandle = 181 ((org.apache.derby.jdbc.Driver20) (realConnection.getLocalDriver())).newBrokeredConnection(this); 182 realConnection.setApplicationConnection(applicationConnection); 183 return applicationConnection; 184 185 } 186 187 190 private void closeCurrentConnectionHandle() throws SQLException { 191 if (currentConnectionHandle != null) 192 { 193 Vector tmpEventListener = eventListener; 194 eventListener = null; 195 196 try { 197 currentConnectionHandle.close(); 198 } finally { 199 eventListener = tmpEventListener; 200 } 201 202 currentConnectionHandle = null; 203 } 204 } 205 206 void resetRealConnection() throws SQLException { 207 208 realConnection.rollback(); 211 212 realConnection.clearWarnings(); 214 215 if (realConnection.getTransactionIsolation() != defaultIsolationLevel) { 217 218 realConnection.setTransactionIsolation(defaultIsolationLevel); 219 } 220 221 if (!realConnection.getAutoCommit()) 222 realConnection.setAutoCommit(true); 223 224 if (realConnection.isReadOnly() != defaultReadOnly) 225 realConnection.setReadOnly(defaultReadOnly); 226 227 if (realConnection.getHoldability() != JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) 228 realConnection.setHoldability(JDBC30Translation.HOLD_CURSORS_OVER_COMMIT); 229 230 realConnection.resetFromPool(); 232 } 233 234 239 public synchronized void close() throws SQLException 240 { 241 if (!isActive) 242 return; 243 244 closeCurrentConnectionHandle(); 245 try { 246 if (realConnection != null) { 247 if (!realConnection.isClosed()) 248 realConnection.close(); 249 } 250 251 } finally { 252 253 realConnection = null; isActive = false; 255 eventListener = null; 256 } 257 } 258 259 262 public final synchronized void addConnectionEventListener(ConnectionEventListener listener) 263 { 264 if (!isActive) 265 return; 266 if (listener == null) 267 return; 268 if (eventListener == null) 269 eventListener = new Vector (); 270 eventListener.addElement(listener); 271 } 272 273 276 public final synchronized void removeConnectionEventListener(ConnectionEventListener listener) 277 { 278 if (listener == null) 279 return; 280 if (eventListener != null) 281 eventListener.removeElement(listener); 282 } 283 284 287 288 public synchronized EngineConnection getRealConnection() 291 throws SQLException 292 { 293 checkActive(); 294 295 return realConnection; 296 } 297 298 299 public synchronized void notifyError(SQLException exception) 303 { 304 if (exception.getErrorCode() < ExceptionSeverity.SESSION_SEVERITY) 306 return; 307 308 if (eventListener != null && eventListener.size() > 0) 310 { 311 ConnectionEvent errorEvent = new ConnectionEvent (this, exception); 312 313 for (Enumeration e = eventListener.elements(); 314 e.hasMoreElements(); ) 315 { 316 ConnectionEventListener l = 317 (ConnectionEventListener )e.nextElement(); 318 l.connectionErrorOccurred(errorEvent); 319 } 320 } 321 } 322 323 public synchronized void notifyClose() 325 { 326 if (eventListener != null && eventListener.size() > 0) 328 { 329 ConnectionEvent closeEvent = new ConnectionEvent (this); 330 331 for (Enumeration e = eventListener.elements(); 332 e.hasMoreElements(); ) 333 { 334 ConnectionEventListener l = 335 (ConnectionEventListener )e.nextElement(); 336 l.connectionClosed(closeEvent); 337 } 338 } 339 } 340 341 final void checkActive() throws SQLException { 342 if (!isActive) 343 throw Util.noCurrentConnection(); 344 } 345 346 347 350 351 354 public boolean isIsolationLevelSetUsingSQLorJDBC() throws SQLException { 355 if (realConnection != null) 356 return realConnection.getLanguageConnection().isIsolationLevelSetUsingSQLorJDBC(); 357 else 358 return false; 359 } 360 361 367 public void resetIsolationLevelFlag() throws SQLException { 368 realConnection.getLanguageConnection().resetIsolationLevelFlagUsedForSQLandJDBC(); 369 } 370 371 372 376 public void notifyException(SQLException sqle) { 377 this.notifyError(sqle); 378 } 379 380 381 384 public void checkAutoCommit(boolean autoCommit) throws SQLException { 385 } 386 387 390 public int checkHoldCursors(int holdability, boolean downgrade) 391 throws SQLException 392 { 393 return holdability; 394 } 395 396 399 public void checkSavepoint() throws SQLException { 400 } 401 402 405 public void checkRollback() throws SQLException { 406 } 407 408 411 public void checkCommit() throws SQLException { 412 } 413 414 421 public boolean closingConnection() throws SQLException { 422 notifyClose(); 423 currentConnectionHandle = null; 424 return false; 425 } 426 427 430 public Statement wrapStatement(Statement s) throws SQLException { 431 return s; 432 } 433 447 public PreparedStatement wrapStatement(PreparedStatement ps, String sql, Object generatedKeys) throws SQLException { 448 451 EmbedPreparedStatement ps_ = (EmbedPreparedStatement)ps; 452 ps_.setBrokeredConnectionControl(this); 453 return (PreparedStatement)ps_; 454 } 455 456 469 public CallableStatement wrapStatement(CallableStatement cs, String sql) throws SQLException { 470 EmbedCallableStatement cs_ = (EmbedCallableStatement)cs; 471 cs_.setBrokeredConnectionControl(this); 472 return (CallableStatement)cs_; 473 } 474 475 488 public String toString() 489 { 490 if ( connString == null ) 491 { 492 String physicalConnString = isActive ? 493 realConnection.toString() : "<none>"; 494 495 connString = 496 this.getClass().getName() + "@" + this.hashCode() + " " + 497 "(ID = " + connectionId + "), " + 498 "Physical Connection = " + physicalConnString; 499 } 500 501 return connString; 502 } 503 504 505 513 514 519 public void onStatementClose(PreparedStatement statement) { 520 521 } 522 523 529 public void onStatementErrorOccurred(PreparedStatement statement, 530 SQLException sqle) { 531 532 } 533 } 534 | Popular Tags |