1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 27 import java.util.*; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.internal.helper.DBPlatformHelper; 30 import oracle.toplink.essentials.sessions.Login; 31 import oracle.toplink.essentials.internal.sequencing.Sequencing; 32 import oracle.toplink.essentials.internal.sequencing.SequencingCallback; 33 import oracle.toplink.essentials.internal.sequencing.SequencingHome; 34 import oracle.toplink.essentials.internal.sequencing.SequencingFactory; 35 import oracle.toplink.essentials.sequencing.SequencingControl; 36 import oracle.toplink.essentials.logging.SessionLog; 37 import oracle.toplink.essentials.sessions.DatasourceLogin; 38 import oracle.toplink.essentials.sessions.SessionProfiler; 39 import oracle.toplink.essentials.platform.server.ServerPlatform; 40 import oracle.toplink.essentials.platform.server.NoServerPlatform; 41 42 import oracle.toplink.essentials.descriptors.ClassDescriptor; 43 44 67 public class DatabaseSessionImpl extends AbstractSession implements oracle.toplink.essentials.sessions.DatabaseSession { 68 69 73 private SequencingHome sequencingHome; 74 75 78 private ServerPlatform serverPlatform; 79 80 84 private long connectedTime; 85 86 90 91 protected boolean isLoggedIn; 93 94 98 protected void setSequencingHome(SequencingHome sequencingHome) { 99 this.sequencingHome = sequencingHome; 100 } 101 102 107 protected SequencingHome getSequencingHome() { 108 if (sequencingHome == null) { 109 setSequencingHome(SequencingFactory.createSequencingHome(this)); 110 } 111 return sequencingHome; 112 } 113 114 119 public SequencingControl getSequencingControl() { 120 return getSequencingHome().getSequencingControl(); 121 } 122 123 127 public Sequencing getSequencing() { 128 return getSequencingHome().getSequencing(); 129 } 130 131 135 public void initializeSequencing() { 136 getSequencingHome().onDisconnect(); 137 getSequencingHome().onConnect(); 138 } 139 140 144 public void afterTransaction(boolean committed, boolean isExternalTransaction) { 145 SequencingCallback callback = getSequencingHome().getSequencingCallback(); 146 if (callback != null) { 147 callback.afterTransaction(getAccessor(), committed); 148 } 149 } 150 151 156 public DatabaseSessionImpl() { 157 super(); 158 this.setServerPlatform(new NoServerPlatform(this)); 159 } 160 161 170 public DatabaseSessionImpl(Login login) { 171 this(new oracle.toplink.essentials.sessions.Project(login)); 172 } 173 174 182 public DatabaseSessionImpl(oracle.toplink.essentials.sessions.Project project) { 183 super(project); 184 this.setServerPlatform(new NoServerPlatform(this)); 185 } 186 187 198 public void addDescriptor(ClassDescriptor descriptor) { 199 this.lastDescriptorAccessed = null; 201 202 getProject().addDescriptor(descriptor, this); 203 } 204 205 212 public void addDescriptors(Vector descriptors) { 213 this.lastDescriptorAccessed = null; 215 216 getProject().addDescriptors(descriptors, this); 217 } 218 219 225 public void addDescriptors(oracle.toplink.essentials.sessions.Project project) { 226 this.lastDescriptorAccessed = null; 228 229 getProject().addDescriptors(project, this); 230 } 231 232 236 public void connect() throws DatabaseException { 237 connect(false); 238 } 239 240 244 public void connect(boolean updatePlatform) throws DatabaseException { 245 getAccessor().connect(getDatasourceLogin(), this); 246 } 247 248 252 public void disconnect() throws DatabaseException { 253 getSequencingHome().onDisconnect(); 254 getAccessor().disconnect(this); 255 } 256 257 274 public ServerPlatform getServerPlatform() { 275 return serverPlatform; 276 } 277 278 297 public void setServerPlatform(ServerPlatform newServerPlatform) { 298 if (this.isConnected()) { 299 throw ValidationException.serverPlatformIsReadOnlyAfterLogin(newServerPlatform.getClass().getName()); 300 } 301 this.serverPlatform = newServerPlatform; 302 } 303 304 308 protected void finalize() throws DatabaseException { 309 if (isConnected()) { 310 logout(); 311 } 312 } 313 314 318 public Map getDescriptors() { 319 return getProject().getDescriptors(); 320 } 321 322 328 public void initializeDescriptorIfSessionAlive(ClassDescriptor descriptor) { 329 if (isConnected() && (descriptor.requiresInitialization())) { 330 try { 331 try { 332 descriptor.preInitialize(this); 333 descriptor.initialize(this); 334 descriptor.postInitialize(this); 335 getCommitManager().initializeCommitOrder(); 336 } catch (RuntimeException exception) { 337 getIntegrityChecker().handleError(exception); 338 } 339 340 if (getIntegrityChecker().hasErrors()) { 341 handleException(new IntegrityException(getIntegrityChecker())); 343 } 344 } finally { 345 clearIntegrityChecker(); 346 } 347 } 348 } 349 350 356 public void initializeDescriptors() { 357 getProject().setIsPureCMP2Project(true); 359 initializeDescriptors((Map)((HashMap)getDescriptors()).clone()); 361 } 362 363 370 public void initializeDescriptors(Map descriptors) { 371 initializeSequencing(); 372 try { 373 Iterator iterator = descriptors.values().iterator(); 375 while (iterator.hasNext()) { 376 ClassDescriptor descriptor = (ClassDescriptor)iterator.next(); 377 try { 378 AbstractSession session = getSessionForClass(descriptor.getJavaClass()); 379 if (descriptor.requiresInitialization()) { 380 descriptor.preInitialize(session); 381 } 382 383 if (descriptor.isAggregateDescriptor() && descriptor.isChildDescriptor()) { 385 descriptor.initializeAggregateInheritancePolicy(session); 386 } 387 } catch (RuntimeException exception) { 388 getIntegrityChecker().handleError(exception); 389 } 390 } 391 392 iterator = descriptors.values().iterator(); 394 while (iterator.hasNext()) { 395 ClassDescriptor descriptor = (ClassDescriptor)iterator.next(); 396 try { 397 AbstractSession session = getSessionForClass(descriptor.getJavaClass()); 398 if (descriptor.requiresInitialization()) { 399 descriptor.initialize(session); 400 } 401 } catch (RuntimeException exception) { 402 getIntegrityChecker().handleError(exception); 403 } 404 } 405 406 iterator = descriptors.values().iterator(); 408 while (iterator.hasNext()) { 409 ClassDescriptor descriptor = (ClassDescriptor)iterator.next(); 410 try { 411 AbstractSession session = getSessionForClass(descriptor.getJavaClass()); 412 if (descriptor.requiresInitialization()) { 413 descriptor.postInitialize(session); 414 } 415 } catch (RuntimeException exception) { 416 getIntegrityChecker().handleError(exception); 417 } 418 } 419 420 try { 421 getCommitManager().initializeCommitOrder(); 422 } catch (RuntimeException exception) { 423 getIntegrityChecker().handleError(exception); 424 } 425 426 if (getIntegrityChecker().hasErrors()) { 427 handleSevere(new IntegrityException(getIntegrityChecker())); 429 } 430 } finally { 431 clearIntegrityChecker(); 432 } 433 } 434 435 442 public void initializeDescriptors(Vector descriptors) { 443 initializeSequencing(); 444 try { 445 for (Enumeration descriptorEnum = descriptors.elements(); 447 descriptorEnum.hasMoreElements();) { 448 try { 449 ClassDescriptor descriptor = (ClassDescriptor)descriptorEnum.nextElement(); 450 AbstractSession session = getSessionForClass(descriptor.getJavaClass()); 451 if (descriptor.requiresInitialization()) { 452 descriptor.preInitialize(session); 453 } 454 455 if (descriptor.isAggregateDescriptor() && descriptor.isChildDescriptor()) { 457 descriptor.initializeAggregateInheritancePolicy(session); 458 } 459 } catch (RuntimeException exception) { 460 getIntegrityChecker().handleError(exception); 461 } 462 } 463 464 for (Enumeration descriptorEnum = descriptors.elements(); 466 descriptorEnum.hasMoreElements();) { 467 try { 468 ClassDescriptor descriptor = (ClassDescriptor)descriptorEnum.nextElement(); 469 AbstractSession session = getSessionForClass(descriptor.getJavaClass()); 470 if (descriptor.requiresInitialization()) { 471 descriptor.initialize(session); 472 } 473 } catch (RuntimeException exception) { 474 getIntegrityChecker().handleError(exception); 475 } 476 } 477 478 for (Enumeration descriptorEnum = descriptors.elements(); 480 descriptorEnum.hasMoreElements();) { 481 try { 482 ClassDescriptor descriptor = (ClassDescriptor)descriptorEnum.nextElement(); 483 AbstractSession session = getSessionForClass(descriptor.getJavaClass()); 484 if (descriptor.requiresInitialization()) { 485 descriptor.postInitialize(session); 486 } 487 } catch (RuntimeException exception) { 488 getIntegrityChecker().handleError(exception); 489 } 490 } 491 492 try { 493 getCommitManager().initializeCommitOrder(); 494 } catch (RuntimeException exception) { 495 getIntegrityChecker().handleError(exception); 496 } 497 498 if (getIntegrityChecker().hasErrors()) { 499 handleException(new IntegrityException(getIntegrityChecker())); 501 } 502 } finally { 503 clearIntegrityChecker(); 504 } 505 } 506 507 511 public boolean isDatabaseSession() { 512 return true; 513 } 514 515 519 protected Login getReadLogin(){ 520 return getDatasourceLogin(); 521 } 522 523 533 public void loginAndDetectDatasource() throws DatabaseException { 534 preConnectDatasource(); 535 Connection conn = null; 536 try{ 537 conn = (Connection )getReadLogin().connectToDatasource(null); 538 getLogin().setPlatformClassName(DBPlatformHelper.getDBPlatform(conn.getMetaData().getDatabaseProductName(), getSessionLog())); 539 }catch (SQLException ex){ 540 DatabaseException dbEx = DatabaseException.errorRetrieveDbMetadataThroughJDBCConnection(); 541 dbEx.initCause(ex); 544 throw dbEx; 545 }finally{ 546 if (conn != null){ 547 try{ 548 conn.close(); 549 }catch (SQLException ex){ 550 DatabaseException dbEx = DatabaseException.errorRetrieveDbMetadataThroughJDBCConnection(); 551 dbEx.initCause(ex); 554 throw dbEx; 555 } 556 } 557 } 558 connect(true); 559 postConnectDatasource(); 560 } 561 562 569 public void login() throws DatabaseException { 570 preConnectDatasource(); 571 connect(false); 572 postConnectDatasource(); 573 } 574 575 582 public void login(String userName, String password) throws DatabaseException { 583 getDatasourceLogin().setUserName(userName); 584 getDatasourceLogin().setPassword(password); 585 login(); 586 } 587 588 594 public void login(Login login) throws DatabaseException { 595 setLogin(login); 596 login(); 597 } 598 599 607 public void logout() throws DatabaseException { 608 this.lastDescriptorAccessed = null; 610 611 if (isInTransaction()) { 612 throw DatabaseException.logoutWhileTransactionInProgress(); 613 } 614 if (getAccessor() == null) { 615 return; 616 } 617 618 disconnect(); 619 getIdentityMapAccessor().initializeIdentityMaps(); 620 isLoggedIn = false; 621 log(SessionLog.INFO, null, "logout_successful", this.getName()); 622 623 getServerPlatform().unregisterMBean(); 625 } 626 627 632 public void initializeConnectedTime() { 633 connectedTime = System.currentTimeMillis(); 634 } 635 636 641 public long getConnectedTime() { 642 return connectedTime; 643 } 644 645 650 protected void preConnectDatasource(){ 651 if (isLoggedIn) { 653 throw ValidationException.alreadyLoggedIn(this.getName()); 654 } 655 this.platform = null; 656 if (isInProfile()) { 657 getProfiler().initialize(); 658 } 659 updateProfile(SessionProfiler.LoginTime, new Date(System.currentTimeMillis())); 660 661 getEventManager().preLogin(this); 663 getServerPlatform().initializeExternalTransactionController(); 665 log(SessionLog.INFO, null, "topLink_version", DatasourceLogin.getVersion()); 666 if (getServerPlatform().getServerNameAndVersion() != null) { 667 log(SessionLog.INFO, null, "application_server_name_and_version", getServerPlatform().getServerNameAndVersion()); 668 } 669 } 670 671 676 protected void postConnectDatasource(){ 677 initializeDescriptors(); 678 processEJBQLQueries(); 680 log(SessionLog.INFO, null, "login_successful", this.getName()); 681 getEventManager().postLogin(this); 682 683 initializeConnectedTime(); 684 this.isLoggedIn = true; 685 this.platform = null; 686 687 getServerPlatform().registerMBean(); 689 } 690 691 701 public void writeAllObjects(Collection domainObjects) throws DatabaseException, OptimisticLockException { 702 for (Iterator objectsEnum = domainObjects.iterator(); objectsEnum.hasNext();) { 703 writeObject(objectsEnum.next()); 704 } 705 } 706 707 717 public void writeAllObjects(Vector domainObjects) throws DatabaseException, OptimisticLockException { 718 for (Enumeration objectsEnum = domainObjects.elements(); objectsEnum.hasMoreElements();) { 719 writeObject(objectsEnum.nextElement()); 720 } 721 } 722 } 723 | Popular Tags |