1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import javax.ejb.EJBException ; 29 import javax.ejb.NoSuchObjectLocalException ; 30 import javax.ejb.TransactionRolledbackLocalException ; 31 import javax.transaction.Status ; 32 import javax.transaction.SystemException ; 33 import javax.transaction.Transaction ; 34 35 import org.objectweb.jonas_ejb.deployment.api.EntityDesc; 36 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 45 public class JEntitySwitchCRC extends JEntitySwitch { 46 47 50 protected JEntityContext itContext = null; 51 52 55 protected JEntityContext ihContext = null; 56 57 protected static int maxtime = 5000; 61 65 public JEntitySwitchCRC() { 66 lockpolicy = EntityDesc.LOCK_CONTAINER_READ_COMMITTED; 67 txUpdates = true; 68 } 69 70 protected void initpolicy(JEntityFactory bf) { 71 lazyregister = false; 72 } 73 74 protected JEntityContext getContext4Tx(Transaction tx) { 75 JEntityContext ctx = null; 76 if (tx == null) { 77 ctx = ihContext; 78 } else { 79 ctx = itContext; 80 } 81 return ctx; 82 } 83 84 88 protected void setContext4Tx(Transaction tx, JEntityContext ctx) { 89 if (tx == null) { 90 ihContext = ctx; 91 } else { 92 itContext = ctx; 93 } 94 } 95 96 99 protected void removeContext4Tx(Transaction tx) { 100 if (tx == null) { 102 ihContext = null; 103 } else { 104 itContext = null; 105 } 106 } 107 108 public void waitmyturn(Transaction tx) { 109 110 if (tx != null) { 112 int waitcount = 0; 116 while (runningtx != null && !tx.equals(runningtx)) { 117 if (TraceEjb.isDebugSynchro()) { 118 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IT: WAIT end IT"); 119 } 120 blockedtx = tx; 122 if (waitcount++ > 1 && bf.isBlocked(runningtx) && bf.isBlocking(tx)) { 123 try { 124 tx.setRollbackOnly(); 125 } catch (SystemException e) { 126 TraceEjb.logger.log(BasicLevel.ERROR, ident 127 + "getICtx IT: unexpected exception setting rollbackonly", e); 128 } 129 TraceEjb.logger.log(BasicLevel.WARN, ident + "getICtx IT: transaction rolled back"); 130 throw new TransactionRolledbackLocalException ("possible deadlock"); 131 } 132 waiters++; 133 try { 134 wait(maxtime); 135 if (TraceEjb.isDebugSynchro()) { 136 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IT: NOTIFIED"); 137 } 138 } catch (InterruptedException e) { 139 if (TraceEjb.isDebugSynchro()) { 140 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IT: INTERRUPTED", e); 141 } 142 } catch (Exception e) { 143 throw new EJBException ("JEntitySwitch synchronization pb", e); 144 } finally { 145 waiters--; 146 blockedtx = null; 147 } 148 int status = Status.STATUS_ROLLEDBACK; 150 try { 151 status = tx.getStatus(); 152 } catch (SystemException e) { 153 TraceEjb.logger.log(BasicLevel.ERROR, ident 154 + "getICtx IT: unexpected exception getting transaction status", e); 155 } 156 switch (status) { 157 case Status.STATUS_MARKED_ROLLBACK: 158 case Status.STATUS_ROLLEDBACK: 159 case Status.STATUS_ROLLING_BACK: 160 if (TraceEjb.isVerbose()) { 161 TraceEjb.logger.log(BasicLevel.WARN, ident + "getICtx IT: transaction rolled back"); 162 } 163 throw new TransactionRolledbackLocalException ("rollback occured while waiting"); 164 } 165 } 166 } 167 } 168 169 174 public synchronized boolean passivateIH(boolean passivation) { 175 176 if (ihContext == null && itContext == null) { 178 if (inactivityTimeout > 0 && 179 System.currentTimeMillis() - timestamp > inactivityTimeout) { 180 if (TraceEjb.isDebugContext()) { 181 TraceEjb.context.log(BasicLevel.DEBUG, "discard object on timeout"); 182 } 183 discardContext(null, true, false); 184 } 185 return true; 186 } 187 188 if (passivation) { 190 191 if (TraceEjb.isDebugSwapper()) { 192 TraceEjb.swapper.log(BasicLevel.DEBUG, ident); 193 } 194 195 JEntityContext jec = ihContext; 197 if (jec != null) { 198 if (TraceEjb.isDebugContext()) { 199 TraceEjb.context.log(BasicLevel.DEBUG, "passivated: " + jec); 200 } 201 jec.passivate(); 202 bf.releaseJContext(jec); 203 ihContext = null; 204 } 205 jec = itContext; 207 if (jec != null && runningtx == null) { 208 if (TraceEjb.isDebugContext()) { 209 TraceEjb.context.log(BasicLevel.DEBUG, "passivated: " + jec); 210 } 211 jec.passivate(); 212 bf.releaseJContext(jec); 213 itContext = null; 214 } 215 216 if (ihContext == null && itContext == null) { 218 timestamp = System.currentTimeMillis(); 219 } 220 221 if (waiters > 0) { 222 if (TraceEjb.isDebugSynchro()) { 223 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " notify"); 224 } 225 notifyAll(); 226 } 227 } 228 return true; 229 } 230 231 public synchronized void endIH() { 232 } 234 235 public synchronized void notifyWriting(Transaction tx, JEntityContext bctx) { 236 return; } 238 239 244 public int getState() { 245 if (ihContext != null) { 246 if (ihContext.isMarkedRemoved()) { 247 return 4; 248 } else { 249 if (inDirtyList) { 250 return 1; 251 } else { 252 if (itContext == null) { 253 return 2; 254 } 255 } 256 } 257 } 258 if (itContext != null) { 259 if (itContext.isMarkedRemoved()) { 260 return 4; 261 } else { 262 if (runningtx != null) { 263 return 0; 264 } else { 265 return 2; 266 } 267 } 268 } 269 return 3; 270 } 271 272 } 273 | Popular Tags |