1 21 package oracle.toplink.essentials.internal.databaseaccess; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.sessions.Login; 27 import oracle.toplink.essentials.queryframework.Call; 28 import oracle.toplink.essentials.logging.SessionLog; 29 import oracle.toplink.essentials.sessions.SessionProfiler; 30 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 33 48 public abstract class DatasourceAccessor implements Accessor { 49 50 51 protected Object datasourceConnection; 52 53 54 protected Login login; 55 56 60 protected int callCount; 61 62 63 protected boolean isInTransaction; 64 65 66 protected boolean isConnected; 67 68 69 70 protected DatasourcePlatform platform; 71 72 75 public DatasourceAccessor() { 76 this.isInTransaction = false; 77 this.callCount = 0; 78 this.isConnected = false; 79 } 80 81 84 public Object clone() { 85 try { 86 DatasourceAccessor accessor = (DatasourceAccessor)super.clone(); 87 return accessor; 88 } catch (CloneNotSupportedException exception) { 89 throw new InternalError ("clone not supported"); 90 } 91 } 92 93 96 public void afterJTSTransaction() { 97 if (usesExternalTransactionController()) { 98 setIsInTransaction(false); 99 if ((getDatasourceConnection() != null) && usesExternalConnectionPooling()) { 100 closeConnection(); 101 setDatasourceConnection(null); 102 } 103 } 104 } 105 106 109 protected void setIsInTransaction(boolean value) { 110 isInTransaction = value; 111 } 112 113 116 public boolean isInTransaction() { 117 return isInTransaction; 118 } 119 120 123 public boolean usesExternalConnectionPooling() { 124 if (getLogin() == null) { 125 throw DatabaseException.databaseAccessorNotConnected(); 126 } 127 return getLogin().shouldUseExternalConnectionPooling(); 128 } 129 130 133 public void beginTransaction(AbstractSession session) throws DatabaseException { 134 if (usesExternalTransactionController()) { 135 setIsInTransaction(true); 136 return; 137 } 138 139 session.log(SessionLog.FINER, SessionLog.TRANSACTION, "begin_transaction", (Object [])null, this); 140 141 try { 142 session.startOperationProfile(SessionProfiler.TRANSACTION); 143 incrementCallCount(session); 144 basicBeginTransaction(session); 145 setIsInTransaction(true); 146 } finally { 147 decrementCallCount(); 148 session.endOperationProfile(SessionProfiler.TRANSACTION); 149 } 150 } 151 152 155 protected abstract void basicBeginTransaction(AbstractSession session); 156 157 160 protected abstract void basicCommitTransaction(AbstractSession session); 161 162 165 protected abstract void basicRollbackTransaction(AbstractSession session); 166 167 170 public synchronized void decrementCallCount() { 171 setCallCount(getCallCount() - 1); 172 if (usesExternalConnectionPooling() && (!isInTransaction()) && (getCallCount() == 0)) { 173 try { 174 closeConnection(); 175 } catch (DatabaseException ignore) { 176 } 177 } 179 } 180 181 184 public synchronized void incrementCallCount(AbstractSession session) { 185 setCallCount(getCallCount() + 1); 186 187 if (getCallCount() == 1) { 188 if (getLogin() == null) { 190 throw DatabaseException.databaseAccessorNotConnected(); 191 } 192 193 if (getDatasourceConnection() != null) { 195 if (!isConnected()) { 196 if (isInTransaction()) { 197 throw DatabaseException.databaseAccessorNotConnected(); 198 } else { 199 reconnect(session); 200 } 201 } 202 } else { 203 if (usesExternalConnectionPooling()) { 205 reconnect(session); 206 } else { 207 throw DatabaseException.databaseAccessorNotConnected(); 208 } 209 } 210 } 211 } 212 213 217 protected void connect(Login login) throws DatabaseException { 218 setDatasourceConnection(login.connectToDatasource(this)); 219 setIsConnected(true); 220 } 221 222 225 protected void setIsConnected(boolean isConnected) { 226 this.isConnected = isConnected; 227 } 228 229 232 protected void setCallCount(int callCount) { 233 this.callCount = callCount; 234 } 235 236 239 public int getCallCount() { 240 return callCount; 241 } 242 243 246 public void commitTransaction(AbstractSession session) throws DatabaseException { 247 if (usesExternalTransactionController()) { 248 if (session.getExternalTransactionController() == null) { 254 setIsInTransaction(false); 255 if ((getDatasourceConnection() != null) && usesExternalConnectionPooling()) { 256 closeConnection(); 257 setDatasourceConnection(null); 258 } 259 } 260 return; 261 } 262 263 session.log(SessionLog.FINER, SessionLog.TRANSACTION, "commit_transaction", (Object [])null, this); 264 265 try { 266 session.startOperationProfile(SessionProfiler.TRANSACTION); 267 incrementCallCount(session); 268 basicCommitTransaction(session); 269 270 session.afterTransaction(true, false); 272 setIsInTransaction(false); 273 } finally { 274 decrementCallCount(); 275 session.endOperationProfile(SessionProfiler.TRANSACTION); 276 } 277 } 278 279 283 public void connect(Login login, AbstractSession session) throws DatabaseException { 284 session.startOperationProfile(SessionProfiler.CONNECT); 285 session.incrementProfile(SessionProfiler.TlConnects); 286 287 try { 288 if (session.shouldLog(SessionLog.CONFIG, SessionLog.CONNECTION)) { session.log(SessionLog.CONFIG, SessionLog.CONNECTION, "connecting", new Object [] { login }, this); 290 } 291 setLogin(login); 292 this.setDatasourcePlatform((DatasourcePlatform)session.getDatasourceLogin().getDatasourcePlatform()); 293 try { 294 connect(login); 295 setIsInTransaction(false); 296 } catch (RuntimeException exception) { 297 session.handleSevere(exception); 298 } 299 session.getEventManager().postConnect(this); 300 incrementCallCount(session); 301 try { 302 buildConnectLog(session); 303 } finally { 304 decrementCallCount(); 305 } 306 } finally { 307 session.endOperationProfile(SessionProfiler.CONNECT); 308 } 309 } 310 311 314 protected abstract void closeDatasourceConnection(); 315 316 319 protected abstract Object basicExecuteCall(Call call, AbstractRecord row, AbstractSession session); 320 321 324 protected abstract void buildConnectLog(AbstractSession session); 325 326 329 public Login getLogin() { 330 return login; 331 } 332 333 337 protected void setLogin(Login login) { 338 this.login = login; 339 } 340 341 344 public void disconnect(AbstractSession session) throws DatabaseException { 345 session.log(SessionLog.CONFIG, SessionLog.CONNECTION, "disconnect", (Object [])null, this); 346 347 if (getDatasourceConnection() == null) { 348 return; 349 } 350 session.incrementProfile(SessionProfiler.TlDisconnects); 351 session.startOperationProfile(SessionProfiler.CONNECT); 352 closeDatasourceConnection(); 353 setDatasourceConnection(null); 354 setIsInTransaction(false); 355 session.endOperationProfile(SessionProfiler.CONNECT); 356 } 357 358 363 public void closeConnection() { 364 try { 365 if (getDatasourceConnection() != null) { 366 if (isDatasourceConnected()) { 367 closeDatasourceConnection(); 368 } 369 setDatasourceConnection(null); 370 } 371 } catch (DatabaseException exception) { 372 setDatasourceConnection(null); 374 } 375 } 376 377 381 public Object executeCall(Call call, AbstractRecord translationRow, AbstractSession session) throws DatabaseException { 382 if (getLogin() == null) { 384 throw DatabaseException.databaseAccessorNotConnected(); 385 } 386 387 if (session.shouldLog(SessionLog.FINE, SessionLog.SQL)) { session.log(SessionLog.FINE, SessionLog.SQL, call.getLogString(this), (Object [])null, this, false); 389 } 390 391 Object result = basicExecuteCall(call, translationRow, session); 392 393 return result; 394 } 395 396 403 public void reestablishConnection(AbstractSession session) throws DatabaseException { 404 if (session.shouldLog(SessionLog.CONFIG, SessionLog.CONNECTION)) { Object [] args = { getLogin() }; 406 session.log(SessionLog.CONFIG, SessionLog.CONNECTION, "reconnecting", args, this); 407 } 408 reconnect(session); 409 setIsInTransaction(false); 410 session.getEventManager().postConnect(this); 411 } 412 413 418 protected void reconnect(AbstractSession session) throws DatabaseException { 419 session.log(SessionLog.FINEST, SessionLog.CONNECTION, "reconnecting_to_external_connection_pool"); 420 session.startOperationProfile(SessionProfiler.CONNECT); 421 connect(getLogin()); 422 session.endOperationProfile(SessionProfiler.CONNECT); 423 } 424 425 428 public DatasourcePlatform getDatasourcePlatform() { 429 return platform; 430 } 431 432 437 public void setDatasourcePlatform(DatasourcePlatform platform) { 438 this.platform = platform; 439 } 440 441 444 public Object getDatasourceConnection() { 445 return datasourceConnection; 446 } 447 448 452 public java.sql.Connection getConnection() { 453 return (java.sql.Connection )getDatasourceConnection(); 454 } 455 456 460 public Vector getColumnInfo(String catalog, String schema, String tableName, String columnName, AbstractSession session) throws DatabaseException { 461 return new Vector(); 462 } 463 464 468 public Vector getTableInfo(String catalog, String schema, String tableName, String [] types, AbstractSession session) throws DatabaseException { 469 return new Vector(); 470 } 471 472 475 protected void setDatasourceConnection(Object connection) { 476 this.datasourceConnection = connection; 477 } 478 479 482 public void rollbackTransaction(AbstractSession session) throws DatabaseException { 483 if (usesExternalTransactionController()) { 484 if (session.getExternalTransactionController() == null) { 490 setIsInTransaction(false); 491 if ((getDatasourceConnection() != null) && usesExternalConnectionPooling()) { 492 closeConnection(); 493 setDatasourceConnection(null); 494 } 495 } 496 return; 497 } 498 499 session.log(SessionLog.FINER, SessionLog.TRANSACTION, "rollback_transaction", (Object [])null, this); 500 501 try { 502 session.startOperationProfile(SessionProfiler.TRANSACTION); 503 incrementCallCount(session); 504 basicRollbackTransaction(session); 505 } finally { 506 session.afterTransaction(false, false); 508 setIsInTransaction(false); 509 decrementCallCount(); 510 session.endOperationProfile(SessionProfiler.TRANSACTION); 511 } 512 } 513 514 517 public boolean usesExternalTransactionController() { 518 if (getLogin() == null) { 519 throw DatabaseException.databaseAccessorNotConnected(); 520 } 521 return getLogin().shouldUseExternalTransactionController(); 522 } 523 524 528 public boolean isConnected() { 529 if ((getDatasourceConnection() == null) && (getLogin() == null)) { 530 return false; 531 } 532 if (usesExternalConnectionPooling()) { 533 return true; } 535 536 if (getDatasourceConnection() == null) { 537 return false; 538 } 539 540 return isDatasourceConnected(); 541 } 542 543 546 protected abstract boolean isDatasourceConnected(); 547 548 552 public void flushSelectCalls(AbstractSession session) { 553 } 555 556 562 public void writesCompleted(AbstractSession session) { 563 } 565 } 566 | Popular Tags |