JEntitySwitchDB.java

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         // should be false, because usually this policy implies shared=true.
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         // free this Context
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         // synchro done by database.
00111     }
00112 
00113     public boolean passivateIH(boolean passivation) {
00114         // If already passivated, look if we can destroy the objects
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         // passivate this instance if required.
00125         if (passivation) {
00126             if (TraceEjb.isDebugSwapper()) TraceEjb.swapper.log(BasicLevel.DEBUG, ident);
00127             // Instance used when no transaction.
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             // If passivation done, set the timestamp
00137             if (itsContext.size() == 0) {
00138                 timestamp = System.currentTimeMillis();
00139             }
00140 
00141             // Instances used in transaction are already passivated
00142             // when the transaction is over. (See JEntitySwitch.java)
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                 // Instance must be written later.
00171                 // In case of prefetch, this has already be done (=
00172                 // !lazyregister)
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 }

Generated on Tue Feb 15 15:05:38 2005 for JOnAS by  doxygen 1.3.9.1