1 21 package oracle.toplink.essentials.threetier; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.platform.server.ServerPlatform; 27 import oracle.toplink.essentials.queryframework.*; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.internal.databaseaccess.*; 30 import oracle.toplink.essentials.internal.sequencing.Sequencing; 31 import oracle.toplink.essentials.internal.sequencing.SequencingFactory; 32 import oracle.toplink.essentials.logging.SessionLog; 33 import oracle.toplink.essentials.internal.sessions.*; 34 import oracle.toplink.essentials.sessions.Project; 35 import oracle.toplink.essentials.sessions.SessionProfiler; 36 import oracle.toplink.essentials.internal.sessions.AbstractSession; 37 38 56 public class ClientSession extends AbstractSession { 57 protected ServerSession parent; 58 protected ConnectionPolicy connectionPolicy; 59 protected Accessor writeConnection; 60 protected boolean isActive; 61 protected Sequencing sequencing; 62 63 67 public ClientSession(ServerSession parent, ConnectionPolicy connectionPolicy) { 68 super(parent.getProject()); 69 if (connectionPolicy.isUserDefinedConnection()) { 70 this.setProject((Project)getProject().clone()); 72 this.setLogin(connectionPolicy.getLogin()); 73 } 74 this.isActive = true; 75 this.externalTransactionController = parent.getExternalTransactionController(); 76 this.parent = parent; 77 this.connectionPolicy = connectionPolicy; 78 this.writeConnection = this.accessor; this.accessor = parent.getAccessor(); this.name = parent.getName(); 81 this.profiler = parent.getProfiler(); 82 this.isInProfile = parent.isInProfile; 83 this.commitManager = parent.getCommitManager(); 84 this.sessionLog = parent.getSessionLog(); 85 this.eventManager = parent.getEventManager().clone(this); 86 this.exceptionHandler = parent.getExceptionHandler(); 87 88 getEventManager().postAcquireClientSession(); 89 incrementProfile(SessionProfiler.ClientSessionCreated); 90 } 91 92 protected ClientSession(oracle.toplink.essentials.sessions.Project project) { 93 super(project); 94 } 95 96 100 public void afterTransaction(boolean committed, boolean isExternalTransaction) { 101 if (hasWriteConnection()) { 102 getParent().afterTransaction(committed, isExternalTransaction, getWriteConnection()); 103 if (isExternalTransaction) { 104 getWriteConnection().afterJTSTransaction(); 105 releaseWriteConnection(); 106 } 107 } 108 } 109 110 114 public void basicBeginTransaction() { 115 if (!hasWriteConnection()) { 118 if (getConnectionPolicy().isLazy()) { 120 getParent().acquireClientConnection(this); 121 } 122 } 123 super.basicBeginTransaction(); 124 } 125 126 130 public void basicCommitTransaction() { 131 super.basicCommitTransaction(); 134 135 if (!getParent().getDatasourceLogin().shouldUseExternalTransactionController()) { 138 releaseWriteConnection(); 139 } 140 else if (this.getExternalTransactionController() == null) { 146 releaseWriteConnection(); 147 } 148 } 149 150 154 public void basicRollbackTransaction() { 155 try { 156 if (hasWriteConnection()) { 159 super.basicRollbackTransaction(); 160 } 161 } finally { 162 if (!getParent().getDatasourceLogin().shouldUseExternalTransactionController()) { 165 releaseWriteConnection(); 166 } 167 else if (this.getExternalTransactionController() == null) { 173 releaseWriteConnection(); 174 } 175 } 176 } 177 178 182 public void connect() throws DatabaseException { 183 getWriteConnection().connect(getDatasourceLogin(), this); 184 } 185 186 191 public boolean containsQuery(String queryName) { 192 boolean containsQuery = getQueries().containsKey(queryName); 193 if (containsQuery == false) { 194 containsQuery = getParent().containsQuery(queryName); 195 } 196 return containsQuery; 197 } 198 199 203 public void disconnect() throws DatabaseException { 204 getWriteConnection().disconnect(this); 205 } 206 207 211 protected void finalize() throws DatabaseException { 212 if (isActive()) { 213 release(); 214 } 215 } 216 217 221 public Accessor getAccessor() { 222 if (isInTransaction()) { 223 return getWriteConnection(); 224 } 225 return super.getAccessor(); 226 } 227 228 234 public ConnectionPolicy getConnectionPolicy() { 235 return connectionPolicy; 236 } 237 238 244 public Map getDescriptors() { 245 return getParent().getDescriptors(); 246 } 247 248 264 public AbstractSession getParentIdentityMapSession(DatabaseQuery query, boolean canReturnSelf, boolean terminalOnly) { 265 return getParent().getParentIdentityMapSession(query, canReturnSelf, terminalOnly); 269 } 270 271 286 public AbstractSession getExecutionSession(DatabaseQuery query) { 287 if (isInTransaction()) { 296 return this; 297 } 298 return getParent().getExecutionSession(query); 299 } 300 301 306 public ServerSession getParent() { 307 return parent; 308 } 309 310 316 public DatabaseQuery getQuery(String name) { 317 DatabaseQuery query = (DatabaseQuery)super.getQuery(name); 318 if (query == null) { 319 query = getParent().getQuery(name); 320 } 321 322 return query; 323 } 324 325 328 public DatabaseQuery getQuery(String name, Vector args) { DatabaseQuery query = super.getQuery(name, args); 330 if (query == null) { 331 query = getParent().getQuery(name, args); 332 } 333 return query; 334 } 335 336 343 public void initializeSequencing() { 344 this.sequencing = SequencingFactory.createSequencing(this); 345 } 346 347 352 public Sequencing getSequencing() { 353 if (sequencing == null) { 355 initializeSequencing(); 356 } 357 return sequencing; 358 } 359 360 366 public ServerPlatform getServerPlatform(){ 367 return getParent().getServerPlatform(); 368 } 369 370 381 public String getSessionTypeString() { 382 return "ClientSession"; 383 } 384 385 389 public Accessor getWriteConnection() { 390 return writeConnection; 391 } 392 393 397 protected boolean hasWriteConnection() { 398 if (getWriteConnection() == null) { 399 return false; 400 } 401 402 return getWriteConnection().isConnected(); 403 } 404 405 410 public void initializeIdentityMapAccessor() { 411 this.identityMapAccessor = new ClientSessionIdentityMapAccessor(this); 412 } 413 414 419 public boolean isActive() { 420 return isActive; 421 } 422 423 427 public boolean isClientSession() { 428 return true; 429 } 430 431 436 public boolean isConnected() { 437 return getParent().isConnected(); 438 } 439 440 448 public void release() throws DatabaseException { 449 if (!isActive()) { 450 return; 451 } 452 getEventManager().preReleaseClientSession(); 453 454 if (hasWriteConnection()) { 458 getParent().releaseClientSession(this); 459 } 460 461 setIsActive(false); 463 log(SessionLog.FINER, SessionLog.CONNECTION, "client_released"); 464 getEventManager().postReleaseClientSession(); 465 } 466 467 471 protected void releaseWriteConnection() { 472 if (getConnectionPolicy().isLazy() && hasWriteConnection()) { 473 getParent().releaseClientSession(this); 474 setWriteConnection(null); 475 } 476 } 477 478 482 protected void setConnectionPolicy(ConnectionPolicy connectionPolicy) { 483 this.connectionPolicy = connectionPolicy; 484 } 485 486 490 protected void setIsActive(boolean isActive) { 491 this.isActive = isActive; 492 } 493 494 499 protected void setParent(ServerSession parent) { 500 this.parent = parent; 501 } 502 503 507 public void setWriteConnection(Accessor writeConnection) { 508 this.writeConnection = writeConnection; 509 } 510 511 515 public String toString() { 516 StringWriter writer = new StringWriter(); 517 writer.write(getSessionTypeString()); 518 writer.write("("); 519 writer.write(String.valueOf(getWriteConnection())); 520 writer.write(")"); 521 return writer.toString(); 522 } 523 } 524 | Popular Tags |