1 21 package oracle.toplink.essentials.internal.ejb.cmp3.transaction.base; 23 24 import javax.persistence.RollbackException; 25 26 import oracle.toplink.essentials.exceptions.TransactionException; 27 import oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork; 28 import oracle.toplink.essentials.internal.localization.ExceptionLocalization; 29 30 41 public class EntityTransactionImpl { 42 43 protected EntityTransactionWrapper wrapper; 44 45 protected boolean active = false; 46 47 protected Boolean hasLastTransactionCompleted; 48 49 protected boolean rollbackOnly = false; 50 51 public EntityTransactionImpl(EntityTransactionWrapper wrapper) { 52 this.wrapper = wrapper; 53 } 54 59 public void begin(){ 60 if (isActive()){ 61 throw new IllegalStateException (TransactionException.transactionIsActive().getMessage()); 62 } 63 if (this.wrapper.getEntityManager().isExtended()){ 64 this.wrapper.localUOW = this.wrapper.getEntityManager().getActivePersistenceContext(null); 66 this.wrapper.localUOW.setShouldTerminateTransaction(false); 67 }else{ 68 this.wrapper.localUOW = new RepeatableWriteUnitOfWork(this.wrapper.getEntityManager().getServerSession().acquireClientSession()); 69 this.wrapper.localUOW.setShouldTerminateTransaction(false); 70 this.wrapper.localUOW.setShouldCascadeCloneToJoinedRelationship(true); 71 } 72 this.active = true; 73 } 74 75 80 public void commit(){ 81 if (!isActive()){ 82 throw new IllegalStateException (TransactionException.transactionNotActive().getMessage()); 83 } 84 try { 85 transactionCompleted(); 86 if (this.wrapper.localUOW != null){ 87 this.wrapper.localUOW.setShouldTerminateTransaction(true); 88 if (! this.rollbackOnly){ 89 if (this.wrapper.localUOW.shouldResumeUnitOfWorkOnTransactionCompletion()){ 90 this.wrapper.localUOW.commitAndResume(); 91 return; 92 }else{ 93 this.wrapper.localUOW.commit(); 94 } 95 } 96 wrapper.getLocalUnitOfWork().release(); 97 wrapper.getLocalUnitOfWork().getParent().release(); 98 if (this.rollbackOnly){ 99 wrapper.getEntityManager().clear(); 100 throw new RollbackException(ExceptionLocalization.buildMessage("rollback_because_of_rollback_only")); 101 } 102 } 103 }catch (RuntimeException ex){ 104 if (this.wrapper.localUOW != null){ 105 wrapper.getEntityManager().clear(); 106 this.wrapper.localUOW.release(); 107 this.wrapper.localUOW.getParent().release(); 108 } 109 throw new RollbackException(ex); 110 } finally { 111 this.active = false; 112 this.rollbackOnly = false; 113 wrapper.setLocalUnitOfWork(null); 114 } 115 } 116 117 123 public void rollback(){ 124 if (!isActive()){ 125 throw new IllegalStateException (TransactionException.transactionNotActive().getMessage()); 126 } 127 try{ 128 transactionCompleted(); 129 if (wrapper.getLocalUnitOfWork() != null){ 130 this.wrapper.localUOW.setShouldTerminateTransaction(true); 131 this.wrapper.localUOW.release(); 132 this.wrapper.localUOW.getParent().release(); 133 } 134 }finally{ 135 this.active = false; 136 this.rollbackOnly = false; 137 wrapper.getEntityManager().clear(); 138 wrapper.setLocalUnitOfWork(null); 139 } 140 } 141 142 148 public void setRollbackOnly(){ 149 if (!isActive()){ 150 throw new IllegalStateException (TransactionException.transactionNotActive().getMessage()); 151 } 152 this.rollbackOnly = true; 153 } 154 155 160 protected void finalize() throws Throwable { 161 try{ 162 if (isActive()) 163 this.rollback(); 164 }finally{ 165 super.finalize(); 166 } 167 } 168 173 public boolean getRollbackOnly(){ 174 if (!isActive()){ 175 throw new IllegalStateException (TransactionException.transactionNotActive().getMessage()); 176 } 177 return this.rollbackOnly; 178 } 179 180 183 public boolean isActive(){ 184 return this.active; 185 } 186 187 192 public void markLastTransaction() { 193 if(hasLastTransactionCompleted == null) { 194 hasLastTransactionCompleted = Boolean.FALSE; 195 } 196 } 197 public boolean hasLastTransactionCompleted() { 198 return hasLastTransactionCompleted != null && hasLastTransactionCompleted.booleanValue(); 199 } 200 protected void transactionCompleted() { 201 if(hasLastTransactionCompleted != null) { 202 hasLastTransactionCompleted = Boolean.TRUE; 203 } 204 } 205 206 } 207 | Popular Tags |