User: patriot1burke
  Date: 01/06/14 20:42:29

  Modified:    src/main/org/jboss/ejb/plugins
                        EntitySynchronizationInterceptor.java
  Log:
  EntitySynchronizationInterceptor and InstanceSynchronization now implement
  store before find. see below.
  
  The EJB spec 2.0 reads.... 9.6.4
  
  "Before invoking the ejbFind<METHOD>(...) method,
  the container must first synchronize the state of any entity bean instances
  that are participating in the same transaction context as is used to execute
  the ejbFind<METHOD>(...) by invoking the ejbStore() method on those entity
   bean instances."
  
  Revision  Changes    Path
  1.36      +47 -13    
jboss/src/main/org/jboss/ejb/plugins/EntitySynchronizationInterceptor.java
  
  Index: EntitySynchronizationInterceptor.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/EntitySynchronizationInterceptor.java,v
  retrieving revision 1.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- EntitySynchronizationInterceptor.java     2001/06/11 19:52:26     1.35
  +++ EntitySynchronizationInterceptor.java     2001/06/15 03:42:29     1.36
  @@ -50,7 +50,7 @@
   *   @see <related>
   *   @author Rickard Öberg ([EMAIL PROTECTED])
   *   @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
  -*   @version $Revision: 1.35 $
  +*   @version $Revision: 1.36 $
   */
   public class EntitySynchronizationInterceptor
   extends AbstractInterceptor
  @@ -148,16 +148,21 @@
            // return;
            //}
   
  +      // associate the entity bean with the transaction so that
  +      // we can do things like synchronizeEntitiesWithinTransaction
  +      ((EntityContainer)ctx.getContainer()).getTxEntityMap().associate(tx, ctx);  
  +
            // We want to be notified when the transaction commits
            tx.registerSynchronization(synch);
   
          } catch (RollbackException e) {
  +      ((EntityContainer)ctx.getContainer()).getTxEntityMap().disassociate(tx, ctx); 
 
   
            // The state in the instance is to be discarded, we force a reload of state
            ctx.setValid(false);
   
          } catch (Exception e) {
  -
  +      ((EntityContainer)ctx.getContainer()).getTxEntityMap().disassociate(tx, ctx); 
 
            throw new EJBException(e);
   
          }
  @@ -173,12 +178,48 @@
          // OSH: Pool seems to do this: ctx.setInvoked(false);
       }
   
  +    /**
  +     * As per the spec 9.6.4, entities must be synchronized with the datastore
  +     * when an ejbFind<METHOD> is called.
  +     */
  +    private void synchronizeEntitiesWithinTransaction(Transaction tx) throws 
Exception
  +    {
  +     Object[] entities = 
((EntityContainer)getContainer()).getTxEntityMap().getEntities(tx);
  +     for (int i = 0; i < entities.length; i++)
  +     {
  +         EntityEnterpriseContext ctx = (EntityEnterpriseContext)entities[i];
  +         storeEntity(ctx);
  +     }
  +    }
  +
  +    private void storeEntity(EntityEnterpriseContext ctx) throws Exception
  +    {
  +     if (ctx.getId() != null)
  +     {
  +         boolean dirty = true;
  +         // Check isModified bean flag
  +         if (isModified != null)
  +         {
  +             dirty = ((Boolean)isModified.invoke(ctx.getInstance(), new 
Object[0])).booleanValue();
  +         }
  +         
  +         // Store entity
  +         if (dirty)
  +             
((EntityContainer)getContainer()).getPersistenceManager().storeEntity(ctx);
  +     }
  +    }
       // Interceptor implementation --------------------------------------
   
       public Object invokeHome(MethodInvocation mi)
       throws Exception
       {
          try {
  +      if (mi.getTransaction() != null && 
mi.getMethod().getName().startsWith("find"))
  +      {
  +          // As per the spec 9.6.4, entities must be synchronized with the datastore
  +          // when an ejbFind<METHOD> is called.
  +          synchronizeEntitiesWithinTransaction(mi.getTransaction());
  +      }
            return getNext().invokeHome(mi);
          } finally {
            // Anonymous was sent in, so if it has an id it was created
  @@ -281,16 +322,7 @@
             // the EJB specification? Think of SequenceBean.getNext().
             if (ctx.getId() != null)
             {
  -              boolean dirty = true;
  -              // Check isModified bean flag
  -              if (isModified != null)
  -              {
  -                 dirty = ((Boolean)isModified.invoke(ctx.getInstance(), new 
Object[0])).booleanValue();
  -              }
  -
  -              // Store entity
  -              if (dirty)
  -                 
((EntityContainer)getContainer()).getPersistenceManager().storeEntity(ctx);
  +           storeEntity(ctx);
             }
   
             return result;
  @@ -412,7 +444,6 @@
             // If rolled back -> invalidate instance
             // If removed -> send back to the pool
             if (status == Status.STATUS_ROLLEDBACK || ctx.getId() == null) {
  -           EntityContainer container = (EntityContainer)ctx.getContainer();
              AbstractInstanceCache cache = 
(AbstractInstanceCache)container.getInstanceCache();
              Object id = ctx.getCacheKey();
              // Hopefully this transaction has an exclusive lock
  @@ -423,7 +454,9 @@
                 // We really should be acquiring a mutex before
                 // mucking with the InstanceCache or InstancePool
                 mutex.acquire();
  +
                    // finish the transaction association
  +              
((EntityContainer)ctx.getContainer()).getTxEntityMap().disassociate(tx, ctx);  
                    ctx.setTransaction(null);
   
                    // remove from the cache
  @@ -479,6 +512,7 @@
                 }
   
                 // finish the transaction association
  +           ((EntityContainer)ctx.getContainer()).getTxEntityMap().disassociate(tx, 
ctx);  
                 ctx.setTransaction(null);
             }
            }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to