1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 31 import javax.ejb.EJBException ; 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 44 public class JEntitySwitchDB extends JEntitySwitch { 45 46 49 protected JEntityContext ihContext = null; 50 51 54 protected HashMap itsContext; 55 56 60 public JEntitySwitchDB() { 61 lockpolicy = EntityDesc.LOCK_DATABASE; 62 txUpdates = true; 63 itsContext = new HashMap (); 64 } 65 66 protected void initpolicy(JEntityFactory bf) { 67 lazyregister = !bf.isPrefetch() && !shared; 69 } 70 71 75 protected JEntityContext getContext4Tx(Transaction tx) { 76 if (tx == null) { 77 return ihContext; 78 } else { 79 return (JEntityContext) itsContext.get(tx); 80 } 81 } 82 83 87 protected void setContext4Tx(Transaction tx, JEntityContext ctx) { 88 if (tx == null) { 89 ihContext = ctx; 90 } else { 91 if (TraceEjb.isDebugSwapper()) TraceEjb.swapper.log(BasicLevel.DEBUG, ident + " add new Context"); 92 itsContext.put(tx, ctx); 93 } 94 } 95 96 99 protected void removeContext4Tx(Transaction tx) { 100 if (tx == null) { 102 ihContext = null; 103 } else { 104 if (TraceEjb.isDebugSwapper()) TraceEjb.swapper.log(BasicLevel.DEBUG, ident + " get rid of this Context"); 105 itsContext.remove(tx); 106 } 107 } 108 109 public void waitmyturn(Transaction tx) { 110 } 112 113 public boolean passivateIH(boolean passivation) { 114 if (ihContext == null && itsContext.size() == 0) { 116 if (inactivityTimeout > 0 && 117 System.currentTimeMillis() - timestamp > inactivityTimeout) { 118 if (TraceEjb.isDebugContext()) { 119 TraceEjb.context.log(BasicLevel.DEBUG, "discard object on timeout"); 120 } 121 discardContext(null, true, false); 122 } 123 return true; 124 } 125 126 if (passivation) { 128 if (TraceEjb.isDebugSwapper()) TraceEjb.swapper.log(BasicLevel.DEBUG, ident); 129 JEntityContext jec = ihContext; 131 if (jec != null) { 132 if (TraceEjb.isDebugContext()) TraceEjb.context.log(BasicLevel.DEBUG, "passivated: " + jec); 133 jec.passivate(); 134 bf.releaseJContext(jec); 135 ihContext = null; 136 } 137 138 if (itsContext.size() == 0) { 140 timestamp = System.currentTimeMillis(); 141 } 142 143 if (waiters > 0) { 146 if (TraceEjb.isDebugSynchro()) { 147 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " notify"); 148 } 149 notifyAll(); 150 } 151 } 152 return true; 153 } 154 155 public void endIH() { 156 return; 157 } 158 159 165 public JEntityContext getICtx(Transaction tx, boolean checkr) { 166 return mapICtx(tx, null, false, true, false); 169 } 170 171 175 public synchronized void notifyWriting(Transaction tx, JEntityContext bctx) { 176 if (TraceEjb.isDebugSynchro()) { 177 TraceEjb.synchro.log(BasicLevel.DEBUG, ident); 178 } 179 if (writingtx != null) { 180 if (!tx.equals(writingtx)) { 181 TraceEjb.logger.log(BasicLevel.WARN, bf.getEJBName() + " Conflict: 2 transactions attempt to write the same instance"); 182 try { 183 tx.setRollbackOnly(); 184 } catch (SystemException e) { 185 TraceEjb.logger.log(BasicLevel.ERROR, "cannot setRollbackOnly"); 186 } 187 } 188 } else { 189 if (lazyregister) { 190 registerCtx(tx, bctx); 194 } 195 writingtx = tx; 196 } 197 } 198 199 204 public int getState() { 205 if (ihContext != null) { 206 if (ihContext.isMarkedRemoved()) { 207 return 4; 208 } else { 209 if (inDirtyList) { 210 return 1; 211 } else { 212 return 2; 213 } 214 } 215 } 216 if (itsContext.size() > 0) { 217 return 0; 218 } 219 return 3; 220 } 221 222 } 223 | Popular Tags |