1 21 package oracle.toplink.essentials.transaction; 23 24 import oracle.toplink.essentials.internal.sessions.AbstractSession; 25 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 26 import oracle.toplink.essentials.exceptions.TransactionException; 27 import oracle.toplink.essentials.logging.*; 28 import oracle.toplink.essentials.sessions.SessionProfiler; 29 30 43 public abstract class AbstractSynchronizationListener { 44 45 49 protected AbstractTransactionController controller; 50 51 54 protected AbstractSession session; 55 56 60 protected UnitOfWorkImpl unitOfWork; 61 62 65 protected Object transaction; 66 67 70 public AbstractSynchronizationListener() { 71 super(); 72 } 73 74 77 protected AbstractSynchronizationListener(UnitOfWorkImpl unitOfWork, AbstractSession session, Object transaction, AbstractTransactionController controller) { 78 this.session = session; 79 this.unitOfWork = unitOfWork; 80 this.transaction = transaction; 81 this.controller = controller; 82 } 83 84 92 public void beforeCompletion() { 93 UnitOfWorkImpl uow = getUnitOfWork(); 94 try { 95 Object status = getTransactionController().getTransactionStatus(); 96 getTransactionController().logTxStateTrace(uow, "TX_beforeCompletion", status); 97 session.startOperationProfile(SessionProfiler.JtsBeforeCompletion); 99 100 if (!uow.isActive()) { 102 throw TransactionException.inactiveUnitOfWork(uow); 103 } 104 105 if (!getTransactionController().canIssueSQLToDatabase_impl(status)) { 107 return; 108 } 109 110 if (getSession().isInTransaction()) { 112 getSession().getTransactionMutex().setActiveThread(Thread.currentThread()); 113 } 114 115 uow.issueSQLbeforeCompletion(); 117 118 uow.setPendingMerge(); 120 121 } catch (RuntimeException exception) { 122 uow.log(new SessionLogEntry(uow, SessionLog.WARNING, SessionLog.TRANSACTION, exception)); 124 handleException(exception); 126 } finally { 127 session.endOperationProfile(SessionProfiler.JtsBeforeCompletion); 128 } 129 } 130 131 141 public void afterCompletion(Object status) { 142 UnitOfWorkImpl uow = getUnitOfWork(); 143 try { 144 getTransactionController().logTxStateTrace(uow, "TX_afterCompletion", status); 146 session.startOperationProfile(SessionProfiler.JtsAfterCompletion); 148 if (!uow.isActive()) { 150 throw TransactionException.inactiveUnitOfWork(uow); 151 } 152 153 if (getTransactionController().canMergeUnitOfWork_impl(status)) { 155 uow.afterTransaction(true, true); if (uow.isMergePending()) { 157 uow.mergeClonesAfterCompletion(); 159 } 160 } else { 161 uow.afterTransaction(false, true); } 163 } catch (RuntimeException rtEx) { 164 uow.log(new SessionLogEntry(uow, SessionLog.WARNING, SessionLog.TRANSACTION, rtEx)); 166 throw rtEx; 168 } finally { 169 session.endOperationProfile(SessionProfiler.JtsAfterCompletion); 170 } 171 172 if (uow.shouldResumeUnitOfWorkOnTransactionCompletion() && getTransactionController().canMergeUnitOfWork_impl(status)){ 174 uow.synchronizeAndResume(); 175 uow.setSynchronized(false); 176 }else{ 177 uow.release(); 178 if (getSession().isClientSession()) { 180 getSession().release(); 181 } 182 } 183 getTransactionController().removeUnitOfWork(getTransaction()); 184 setUnitOfWork(null); 185 setTransaction(null); 186 setSession(null); 187 } 188 189 201 public void handleException(RuntimeException exception) { 202 throw exception; 205 } 206 207 protected AbstractTransactionController getTransactionController() { 208 return controller; 209 } 210 211 protected void setTransactionController(AbstractTransactionController newController) { 212 controller = newController; 213 } 214 215 protected Object getTransaction() { 216 return transaction; 217 } 218 219 protected void setTransaction(Object transaction) { 220 this.transaction = transaction; 221 } 222 223 protected AbstractSession getSession() { 224 return session; 225 } 226 227 protected void setSession(AbstractSession session) { 228 this.session = session; 229 } 230 231 protected UnitOfWorkImpl getUnitOfWork() { 232 return unitOfWork; 233 } 234 235 protected void setUnitOfWork(UnitOfWorkImpl unitOfWork) { 236 this.unitOfWork = unitOfWork; 237 } 238 } | Popular Tags |