1 21 package oracle.toplink.essentials.threetier; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.databaseaccess.*; 26 import oracle.toplink.essentials.queryframework.*; 27 import oracle.toplink.essentials.sessions.DatasourceLogin; 28 import oracle.toplink.essentials.sessions.Login; 29 import oracle.toplink.essentials.sessions.UnitOfWork; 30 import oracle.toplink.essentials.exceptions.*; 31 import oracle.toplink.essentials.internal.sequencing.SequencingCallback; 32 import oracle.toplink.essentials.internal.sequencing.SequencingServer; 33 import oracle.toplink.essentials.logging.SessionLog; 34 import oracle.toplink.essentials.internal.sessions.*; 35 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 36 import oracle.toplink.essentials.internal.sessions.AbstractSession; 37 38 62 public class ServerSession extends DatabaseSessionImpl implements Server { 63 protected ConnectionPool readConnectionPool; 64 protected Map connectionPools; 65 protected ConnectionPolicy defaultConnectionPolicy; 66 protected int maxNumberOfNonPooledConnections; 67 protected int numberOfNonPooledConnectionsUsed; 68 public static final int MAX_WRITE_CONNECTIONS = 10; 69 public static final int MIN_WRITE_CONNECTIONS = 5; 70 71 76 public ServerSession() { 77 super(); 78 this.connectionPools = new HashMap(10); 79 } 80 81 92 public ServerSession(Login login) { 93 this(new oracle.toplink.essentials.sessions.Project(login)); 94 } 95 96 101 public ServerSession(Login login, int minNumberOfPooledConnection, int maxNumberOfPooledConnection) { 102 this(new oracle.toplink.essentials.sessions.Project(login), minNumberOfPooledConnection, maxNumberOfPooledConnection); 103 } 104 105 111 public ServerSession(Login login, ConnectionPolicy defaultConnectionPolicy) { 112 this(new oracle.toplink.essentials.sessions.Project(login), defaultConnectionPolicy); 113 } 114 115 126 public ServerSession(oracle.toplink.essentials.sessions.Project project) { 127 this(project, MIN_WRITE_CONNECTIONS, MAX_WRITE_CONNECTIONS); 128 } 129 130 135 public ServerSession(oracle.toplink.essentials.sessions.Project project, int minNumberOfPooledConnection, int maxNumberOfPooledConnection) { 136 this(project, new ConnectionPolicy("default")); 137 138 ConnectionPool pool = null; 139 if (project.getDatasourceLogin().shouldUseExternalConnectionPooling()) { 140 pool = new ExternalConnectionPool("default", project.getDatasourceLogin(), this); 141 } else { 142 pool = new ConnectionPool("default", project.getDatasourceLogin(), minNumberOfPooledConnection, maxNumberOfPooledConnection, this); 143 } 144 this.connectionPools.put("default", pool); 145 } 146 147 159 public ServerSession(oracle.toplink.essentials.sessions.Project project, int minNumberOfPooledConnection, int maxNumberOfPooledConnection, oracle.toplink.essentials.sessions.Login readLogin) { 160 this(project, new ConnectionPolicy("default"), readLogin); 161 162 ConnectionPool pool = null; 163 if (project.getDatasourceLogin().shouldUseExternalConnectionPooling()) { 164 pool = new ExternalConnectionPool("default", project.getDatasourceLogin(), this); 165 } else { 166 pool = new ConnectionPool("default", project.getDatasourceLogin(), minNumberOfPooledConnection, maxNumberOfPooledConnection, this); 167 } 168 this.connectionPools.put("default", pool); 169 } 170 171 189 public ServerSession(oracle.toplink.essentials.sessions.Project project, int minNumberOfPooledConnection, int maxNumberOfPooledConnection, oracle.toplink.essentials.sessions.Login readLogin, oracle.toplink.essentials.sessions.Login sequenceLogin) { 190 this(project, new ConnectionPolicy("default"), readLogin, sequenceLogin); 191 192 ConnectionPool pool = null; 193 if (project.getDatasourceLogin().shouldUseExternalConnectionPooling()) { 194 pool = new ExternalConnectionPool("default", project.getDatasourceLogin(), this); 195 } else { 196 pool = new ConnectionPool("default", project.getDatasourceLogin(), minNumberOfPooledConnection, maxNumberOfPooledConnection, this); 197 } 198 this.connectionPools.put("default", pool); 199 } 200 201 207 public ServerSession(oracle.toplink.essentials.sessions.Project project, ConnectionPolicy defaultConnectionPolicy) { 208 super(project); 209 this.connectionPools = new HashMap(10); 210 this.defaultConnectionPolicy = defaultConnectionPolicy; 211 this.maxNumberOfNonPooledConnections = 50; 212 this.numberOfNonPooledConnectionsUsed = 0; 213 setReadConnectionPool(project.getDatasourceLogin()); 214 } 215 216 224 public ServerSession(oracle.toplink.essentials.sessions.Project project, ConnectionPolicy defaultConnectionPolicy, oracle.toplink.essentials.sessions.Login readLogin) { 225 super(project); 226 this.connectionPools = new HashMap(10); 227 this.defaultConnectionPolicy = defaultConnectionPolicy; 228 this.maxNumberOfNonPooledConnections = 50; 229 this.numberOfNonPooledConnectionsUsed = 0; 230 Login login = (readLogin != null) ? readLogin : project.getDatasourceLogin(); 231 setReadConnectionPool(login); 232 } 233 234 248 public ServerSession(oracle.toplink.essentials.sessions.Project project, ConnectionPolicy defaultConnectionPolicy, oracle.toplink.essentials.sessions.Login readLogin, oracle.toplink.essentials.sessions.Login sequenceLogin) { 249 this(project, defaultConnectionPolicy, readLogin); 250 251 if (sequenceLogin != null) { 252 getSequencingControl().setShouldUseSeparateConnection(true); 254 getSequencingControl().setLogin(sequenceLogin); 255 } 256 } 257 258 262 public void acquireClientConnection(ClientSession clientSession) throws DatabaseException, ConcurrencyException { 263 if (clientSession.getConnectionPolicy().isPooled()) { 264 ConnectionPool pool = (ConnectionPool)getConnectionPools().get(clientSession.getConnectionPolicy().getPoolName()); 265 Accessor connection = pool.acquireConnection(); 266 clientSession.setWriteConnection(connection); 267 getEventManager().postAcquireConnection(connection); 268 } else { 269 synchronized (this) { 271 while (getNumberOfNonPooledConnectionsUsed() >= getMaxNumberOfNonPooledConnections()) { 272 try { 273 wait(); } catch (InterruptedException exception) { 275 throw ConcurrencyException.waitFailureOnServerSession(exception); 276 } 277 } 278 279 setNumberOfNonPooledConnectionsUsed(getNumberOfNonPooledConnectionsUsed() + 1); 280 clientSession.setWriteConnection(clientSession.getLogin().buildAccessor()); 281 clientSession.connect(); 282 } 283 } 284 } 285 286 293 public ClientSession acquireClientSession() throws DatabaseException { 294 return acquireClientSession((ConnectionPolicy)getDefaultConnectionPolicy().clone()); 295 } 296 297 306 public ClientSession acquireClientSession(String poolName) throws DatabaseException { 307 return acquireClientSession(new ConnectionPolicy(poolName)); 308 } 309 310 320 public ClientSession acquireClientSession(Login login) throws DatabaseException { 321 return acquireClientSession(new ConnectionPolicy(login)); 322 } 323 324 329 public ClientSession acquireClientSession(ConnectionPolicy connectionPolicy) throws DatabaseException, ValidationException { 330 if (!isConnected()) { 331 throw ValidationException.loginBeforeAllocatingClientSessions(); 332 } 333 334 log(SessionLog.FINER, SessionLog.CONNECTION, "client_acquired"); 335 if (!connectionPolicy.isPooled() && (connectionPolicy.getLogin() == null)) { 336 connectionPolicy.setPoolName(getDefaultConnectionPolicy().getPoolName()); 339 connectionPolicy.setLogin(getDefaultConnectionPolicy().getLogin()); 340 } 341 if (connectionPolicy.isPooled()) { 342 ConnectionPool pool = (ConnectionPool)getConnectionPools().get(connectionPolicy.getPoolName()); 343 if (pool == null) { 344 throw ValidationException.poolNameDoesNotExist(connectionPolicy.getPoolName()); 345 } 346 connectionPolicy.setLogin((Login)pool.getLogin().clone()); 347 } 348 ClientSession client = null; 349 if (getProject().hasIsolatedClasses()) { 350 client = new IsolatedClientSession(this, connectionPolicy); 351 } else { 352 client = new ClientSession(this, connectionPolicy); 353 } 354 if (!connectionPolicy.isLazy()) { 355 acquireClientConnection(client); 356 } 357 358 return client; 359 } 360 361 370 public UnitOfWork acquireUnitOfWork() { 371 return acquireClientSession().acquireUnitOfWork(); 372 } 373 374 379 public void addConnectionPool(String poolName, Login login, int minNumberOfConnections, int maxNumberOfConnections) throws ValidationException { 380 if (minNumberOfConnections > maxNumberOfConnections) { 381 throw ValidationException.maxSizeLessThanMinSize(); 382 } 383 if (isConnected()) { 384 throw ValidationException.poolsMustBeConfiguredBeforeLogin(); 385 } 386 ConnectionPool pool = null; 387 if (login.shouldUseExternalConnectionPooling()) { 388 pool = new ExternalConnectionPool(poolName, login, this); 389 } else { 390 pool = new ConnectionPool(poolName, login, minNumberOfConnections, maxNumberOfConnections, this); 391 } 392 addConnectionPool(pool); 393 } 394 395 399 public void addConnectionPool(ConnectionPool pool) { 400 pool.setOwner(this); 401 getConnectionPools().put(pool.getName(), pool); 402 403 } 404 405 409 public void afterTransaction(boolean committed, boolean isExternalTransaction, Accessor accessor) { 410 SequencingCallback callback = getSequencingHome().getSequencingCallback(); 411 if (callback != null) { 412 callback.afterTransaction(accessor, committed); 413 } 414 } 415 416 422 public Accessor allocateReadConnection() { 423 Accessor connection = getReadConnectionPool().acquireConnection(); 424 getEventManager().postAcquireConnection(connection); 425 return connection; 426 } 427 428 432 public void connect(boolean updatePlatform) { 433 updateStandardConnectionPools(); 435 if (updatePlatform){ 436 for (Iterator pools = getConnectionPools().values().iterator(); pools.hasNext();){ 438 ((DatasourceLogin)((ConnectionPool)pools.next()).getLogin()).usePlatform(getDatasourceLogin().getDatasourcePlatform()); 439 } 440 ((DatasourceLogin)getReadConnectionPool().getLogin()).usePlatform(getDatasourceLogin().getDatasourcePlatform()); 441 } 442 getReadConnectionPool().startUp(); 444 setAccessor(allocateReadConnection()); 445 releaseReadConnection(getAccessor()); 446 447 for (Iterator poolsEnum = getConnectionPools().values().iterator(); poolsEnum.hasNext();) { 448 ((ConnectionPool)poolsEnum.next()).startUp(); 449 } 450 } 451 452 456 public Object executeCall(Call call, AbstractRecord translationRow, DatabaseQuery query) throws DatabaseException { 457 RuntimeException exception = null; 458 Object object = null; 459 boolean accessorAllocated = false; 460 if (query.getAccessor() == null) { 461 query.setAccessor(this.allocateReadConnection()); 462 accessorAllocated = true; 463 } 464 try { 465 object = query.getAccessor().executeCall(call, translationRow, this); 466 } catch (RuntimeException caughtException) { 467 exception = caughtException; 468 } finally { 469 if (call.isFinished()) { 470 try { 472 if (accessorAllocated) { 473 releaseReadConnection(query.getAccessor()); 474 query.setAccessor(null); 475 } 476 } catch (RuntimeException releaseException) { 477 if (exception == null) { 478 throw releaseException; 479 } 480 } 482 } 483 if (exception != null) { 484 throw exception; 485 } 486 } 487 return object; 488 } 489 490 494 public ConnectionPool getConnectionPool(String poolName) { 495 return (ConnectionPool)getConnectionPools().get(poolName); 496 } 497 498 502 public Map getConnectionPools() { 503 return connectionPools; 504 } 505 506 511 public ConnectionPolicy getDefaultConnectionPolicy() { 512 if (defaultConnectionPolicy == null) { 513 this.defaultConnectionPolicy = new ConnectionPolicy("default"); 514 } 515 return defaultConnectionPolicy; 516 } 517 518 522 public ConnectionPool getDefaultConnectionPool() { 523 return getConnectionPool("default"); 524 } 525 526 541 public AbstractSession getExecutionSession(DatabaseQuery query) { 542 if (query.isObjectLevelModifyQuery()) { 543 throw QueryException.invalidQueryOnServerSession(query); 544 } 545 return this; 546 } 547 548 554 public int getMaxNumberOfNonPooledConnections() { 555 return maxNumberOfNonPooledConnections; 556 } 557 558 562 public int getNumberOfNonPooledConnectionsUsed() { 563 return numberOfNonPooledConnectionsUsed; 564 } 565 566 570 protected Login getReadLogin(){ 571 return getReadConnectionPool().getLogin(); 572 } 573 574 585 public ConnectionPool getReadConnectionPool() { 586 return readConnectionPool; 587 } 588 589 593 public boolean isConnected() { 594 if (getReadConnectionPool() == null) { 595 return false; 596 } 597 598 return getReadConnectionPool().isConnected(); 599 } 600 601 605 public boolean isServerSession() { 606 return true; 607 } 608 609 613 public void logout() { 614 super.logout(); 615 616 getReadConnectionPool().shutDown(); 617 618 for (Iterator poolsEnum = getConnectionPools().values().iterator(); poolsEnum.hasNext();) { 619 ((ConnectionPool)poolsEnum.next()).shutDown(); 620 } 621 } 622 623 627 public void releaseClientSession(ClientSession clientSession) throws DatabaseException { 628 if (clientSession.getConnectionPolicy().isPooled()) { 629 ConnectionPool pool = (ConnectionPool)getConnectionPools().get(clientSession.getConnectionPolicy().getPoolName()); 630 getEventManager().preReleaseConnection(clientSession.getWriteConnection()); 631 pool.releaseConnection(clientSession.getWriteConnection()); 632 clientSession.setWriteConnection(null); 633 } else { 634 synchronized (this) { 635 clientSession.disconnect(); 636 clientSession.setWriteConnection(null); 637 setNumberOfNonPooledConnectionsUsed(getNumberOfNonPooledConnectionsUsed() - 1); 638 notify(); 639 } 640 } 641 } 642 643 647 public void releaseReadConnection(Accessor connection) { 648 getEventManager().preReleaseConnection(connection); 649 getReadConnectionPool().releaseConnection(connection); 650 } 651 652 656 public void setConnectionPools(Map connectionPools) { 657 this.connectionPools = connectionPools; 658 } 659 660 665 public void setDefaultConnectionPolicy(ConnectionPolicy defaultConnectionPolicy) { 666 this.defaultConnectionPolicy = defaultConnectionPolicy; 667 } 668 669 673 public void setDefaultConnectionPool() { 674 addConnectionPool("default", getDatasourceLogin(), 5, 10); 675 } 676 677 683 public void setMaxNumberOfNonPooledConnections(int maxNumberOfNonPooledConnections) { 684 this.maxNumberOfNonPooledConnections = maxNumberOfNonPooledConnections; 685 } 686 687 691 public void setNumberOfNonPooledConnectionsUsed(int numberOfNonPooledConnectionsUsed) { 692 this.numberOfNonPooledConnectionsUsed = numberOfNonPooledConnectionsUsed; 693 } 694 695 706 public void setReadConnectionPool(ConnectionPool readConnectionPool) { 707 if (isConnected()) { 708 throw ValidationException.cannotSetReadPoolSizeAfterLogin(); 709 } 710 this.readConnectionPool = readConnectionPool; 711 this.readConnectionPool.setOwner(this); 712 } 713 714 718 public void setReadConnectionPool(Login readLogin) throws ValidationException { 719 if (isConnected()) { 720 throw ValidationException.poolsMustBeConfiguredBeforeLogin(); 721 } 722 ConnectionPool pool = null; 723 if (readLogin.shouldUseExternalConnectionPooling()) { 724 pool = new ExternalConnectionPool("read", readLogin, this); 725 } else { 726 pool = new ConnectionPool("read", readLogin, 2, 2, this); 727 } 728 this.readConnectionPool = pool; 729 } 730 731 740 protected void updateStandardConnectionPools() { 741 if (getDefaultConnectionPool() != null) { 742 if (getDefaultConnectionPool().isThereConflictBetweenLoginAndType()) { 743 setDefaultConnectionPool(); 744 } 745 } 746 747 if (getReadConnectionPool() != null) { 748 if (getReadConnectionPool().isThereConflictBetweenLoginAndType()) { 749 setReadConnectionPool(getReadConnectionPool().getLogin()); 750 } 751 } 752 } 753 754 765 public void useExclusiveReadConnectionPool(int minNumerOfConnections, int maxNumerOfConnections) { 766 setReadConnectionPool(new ConnectionPool("read", getDatasourceLogin(), minNumerOfConnections, maxNumerOfConnections, this)); 767 } 768 769 780 public void useExternalReadConnectionPool() { 781 setReadConnectionPool(new ExternalConnectionPool("read", getDatasourceLogin(), this)); 782 } 783 784 795 public void useReadConnectionPool(int minNumerOfConnections, int maxNumerOfConnections) { 796 setReadConnectionPool(new ReadConnectionPool("read", getDatasourceLogin(), minNumerOfConnections, maxNumerOfConnections, this)); 797 } 798 799 803 public SequencingServer getSequencingServer() { 804 return getSequencingHome().getSequencingServer(); 805 } 806 } 807 | Popular Tags |