00001
00026 package org.objectweb.jonas_ejb.container;
00027
00028 import java.util.HashMap;
00029 import java.util.Iterator;
00030
00031 import javax.ejb.EJBException;
00032 import javax.transaction.SystemException;
00033 import javax.transaction.Transaction;
00034
00035 import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
00036
00037 import org.objectweb.util.monolog.api.BasicLevel;
00038
00044 public class JEntitySwitchDB extends JEntitySwitch {
00045
00049 protected JEntityContext ihContext = null;
00050
00054 protected HashMap itsContext;
00055
00060 public JEntitySwitchDB() {
00061 lockpolicy = EntityDesc.LOCK_DATABASE;
00062 txUpdates = true;
00063 itsContext = new HashMap();
00064 }
00065
00066 protected void initpolicy(JEntityFactory bf) {
00067
00068 lazyregister = !bf.isPrefetch() && !shared;
00069 }
00070
00075 protected JEntityContext getContext4Tx(Transaction tx) {
00076 if (tx == null) {
00077 return ihContext;
00078 } else {
00079 return (JEntityContext) itsContext.get(tx);
00080 }
00081 }
00082
00087 protected void setContext4Tx(Transaction tx, JEntityContext ctx) {
00088 if (tx == null) {
00089 ihContext = ctx;
00090 } else {
00091 if (TraceEjb.isDebugSwapper()) TraceEjb.swapper.log(BasicLevel.DEBUG, ident + " add new Context");
00092 itsContext.put(tx, ctx);
00093 }
00094 }
00095
00099 protected void removeContext4Tx(Transaction tx) {
00100
00101 if (tx == null) {
00102 ihContext = null;
00103 } else {
00104 if (TraceEjb.isDebugSwapper()) TraceEjb.swapper.log(BasicLevel.DEBUG, ident + " get rid of this Context");
00105 itsContext.remove(tx);
00106 }
00107 }
00108
00109 public void waitmyturn(Transaction tx) {
00110
00111 }
00112
00113 public boolean passivateIH(boolean passivation) {
00114
00115 if (ihContext == null && itsContext.size() == 0) {
00116 if (inactivityTimeout > 0 &&
00117 System.currentTimeMillis() - timestamp > inactivityTimeout) {
00118 TraceEjb.context.log(BasicLevel.DEBUG, "discard object on timeout");
00119 discardContext(null, true, false);
00120 }
00121 return true;
00122 }
00123
00124
00125 if (passivation) {
00126 if (TraceEjb.isDebugSwapper()) TraceEjb.swapper.log(BasicLevel.DEBUG, ident);
00127
00128 JEntityContext jec = ihContext;
00129 if (jec != null) {
00130 if (TraceEjb.isDebugContext()) TraceEjb.context.log(BasicLevel.DEBUG, "passivated: " + jec);
00131 jec.passivate();
00132 bf.releaseJContext(jec);
00133 ihContext = null;
00134 }
00135
00136
00137 if (itsContext.size() == 0) {
00138 timestamp = System.currentTimeMillis();
00139 }
00140
00141
00142
00143 if (waiters > 0) {
00144 TraceEjb.synchro.log(BasicLevel.DEBUG, ident + " notify");
00145 notifyAll();
00146 }
00147 }
00148 return true;
00149 }
00150
00151 public void endIH() {
00152 return;
00153 }
00154
00159 public synchronized void notifyWriting(Transaction tx, JEntityContext bctx) {
00160 TraceEjb.synchro.log(BasicLevel.DEBUG, ident);
00161 if (writingtx != null) {
00162 TraceEjb.logger.log(BasicLevel.WARN, "Conflict: 2 transactions attempt to write the same instance");
00163 try {
00164 tx.setRollbackOnly();
00165 } catch (SystemException e) {
00166 TraceEjb.logger.log(BasicLevel.ERROR, "cannot setRollbackOnly");
00167 }
00168 } else {
00169 if (lazyregister) {
00170
00171
00172
00173 registerCtx(tx, bctx);
00174 }
00175 writingtx = tx;
00176 }
00177 }
00178
00184 public int getState() {
00185 if (ihContext != null) {
00186 if (ihContext.isMarkedRemoved()) {
00187 return 4;
00188 } else {
00189 if (inDirtyList) {
00190 return 1;
00191 } else {
00192 return 2;
00193 }
00194 }
00195 }
00196 if (itsContext.size() > 0) {
00197 return 0;
00198 }
00199 return 3;
00200 }
00201
00202 }