1 21 23 package oracle.toplink.essentials.internal.ejb.cmp3.base; 24 25 import java.util.Vector ; 26 import java.util.Map ; 27 28 import javax.persistence.EntityExistsException; 29 import javax.persistence.EntityNotFoundException; 30 import javax.persistence.LockModeType; 31 import javax.persistence.PersistenceException; 32 33 import oracle.toplink.essentials.descriptors.ClassDescriptor; 34 import oracle.toplink.essentials.exceptions.TopLinkException; 35 import oracle.toplink.essentials.exceptions.ValidationException; 36 import oracle.toplink.essentials.expressions.Expression; 37 import oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.TransactionWrapperImpl; 38 import oracle.toplink.essentials.internal.localization.ExceptionLocalization; 39 import oracle.toplink.essentials.internal.sessions.MergeManager; 40 import oracle.toplink.essentials.internal.helper.IdentityHashtable; 41 import oracle.toplink.essentials.internal.descriptors.OptimisticLockingPolicy; 42 import oracle.toplink.essentials.descriptors.VersionLockingPolicy; 43 import oracle.toplink.essentials.queryframework.*; 44 import oracle.toplink.essentials.sessions.Session; 45 import oracle.toplink.essentials.sessions.UnitOfWork; 46 import oracle.toplink.essentials.threetier.ServerSession; 47 import oracle.toplink.essentials.tools.sessionmanagement.SessionManager; 48 49 61 62 65 66 67 public abstract class EntityManagerImpl { 68 69 protected TransactionWrapperImpl transaction = null; 70 protected boolean isOpen = true; 71 72 protected RepeatableWriteUnitOfWork extendedPersistenceContext; 73 protected ServerSession serverSession; 76 protected EntityManagerFactoryImpl factory; 79 80 protected boolean extended; 82 protected boolean propagatePersistenceContext; 83 84 protected abstract void setJTATransactionWrapper(); 85 protected abstract void setEntityTransactionWrapper(); 86 90 public abstract boolean isFlushModeAUTO(); 91 92 98 public EntityManagerImpl(String sessionName, boolean propagatePersistenceContext, boolean extended){ 99 this((ServerSession) SessionManager.getManager().getSession(sessionName), null, propagatePersistenceContext, extended); 100 } 101 102 106 public EntityManagerImpl(ServerSession serverSession, boolean propagatePersistenceContext, boolean extended){ 107 this(serverSession, null, propagatePersistenceContext, extended); 108 } 109 110 111 117 public EntityManagerImpl(ServerSession serverSession, Map properties, boolean propagatePersistenceContext, boolean extended){ 118 this.serverSession = serverSession; 119 detectTransactionWrapper(); 120 this.extended = true; 121 this.propagatePersistenceContext = false; 122 } 123 124 130 public EntityManagerImpl(EntityManagerFactoryImpl factory, Map properties, boolean propagatePersistenceContext, boolean extended){ 131 this.factory = factory; 132 this.serverSession = factory.getServerSession(); 133 detectTransactionWrapper(); 134 this.extended = true; 135 this.propagatePersistenceContext = false; 136 } 137 138 144 public void clear(){ 145 if (this.isExtended()){ 146 if (this.extendedPersistenceContext != null) { 147 if (checkForTransaction(false) != null){ 148 this.extendedPersistenceContext.clear(); 149 }else{ 150 this.extendedPersistenceContext = null; 151 } 152 } 153 } else { 154 transaction.clear(); 155 } 156 } 157 158 164 public void persist(Object entity){ 165 verifyOpen(); 166 if (entity == null){ 167 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("not_an_entity", new Object [] {entity})); 168 } 169 try { 170 getActivePersistenceContext(checkForTransaction(!isExtended())).registerNewObjectForPersist(entity, new IdentityHashtable()); 171 } catch (RuntimeException e) { 172 this.transaction.setRollbackOnlyInternal(); 173 if (ValidationException.class.isAssignableFrom(e.getClass())){ 174 throw new EntityExistsException(e.getLocalizedMessage() , e); 175 } 176 throw e; 177 } 178 } 179 180 188 protected Object mergeInternal(Object entity){ 189 verifyOpen(); 190 if (entity == null){ 191 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("not_an_entity", new Object [] {entity})); 192 } 193 if (getActivePersistenceContext(checkForTransaction(!isExtended())).getDeletedObjects().contains(entity)){ 195 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("cannot_merge_removed_entity", new Object []{entity})); 196 } 197 try { 198 return getActivePersistenceContext(checkForTransaction(!isExtended())).mergeCloneWithReferences(entity, MergeManager.CASCADE_BY_MAPPING); 199 } catch (oracle.toplink.essentials.exceptions.OptimisticLockException ole) { 200 throw new javax.persistence.OptimisticLockException(ole); 201 } 202 } 203 204 209 public void remove(Object entity){ 210 verifyOpen(); 211 if (entity == null){ throw new IllegalArgumentException (ExceptionLocalization.buildMessage("not_an_entity", new Object [] {entity})); 213 } 214 try{ 215 getActivePersistenceContext(checkForTransaction(!isExtended())).performRemove(entity, new IdentityHashtable()); 216 }catch (RuntimeException e){ 217 this.transaction.setRollbackOnlyInternal(); 218 throw e; 219 } 220 } 221 222 230 public Object find(String entityName, Object primaryKey){ 231 verifyOpen(); 232 try{ 233 Session session = getActiveSession(); 234 ClassDescriptor descriptor = session.getDescriptorForAlias(entityName); 235 if (descriptor == null || descriptor.isAggregateDescriptor() || descriptor.isAggregateCollectionDescriptor()){ 236 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("unknown_entitybean_name", new Object [] {entityName})); 237 } 238 if (primaryKey == null){ throw new IllegalArgumentException (ExceptionLocalization.buildMessage("null_pk")); 240 } 241 if ( ((CMP3Policy)descriptor.getCMPPolicy()).getPKClass() != null && !((CMP3Policy)descriptor.getCMPPolicy()).getPKClass().isAssignableFrom(primaryKey.getClass())){ 242 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("invalid_pk_class", new Object [] {((CMP3Policy)descriptor.getCMPPolicy()).getPKClass(), primaryKey.getClass()})); 243 } 244 return findInternal(descriptor, session, primaryKey); 245 }catch (RuntimeException e){ 246 this.transaction.setRollbackOnlyInternal(); 247 throw e; 248 } 249 } 250 251 261 protected Object findInternal(Class entityClass, Object primaryKey) { 262 verifyOpen(); 263 Session session = getActiveSession(); 264 ClassDescriptor descriptor = session.getDescriptor(entityClass); 265 if (descriptor == null || descriptor.isAggregateDescriptor() || descriptor.isAggregateCollectionDescriptor()){ 266 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("unknown_bean_class", new Object []{ entityClass})); 267 } 268 if (primaryKey == null){ throw new IllegalArgumentException (ExceptionLocalization.buildMessage("null_pk")); 270 } 271 if ( ((CMP3Policy)descriptor.getCMPPolicy()).getPKClass() != null && !((CMP3Policy)descriptor.getCMPPolicy()).getPKClass().isAssignableFrom(primaryKey.getClass())){ 272 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("invalid_pk_class", new Object [] {((CMP3Policy)descriptor.getCMPPolicy()).getPKClass(), primaryKey.getClass()})); 273 } 274 return findInternal(descriptor, session, primaryKey); 275 } 276 277 288 protected static Object findInternal(ClassDescriptor descriptor, Session session, Object primaryKey){ 289 Vector pk; 290 if(primaryKey instanceof Vector ) { 291 pk = (Vector )primaryKey; 292 } else { 293 pk = ((CMP3Policy) descriptor.getCMPPolicy()).createPkVectorFromKey(primaryKey, (oracle.toplink.essentials.internal.sessions.AbstractSession)session); 294 } 295 ReadObjectQuery query = new ReadObjectQuery(descriptor.getJavaClass()); 296 query.setSelectionKey(pk); 297 query.conformResultsInUnitOfWork(); 298 return session.executeQuery(query); 299 } 300 301 305 public void flush(){ 306 verifyOpen(); 307 308 try { 309 getActivePersistenceContext(checkForTransaction(true)).writeChanges(); 310 }catch (RuntimeException e) { 311 this.transaction.setRollbackOnlyInternal(); 312 if (TopLinkException.class.isAssignableFrom(e.getClass())){ 313 throw new PersistenceException(e); 314 } 315 throw e; 316 } 317 } 318 319 protected void detectTransactionWrapper(){ 320 if (this.serverSession.hasExternalTransactionController()){ 321 setJTATransactionWrapper(); 322 } else { 323 setEntityTransactionWrapper(); 324 } 325 } 326 327 332 public void refresh(Object entity){ 333 verifyOpen(); 334 UnitOfWork uow = getActivePersistenceContext(checkForTransaction(!isExtended())); 335 if(!contains(entity, uow)) { 336 this.transaction.setRollbackOnlyInternal(); 337 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("cant_refresh_not_managed_object", new Object []{entity})); 338 } 339 ReadObjectQuery query = new ReadObjectQuery(); 340 query.setSelectionObject(entity); 341 query.refreshIdentityMapResult(); 342 query.cascadeByMapping(); 343 query.setLockMode(ObjectBuildingQuery.NO_LOCK); 344 Object refreshedEntity = null; 345 try{ 346 refreshedEntity = uow.executeQuery(query); 347 }catch (RuntimeException e){ 348 this.transaction.setRollbackOnlyInternal(); 349 throw e; 350 } 351 if(refreshedEntity == null) { 352 throw new EntityNotFoundException(ExceptionLocalization.buildMessage("entity_no_longer_exists_in_db", new Object []{entity})); 353 } 354 } 355 356 363 public boolean contains(Object entity){ 364 verifyOpen(); 365 if (entity == null){ 366 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("not_an_entity", new Object [] {entity})); 367 } 368 ClassDescriptor descriptor = (ClassDescriptor)getServerSession().getDescriptors().get(entity.getClass()); 369 if (descriptor == null || descriptor.isAggregateDescriptor() || descriptor.isAggregateCollectionDescriptor()){ 370 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("not_an_entity", new Object []{entity})); 371 } 372 373 if( (!hasActivePersistenceContext())) { 374 return false; 375 } 376 377 return contains(entity,getActivePersistenceContext(checkForTransaction(false))); 378 } 379 380 387 protected boolean contains(Object entity, UnitOfWork uow){ 388 389 return ((oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl)uow).isObjectRegistered(entity) && 390 !((oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl)uow).isObjectDeleted(entity); 391 } 392 393 399 public Session getActiveSession(){ 400 Object txn = checkForTransaction(false); 401 if ( txn == null && ! this.isExtended() ){ 402 return this.serverSession.acquireNonSynchronizedUnitOfWork(); 403 }else{ 404 return getActivePersistenceContext(txn); 405 } 406 407 } 408 409 414 public Object getDelegate(){ 415 return this; 416 } 417 418 421 public UnitOfWork getUnitOfWork(){ 422 return getActivePersistenceContext(checkForTransaction(false)); 423 } 424 425 428 public Session getSession(){ 429 if (checkForTransaction(false) == null){ 430 return this.serverSession.acquireNonSynchronizedUnitOfWork(); 431 } 432 return null; 433 } 434 435 438 public ServerSession getServerSession(){ 439 return this.serverSession; 440 } 441 445 protected DatabaseQuery createNativeQueryInternal(String sqlString, Class resultType){ 446 verifyOpen(); 447 ReadAllQuery query = new ReadAllQuery(resultType); 448 query.setSQLString(sqlString); 449 query.setIsUserDefined(true); 450 return query; 451 } 452 453 456 protected DatabaseQuery createQueryInternal(Expression expression, Class resultType){ 457 ReadAllQuery query = new ReadAllQuery(resultType); 458 query.setSelectionCriteria(expression); 459 return query; 460 } 461 462 463 475 public void close(){ 476 verifyOpen(); 477 isOpen = false; 478 try { 479 if (!transaction.shouldClose()){ 480 this.transaction.markLastTransaction(); 481 } 482 } finally { 483 factory = null; 484 serverSession = null; 485 } 486 } 487 488 489 492 public boolean isExtended(){ 493 return this.extended; 494 } 495 496 500 public boolean isOpen(){ 501 return isOpen && factory.isOpen(); 502 } 503 504 512 public void lock(Object entity, LockModeType lockMode){ 513 RepeatableWriteUnitOfWork context = getActivePersistenceContext(checkForTransaction(!isExtended())); 514 ClassDescriptor descriptor = context.getDescriptor(entity); 515 OptimisticLockingPolicy lockingPolicy = descriptor.getOptimisticLockingPolicy(); 516 if ((lockingPolicy == null) || !(lockingPolicy instanceof VersionLockingPolicy)){ 517 this.transaction.setRollbackOnlyInternal(); 518 throw new PersistenceException(ExceptionLocalization.buildMessage("ejb30-wrong-lock_called_without_version_locking-index", null)); 519 } 520 context.forceUpdateToVersionField(entity, (lockMode == LockModeType.WRITE)); 521 } 522 523 public void verifyOpen(){ 524 if (!isOpen()){ 525 throw new IllegalStateException (ExceptionLocalization.buildMessage("operation_on_closed_entity_manager")); 526 } 527 } 528 529 public RepeatableWriteUnitOfWork getActivePersistenceContext(Object txn) { 530 if (this.isExtended()){ 531 if (this.extendedPersistenceContext == null || !this.extendedPersistenceContext.isActive()){ 533 this.extendedPersistenceContext = new RepeatableWriteUnitOfWork(this.serverSession.acquireClientSession()); 534 this.extendedPersistenceContext.setResumeUnitOfWorkOnTransactionCompletion(true); 535 this.extendedPersistenceContext.setShouldCascadeCloneToJoinedRelationship(true); 536 if (txn != null) { 537 transaction.registerUnitOfWorkWithTxn(this.extendedPersistenceContext); 539 } 540 } 541 return this.extendedPersistenceContext; 542 }else{ 543 return getTransactionalUnitOfWork_new(txn); 544 } 545 } 546 547 551 private boolean hasActivePersistenceContext() 552 { 553 if (isExtended() && (this.extendedPersistenceContext == null || !this.extendedPersistenceContext.isActive())) 554 return false; 555 else 556 return true; 557 } 558 559 protected RepeatableWriteUnitOfWork getTransactionalUnitOfWork_new(Object tnx) { 560 return transaction.getTransactionalUnitOfWork(tnx); 561 } 562 563 protected Object checkForTransaction(boolean validateExistence) { 564 return transaction.checkForTransaction(validateExistence); 565 } 566 567 public boolean shouldFlushBeforeQuery(){ 568 Object foundTransaction = checkForTransaction(false); 569 if ((foundTransaction!=null) && transaction.shouldFlushBeforeQuery(getActivePersistenceContext(foundTransaction))){ 570 return true; 571 } 572 return false; 573 } 574 575 public boolean shouldPropagatePersistenceContext(){ 579 return this.propagatePersistenceContext; 580 } 581 582 591 public void joinTransaction(){ 592 transaction.registerUnitOfWorkWithTxn(getActivePersistenceContext(checkForTransaction(true))); 593 } 594 } 595 | Popular Tags |