| * <p><b>2001/08/06: marcf</b>
| * <ol>
| * <li>Got rid of disassociate and added a
|javax.transaction.Synchronization. The sync will clean up the map now.
| - * <li>This class now interacts with ApplicationTxEntityMap
|available in Application
| + * <li>This class now interacts with GlobalTxEntityMap available.
Bill I appreciate you giving me credit for this but I didn't do it,
please put the revisions rights
marcf
| * </ol>
| */
| public class TxEntityMap
| @@ -47,7 +48,7 @@
| m_map.put(tx, entityMap);
| tx.registerSynchronization(new TxEntityMapCleanup(this, tx));
| }
| -
|entity.getContainer().getApplication().getTxEntityMap().associate(t
|x, entity);
| + EntityContainer.getGlobalTxEntityMap().associate(tx, entity);
| entityMap.put(entity.getCacheKey(), entity);
| }
|
| @@ -58,6 +59,9 @@
| return (EntityEnterpriseContext)entityMap.get(key);
| }
|
| + /**
| + * Cleanup tx/entity map on tx commit/rollback
| + */
| private class TxEntityMapCleanup implements Synchronization
| {
| TxEntityMap map;
|
|
|
| 1.51 +94 -12 jboss/src/main/org/jboss/ejb/EntityContainer.java
|
| Index: EntityContainer.java
| ===================================================================
| RCS file:
|/cvsroot/jboss/jboss/src/main/org/jboss/ejb/EntityContainer.java,v
| retrieving revision 1.50
| retrieving revision 1.51
| diff -u -r1.50 -r1.51
| --- EntityContainer.java 2001/08/06 16:26:27 1.50
| +++ EntityContainer.java 2001/08/07 18:06:42 1.51
| @@ -27,6 +27,7 @@
| import javax.ejb.CreateException;
| import javax.ejb.FinderException;
| import javax.ejb.RemoveException;
| +import javax.transaction.Transaction;
|
| import org.jboss.logging.Logger;
| import org.jboss.monitor.StatisticsProvider;
| @@ -46,7 +47,7 @@
| * @author <a href="mailto:[EMAIL PROTECTED]">Daniel OConnor</a>
| * @author <a href="[EMAIL PROTECTED]">Bill Burke</a>
| * @author <a
|href="mailto:[EMAIL PROTECTED]">Andreas Schaefer</a>
| - * @version $Revision: 1.50 $
| + * @version $Revision: 1.51 $
| *
| * <p><b>Revisions:</b>
| *
| @@ -58,6 +59,11 @@
| * <ul>
| * <li>- Added statistics gathering
| * </ul>
| + * <p><b>20010807 bill burke:</b>
| + * <ul>
| + * <li> Moved storeEntity from EntitySynchronization to here so
|other classes can use it.
| + * <li> Moved synchronizeEntitiesWithinTransaction to here from
|Application as a static method.
| + * </ul>
| */
| public class EntityContainer
| extends Container
| @@ -109,6 +115,48 @@
| protected long createCount = 0;
| protected long removeCount = 0;
|
| + /**
| + * Optional isModified method used by storeEntity
| + */
| + protected Method isModified;
| +
| + /**
| + * This provides a way to find the entities that are part of a given
| + * transaction EntitySynchronizationInterceptor and
|InstanceSynchronization
| + * manage this instance.
| + */
| + protected static GlobalTxEntityMap globalTxEntityMap = new
|GlobalTxEntityMap();
| +
| +
| + public static GlobalTxEntityMap getGlobalTxEntityMap() {
|return globalTxEntityMap; }
| +
| + /**
| + * Stores all of the entities associated with the specified
|transaction.
| + * @param tx the transaction that associated entites will be stored
| + * @throws Exception if an problem occures while storing the entities
| + */
| + public static void
|synchronizeEntitiesWithinTransaction(Transaction tx) throws
|RemoteException
| + {
| + // If there is no transaction, there is nothing to synchronize.
| + try
| + {
| + if(tx != null)
| + {
| + EntityEnterpriseContext[] entities =
|globalTxEntityMap.getEntities(tx);
| + for (int i = 0; i < entities.length; i++)
| + {
| + EntityEnterpriseContext ctx = entities[i];
| + EntityContainer container =
|(EntityContainer)ctx.getContainer();
| + container.storeEntity(ctx);
| + }
| + }
| + }
| + catch (Exception ex)
| + {
| + throw new
|RemoteException("synchronizeEntitiesWithTransaction failed");
| + }
| + }
| +
| // Public --------------------------------------------------------
|
| public void setContainerInvoker(ContainerInvoker ci)
| @@ -270,6 +318,15 @@
| in = in.getNext();
| }
|
| + try
| + {
| + isModified = getBeanClass().getMethod("isModified",
|new Class[0]);
| + if (!isModified.getReturnType().equals(Boolean.TYPE))
| + isModified = null; // Has to have "boolean" as return type!
| + }
| + catch (NoSuchMethodException ignored) {}
| +
| +
| // Reset classloader
| Thread.currentThread().setContextClassLoader(oldCl);
| }
| @@ -394,7 +451,7 @@
| {
| // synchronize entities with the datastore before the
|bean is removed
| // this will write queued updates so datastore will be
|consistent before removal
| -
|getApplication().synchronizeEntitiesWithinTransaction(mi.getTransaction());
| + synchronizeEntitiesWithinTransaction(mi.getTransaction());
|
| // Get the persistence manager to do the dirty work
|
|getPersistenceManager().removeEntity((EntityEnterpriseContext)mi.ge
|tEnterpriseContext());
| @@ -481,11 +538,11 @@
| public Object findLocal(MethodInvocation mi)
| throws Exception
| {
| - /**
| - * As per the spec 9.6.4, entities must be
|synchronized with the datastore
| - * when an ejbFind<METHOD> is called.
| - */
| -
|getApplication().synchronizeEntitiesWithinTransaction(mi.getTransaction());
| + /**
| + * As per the spec 9.6.4, entities must be synchronized
|with the datastore
| + * when an ejbFind<METHOD> is called.
| + */
| + synchronizeEntitiesWithinTransaction(mi.getTransaction());
|
| // Multi-finder?
| if (!mi.getMethod().getReturnType().equals(getLocalClass()))
| @@ -533,11 +590,11 @@
| */
| public Object find(MethodInvocation mi) throws Exception
| {
| - /**
| - * As per the spec 9.6.4, entities must be
|synchronized with the datastore
| - * when an ejbFind<METHOD> is called.
| - */
| -
|getApplication().synchronizeEntitiesWithinTransaction(mi.getTransaction());
| + /**
| + * As per the spec 9.6.4, entities must be synchronized
|with the datastore
| + * when an ejbFind<METHOD> is called.
| + */
| + synchronizeEntitiesWithinTransaction(mi.getTransaction());
|
| // Multi-finder?
| if (!mi.getMethod().getReturnType().equals(getRemoteClass()))
| @@ -577,6 +634,31 @@
| }
| }
|
| +
| + /**
| + * store entity
| + */
| + public void storeEntity(EntityEnterpriseContext ctx) throws Exception
| + {
| + if (ctx.getId() != null)
| + {
| + boolean dirty = true;
| + // Check isModified bean method flag
| + if (isModified != null)
| + {
| + Object[] args = {};
| + Boolean modified = (Boolean)
|isModified.invoke(ctx.getInstance(), args);
| + dirty = modified.booleanValue();
| + }
| +
| + // Store entity
| + if (dirty)
| + {
| + getPersistenceManager().storeEntity(ctx);
| + }
| + }
| + }
| +
| /**
| * This method takes care of the wiring of the "EJBObject" trio
| * (target, context, proxy). It delegates to the
|persistence manager.
|
|
|
| 1.16 +1 -42 jboss/src/main/org/jboss/ejb/Application.java
|
| Index: Application.java
| ===================================================================
| RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/Application.java,v
| retrieving revision 1.15
| retrieving revision 1.16
| diff -u -r1.15 -r1.16
| --- Application.java 2001/08/06 16:26:28 1.15
| +++ Application.java 2001/08/07 18:06:42 1.16
| @@ -14,7 +14,6 @@
| import java.util.Hashtable;
|
| import javax.ejb.EJBLocalHome;
| -import javax.transaction.Transaction;
|
| import org.jboss.util.Service;
|
| @@ -25,13 +24,8 @@
| * @see Container
| * @see ContainerFactory
| * @author <a href="mailto:[EMAIL PROTECTED]">Rickard Öberg</a>
| - * @version $Revision: 1.15 $
| + * @version $Revision: 1.16 $
| *
| - * <p><b>Revisions:</b><br>
| - * <p><b>2001/08/06: dain</b>
| - * <ol>
| - * <li>Moved synchronizeEntitiesWithinTransaction to this class.
| - * </ol>
| */
| public class Application
| implements Service
| @@ -53,13 +47,6 @@
| // url where this application was deployed from
| URL url;
|
| - /**
| - * This provides a way to find the entities that are part of a given
| - * transaction EntitySynchronizationInterceptor and
|InstanceSynchronization
| - * manage this instance.
| - */
| - private ApplicationTxEntityMap txEntityMap = new
|ApplicationTxEntityMap();
| -
| // Static --------------------------------------------------------
|
| // Public --------------------------------------------------------
| @@ -200,34 +187,6 @@
| name = url.toString();
| }
|
| - /**
| - * Gets the transaction to entity map object, which contains
|a map from
| - * a transaction to every entity used in that transaction.
| - * @return the transaction to entity map for this application
| - */
| - public ApplicationTxEntityMap getTxEntityMap() {
| - return txEntityMap;
| - }
| -
| - /**
| - * Stores all of the entities associated with the specified
|transaction.
| - * @param tx the transaction that associated entites will be stored
| - * @throws Exception if an problem occures while storing the entities
| - */
| - public void synchronizeEntitiesWithinTransaction(Transaction
|tx) throws RemoteException {
| - // If there is no transaction, there is nothing to synchronize.
| - if(tx != null)
| - {
| - EntityEnterpriseContext[] entities =
|getTxEntityMap().getEntities(tx);
| - for (int i = 0; i < entities.length; i++)
| - {
| - EntityEnterpriseContext ctx = entities[i];
| - EntityContainer container =
|(EntityContainer)ctx.getContainer();
| - container.getPersistenceManager().storeEntity(ctx);
| - }
| - }
| - }
| -
| // Service implementation ----------------------------------------
|
| /**
|
|
|
| 1.1 jboss/src/main/org/jboss/ejb/GlobalTxEntityMap.java
|
| Index: GlobalTxEntityMap.java
| ===================================================================
| /*
| * JBoss, the OpenSource J2EE webOS
| *
| * Distributable under LGPL license.
| * See terms of license at gnu.org.
| */
| package org.jboss.ejb;
|
| import java.util.ArrayList;
| import java.util.HashMap;
| import javax.transaction.Transaction;
| import javax.transaction.RollbackException;
| import javax.transaction.SystemException;
| import javax.transaction.Synchronization;
|
|
| /**
| * This class provides a way to find out what entities are contained in
| * what transaction. It is used, to find which entities to call
|ejbStore()
| * on when a ejbFind() method is called within a transaction.
|EJB 2.0- 9.6.4
| * also, it is used to synchronize on a remove.
| * Used in EntitySynchronizationInterceptor, EntityContainer
| *
| * Entities are stored in an ArrayList to ensure specific ordering.
| *
| * @author <a href="[EMAIL PROTECTED]">Bill Burke</a>
| * @version $Revision: 1.1 $
| */
| public class GlobalTxEntityMap
| {
| protected HashMap m_map = new HashMap();
|
| /**
| * associate entity with transaction
| */
| public synchronized void associate(Transaction tx,
| EntityEnterpriseContext entity)
| throws RollbackException, SystemException
| {
| ArrayList entityList = (ArrayList)m_map.get(tx);
| if (entityList == null)
| {
| entityList = new ArrayList();
| m_map.put(tx, entityList);
| tx.registerSynchronization(new
|GlobalTxEntityMapCleanup(this, tx));
| }
| entityList.add(entity);
| }
|
| /**
| * get all EntityEnterpriseContext that are involved with a
|transaction.
| */
| public synchronized EntityEnterpriseContext[]
|getEntities(Transaction tx)
| {
| ArrayList entityList = (ArrayList)m_map.get(tx);
| if (entityList == null) // there are no entities associated
| {
| return new EntityEnterpriseContext[0];
| }
| return (EntityEnterpriseContext[])
| entityList.toArray(new
|EntityEnterpriseContext[entityList.size()]);
| }
|
| /**
| * Cleanup tx/entity map on tx commit/rollback
| */
| private class GlobalTxEntityMapCleanup implements Synchronization
| {
| GlobalTxEntityMap map;
| Transaction tx;
|
| public GlobalTxEntityMapCleanup(GlobalTxEntityMap map,
| Transaction tx)
| {
| this.map = map;
| this.tx = tx;
| }
|
| // Synchronization implementation -----------------------------
|
| public void beforeCompletion()
| {
| // complete
| }
|
| public void afterCompletion(int status)
| {
| synchronized(map)
| {
| ArrayList entityList = (ArrayList)m_map.remove(tx);
| if (entityList != null)
| {
| entityList.clear();
| }
| }
| }
| }
|
| }
|
|
|
|
|_______________________________________________
|Jboss-development mailing list
|[EMAIL PROTECTED]
|http://lists.sourceforge.net/lists/listinfo/jboss-development
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development