JEntitySwitchCRU.java

00001 
00026 package org.objectweb.jonas_ejb.container;
00027 
00028 import javax.ejb.EJBException;
00029 import javax.transaction.SystemException;
00030 import javax.transaction.Transaction;
00031 
00032 import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
00033 
00034 import org.objectweb.util.monolog.api.BasicLevel;
00035 
00042 public class JEntitySwitchCRU extends JEntitySwitch {
00043 
00047     protected JEntityContext itContext = null;
00048 
00053     public JEntitySwitchCRU() {
00054         lockpolicy = EntityDesc.LOCK_CONTAINER_READ_UNCOMMITTED;
00055         txUpdates = true;
00056     }
00057 
00058     protected void initpolicy(JEntityFactory bf) {
00059         lazyregister = true;
00060     }
00061 
00062     protected JEntityContext getContext4Tx(Transaction tx) {
00063         return itContext;
00064     }
00065 
00066     protected void setContext4Tx(Transaction tx, JEntityContext ctx) {
00067         itContext = ctx;
00068     }
00069 
00070     protected void removeContext4Tx(Transaction tx) {
00071         itContext = null;
00072     }
00073 
00074     public void waitmyturn(Transaction tx) {
00075         // No synchro for this policy.
00076     }
00077 
00087     public synchronized JEntityContext mapICtx(Transaction tx, JEntityContext bctx, boolean forced, boolean holdit, boolean notused) {
00088 
00089         waitmyturn(tx);
00090 
00091         // Choose the context to use.
00092         boolean newtrans = false;
00093         JEntityContext jec = itContext;
00094         if (forced) {
00095             // If the new context is enforced, we must first release the older
00096             if (jec != null) {
00097                 TraceEjb.context.log(BasicLevel.DEBUG, ident + "new context is enforced!");
00098                 discardContext(tx, false, true);
00099             }
00100             jec = bctx;
00101             itContext = jec;
00102             jec.initEntityContext(this);
00103             newtrans = true;
00104         } else {
00105             if (jec != null) {
00106                 // Reuse the Context for this transaction.
00107                 // If a context was supplied, release it first.
00108                 if (bctx != null) {
00109                     if (TraceEjb.isDebugContext())
00110                             TraceEjb.context.log(BasicLevel.DEBUG, ident + "a context was supplied!");
00111                     bf.releaseJContext(bctx);
00112                 }
00113                 newtrans = true; // TODO: not always a new tx (if more than 1
00114                                  // modifying call on this)
00115                 jec.reuseEntityContext(newtrans);
00116             } else {
00117                 if (bctx != null) {
00118                     jec = bctx;
00119                 } else {
00120                     // no Context available : get one from the pool.
00121                     jec = (JEntityContext) bf.getJContext(this);
00122                 }
00123                 jec.initEntityContext(this);
00124                 jec.activate(true);
00125                 itContext = jec; // after activate
00126                 newtrans = true;
00127             }
00128         }
00129 
00130         if (tx == null) {
00131             if (holdit) {
00132                 countIH++;
00133                 if (TraceEjb.isDebugSynchro())
00134                         TraceEjb.synchro.log(BasicLevel.DEBUG, ident + "mapICtx IH count=" + countIH);
00135                 if (shared && countIH == 1) {
00136                     // reload state that could have been modified by
00137                     // transactions.
00138                     jec.activate(false);
00139                 }
00140             }
00141         } else {
00142             if (holdit) {
00143                 countIT++;
00144             }
00145         }
00146 
00147         return jec;
00148     }
00149 
00153     public boolean passivateIH(boolean passivation) {
00154         JEntityContext jec = getContext4Tx(null);
00155         // If already passivated, look if we can destroy the objects
00156         if (jec == null) {
00157             if (inactivityTimeout > 0 &&
00158                 System.currentTimeMillis() - timestamp > inactivityTimeout) {
00159                 TraceEjb.context.log(BasicLevel.DEBUG, "discard object on timeout");
00160                 discardContext(null, true, false);
00161             }
00162             return true;
00163         }
00164         // passivate this instance if required.
00165         if (passivation) {
00166             if (TraceEjb.isDebugContext()) TraceEjb.context.log(BasicLevel.DEBUG, "TODO: passivate: " + jec);
00167         }
00168         return true;
00169     }
00170 
00171     public void endIH() {
00172         return; // NEVER
00173     }
00174 
00179     public synchronized void notifyWriting(Transaction tx, JEntityContext bctx) {
00180         TraceEjb.synchro.log(BasicLevel.DEBUG, ident);
00181         if (writingtx != null) {
00182             TraceEjb.logger.log(BasicLevel.WARN, "Conflict: 2 transactions attempt to write the same instance");
00183             try {
00184                 tx.setRollbackOnly();
00185             } catch (SystemException e) {
00186                 TraceEjb.logger.log(BasicLevel.ERROR, "cannot set current transaction RollbackOnly");
00187             }
00188             // Since we share the same instance, we must rollback also
00189             // the current transaction (else data would be corrupted).
00190             try {
00191                 writingtx.setRollbackOnly();
00192             } catch (SystemException e) {
00193                 TraceEjb.logger.log(BasicLevel.ERROR, "cannot set other transaction RollbackOnly");
00194             }
00195             // We should also avoid modifying instance, in case writingtx is
00196             // already committing (setRollbackOnly will fail)
00197             throw new EJBException("Conflict writing entity bean");
00198         } else {
00199             // Instance must be written later.
00200             registerCtx(tx, bctx);
00201             writingtx = tx;
00202         }
00203     }
00204 
00210     public int getState() {
00211         if (itContext != null) {
00212             if (itContext.isMarkedRemoved()) {
00213                 return 4;
00214             } else {
00215                 if (writingtx != null) {
00216                     return 0;
00217                 } else {
00218                     return 1;
00219                 }
00220             }
00221         }
00222         return 3;
00223     }
00224 
00225 }

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