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 JEntitySwitchCS extends JEntitySwitch { 46 47 50 protected JEntityContext itContext = null; 51 52 protected static int maxtime = 3000; 56 60 public JEntitySwitchCS() { 61 lockpolicy = EntityDesc.LOCK_CONTAINER_SERIALIZED; 62 txUpdates = false; } 64 65 protected void initpolicy(JEntityFactory bf) { 66 lazyregister = false; 67 } 68 69 protected JEntityContext getContext4Tx(Transaction tx) { 70 return itContext; 71 } 72 73 protected void setContext4Tx(Transaction tx, JEntityContext ctx) { 74 if (TraceEjb.isDebugContext()) { 75 TraceEjb.context.log(BasicLevel.DEBUG, "set itContext=" + ctx); 76 } 77 itContext = ctx; 78 } 79 80 protected void removeContext4Tx(Transaction tx) { 81 if (TraceEjb.isDebugContext()) { 82 TraceEjb.context.log(BasicLevel.DEBUG, "unset itContext=" + itContext); 83 } 84 itContext = null; 85 } 86 87 public void waitmyturn(Transaction tx) { 88 if (tx == null) { 90 while (runningtx != null) { 92 if (TraceEjb.isDebugSynchro()) { 93 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IH: WAIT end IT"); 94 } 95 waiters++; 96 try { 97 wait(maxtime); 98 if (TraceEjb.isDebugSynchro()) 99 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IH: NOTIFIED"); 100 } catch (InterruptedException e) { 101 if (TraceEjb.isDebugSynchro()) 102 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IH: INTERRUPTED"); 103 } catch (Exception e) { 104 throw new EJBException ("JEntitySwitch synchronization pb", e); 105 } finally { 106 waiters--; 107 } 108 } 109 } else { 110 111 int waitcount = 0; 115 while (inDirtyList || (runningtx != null && !tx.equals(runningtx))) { 116 if (inDirtyList) { 120 if (TraceEjb.isDebugSynchro()) 121 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IT: WAIT end IH"); 122 bf.synchronizeEntities(); 125 } else { 126 if (TraceEjb.isDebugSynchro()) 127 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IT: WAIT end IT"); 128 blockedtx = tx; 130 if (waitcount++ > 1 && bf.isBlocked(runningtx) && bf.isBlocking(tx)) { 131 try { 132 tx.setRollbackOnly(); 133 } catch (SystemException e) { 134 TraceEjb.logger.log(BasicLevel.ERROR, ident 135 + "getICtx IT: unexpected exception setting rollbackonly"); 136 } 137 TraceEjb.logger.log(BasicLevel.WARN, ident + "getICtx IT: transaction rolled back"); 138 throw new TransactionRolledbackLocalException ("possible deadlock"); 139 } 140 } 141 waiters++; 142 try { 143 wait(maxtime); 144 if (TraceEjb.isDebugSynchro()) 145 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IT: NOTIFIED"); 146 } catch (InterruptedException e) { 147 if (TraceEjb.isDebugSynchro()) 148 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IT: INTERRUPTED"); 149 } catch (Exception e) { 150 throw new EJBException ("JEntitySwitch synchronization pb", e); 151 } finally { 152 waiters--; 153 blockedtx = null; 154 } 155 int status = Status.STATUS_ROLLEDBACK; 158 try { 159 status = tx.getStatus(); 160 } catch (SystemException e) { 161 TraceEjb.logger.log(BasicLevel.ERROR, ident 162 + "getICtx IT: unexpected exception getting transaction status"); 163 } 164 switch (status) { 165 case Status.STATUS_MARKED_ROLLBACK: 166 case Status.STATUS_ROLLEDBACK: 167 case Status.STATUS_ROLLING_BACK: 168 TraceEjb.logger.log(BasicLevel.WARN, ident + "getICtx IT: transaction rolled back"); 169 throw new TransactionRolledbackLocalException ("rollback occured while waiting"); 170 } 171 } 172 } 173 174 } 175 176 181 public synchronized boolean passivateIH(boolean passivation) { 182 183 JEntityContext jec = getContext4Tx(null); 184 185 if (jec == null) { 187 if (inactivityTimeout > 0) { 188 long diff = System.currentTimeMillis() - timestamp; 189 if (diff > inactivityTimeout) { 190 if (TraceEjb.isDebugContext()) { 191 TraceEjb.context.log(BasicLevel.DEBUG, ident + timestamp); 192 } 193 discardContext(null, true, false); 194 } 195 } 196 return true; 197 } 198 199 if (countIH > 0 && !txUpdates) { 201 if (TraceEjb.isDebugSynchro()) TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " used off TX"); 205 mustStore = true; 206 return true; 207 } 208 if (runningtx != null) { 209 if (TraceEjb.isDebugSynchro()) TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " used in TX"); 210 return false; 211 } 212 213 if (TraceEjb.isDebugSynchro()) TraceEjb.synchro.log(BasicLevel.DEBUG, ident); 214 215 if (jec.isMarkedRemoved()) { 216 discardContext(null, true, true); 217 return true; 218 } 219 220 if (!todiscard && !txUpdates) { 223 try { 224 jec.storeIfModified(); 225 } catch (Exception e) { 226 TraceEjb.logger.log(BasicLevel.ERROR, ident, "error while storing bean state:", e); 227 } 228 mustStore = false; 229 } 232 233 if (passivation) { 235 if (TraceEjb.isDebugContext()) TraceEjb.context.log(BasicLevel.DEBUG, "passivated: " + jec); 236 jec.passivate(); 237 bf.releaseJContext(jec); 238 removeContext4Tx(null); 239 timestamp = System.currentTimeMillis(); 241 if (TraceEjb.isDebugContext()) { 242 TraceEjb.context.log(BasicLevel.DEBUG, ident + timestamp); 243 } 244 if (waiters > 0) { 245 if (TraceEjb.isDebugSynchro()) { 246 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " notify"); 247 } 248 notifyAll(); 249 } 250 } 251 return true; 252 } 253 254 257 public synchronized void endIH() { 258 inDirtyList = false; 259 if (getContext4Tx(null) == null) { 260 if (TraceEjb.isDebugSynchro()) TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " discarded!"); 261 return; 262 } 263 if (TraceEjb.isDebugSynchro()) { 266 if (countIH == 0) { 267 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " ready again"); 268 } else { 269 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " busy!"); 270 } 271 } 272 if (waiters > 0) { 273 if (TraceEjb.isDebugSynchro()) { 274 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " notify"); 275 } 276 notifyAll(); 277 } 278 } 279 280 public synchronized void notifyWriting(Transaction tx, JEntityContext bctx) { 281 return; } 283 284 289 public int getState() { 290 if (itContext != null) { 291 if (itContext.isMarkedRemoved()) { 292 return 4; 293 } else { 294 if (runningtx != null) { 295 return 0; 296 } else { 297 if (inDirtyList) { 298 return 1; 299 } else { 300 return 2; 301 } 302 } 303 } 304 } 305 return 3; 306 } 307 308 } 309 | Popular Tags |