1 21 package oracle.toplink.essentials.internal.ejb.cmp3.transaction.base; 23 24 import oracle.toplink.essentials.transaction.AbstractTransactionController; 25 import oracle.toplink.essentials.exceptions.TransactionException; 26 import oracle.toplink.essentials.internal.ejb.cmp3.base.*; 27 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 28 29 40 public class JTATransactionWrapper extends TransactionWrapperImpl { 41 42 protected AbstractTransactionController txnController; 44 45 protected Object lastTransaction; 46 47 public JTATransactionWrapper(EntityManagerImpl entityManager) { 48 super(entityManager); 49 this.txnController = (AbstractTransactionController)entityManager.getServerSession().getExternalTransactionController(); 50 } 51 52 58 public Object checkForTransaction(boolean validateExistence){ 59 Object transaction = this.txnController.getTransaction(); 60 if (validateExistence && (transaction == null)){ 61 throwCheckTransactionFailedException(); 62 } 63 return transaction; 64 } 65 66 70 public void clear(){ 71 if (txnKey != null && this.entityManager.shouldPropagatePersistenceContext()){ 72 this.txnController.getUnitsOfWork().remove(txnKey); 73 } 74 localUOW.release(); 75 localUOW = null; 76 } 77 78 84 public RepeatableWriteUnitOfWork getTransactionalUnitOfWork(Object transaction){ 85 if (transaction == null){ 86 return null; 87 } 88 if (this.entityManager.shouldPropagatePersistenceContext()){ 89 Object newTxnKey = this.txnController.getTransactionKey(transaction); 90 if (this.txnKey == newTxnKey){ 91 return (RepeatableWriteUnitOfWork)this.localUOW; 92 } 93 this.txnKey = newTxnKey; 94 this.localUOW = (RepeatableWriteUnitOfWork)this.txnController.lookupActiveUnitOfWork(transaction); 95 if (this.localUOW == null){ 96 this.localUOW = new RepeatableWriteUnitOfWork(entityManager.getServerSession().acquireClientSession()); 97 this.localUOW.registerWithTransactionIfRequired(); 98 this.localUOW.setShouldCascadeCloneToJoinedRelationship(true); 99 this.txnController.getUnitsOfWork().put(newTxnKey, this.localUOW); 100 } 101 }else if (this.localUOW == null){ 102 this.localUOW = new RepeatableWriteUnitOfWork(entityManager.getServerSession().acquireClientSession()); 103 this.localUOW.registerWithTransactionIfRequired(); 104 this.localUOW.setShouldCascadeCloneToJoinedRelationship(true); 105 } 106 return (RepeatableWriteUnitOfWork)this.localUOW; 107 } 108 109 protected void throwUserTransactionException() { 110 throw TransactionException.entityTransactionWithJTANotAllowed(); 111 } 112 113 protected void throwCheckTransactionFailedException() { 114 throw TransactionException.externalTransactionNotActive(); 115 } 116 117 public void registerUnitOfWorkWithTxn(UnitOfWorkImpl uow){ 118 uow.registerWithTransactionIfRequired(); 119 } 120 121 public boolean shouldClose() { 122 if (!txnController.noTransactionOrRolledBackOrCommited()) { 123 return false; 124 } 125 return true; 126 } 127 128 132 public boolean shouldFlushBeforeQuery(UnitOfWorkImpl uow){ 133 return uow.isSynchronized(); 134 } 135 136 141 public void markLastTransaction() { 142 if(lastTransaction == null) { 143 lastTransaction = txnController.getTransaction(); 144 } 145 } 146 public boolean hasLastTransactionCompleted() { 147 if(lastTransaction != null) { 148 Object transaction = txnController.getTransaction(); 149 if(transaction != null) { 150 return !lastTransaction.equals(transaction); 151 } else { 152 return true; 153 } 154 } else { 155 return false; 156 } 157 } 158 } 159 | Popular Tags |