R: cache synchronization

From: Claudio Rosati (claudio.rosat..csys.it)
Date: Fri Dec 23 2005 - 08:39:43 EST

  • Next message: Cris Daniluk: "Re: Żlgi:Re: OutOFMemory Java heap exeception"

    Hi Andrus,

    I've done the following:

    public class RecordDocument
      ...
    {

      ...
     
      private transient DataContext dataContext =
    DataContext.createDataContext();

      private transient DataContextDelegate dataContextDelegate = new
    DataContextDelegate() {
        public void finishedMergeChanges ( DataObject object ) {
          System.out.println("DataContextDelegate.finishedMergeChanges");
        }
        public void finishedProcessDelete ( DataObject object ) {
        }
        public boolean shouldMergeChanges ( DataObject object, DataRow
    snapshotInStore ) {
          return true;
        }
        public boolean shouldProcessDelete ( DataObject object ) {
          return true;
        }
        public GenericSelectQuery willPerformSelect ( DataContext context,
    GenericSelectQuery query ) {
          return query;
        }
      };

      private transient SnapshotEventListener snapshotEventListener = new
    SnapshotEventListener() {
        public void snapshotsChanged ( SnapshotEvent event ) {
          System.out.println("SnapshotEventListener.snapshotsChanged");
        }
      };

      ...

      public RecordDocument ( Application application, Long code )
        throws IllegalArgumentException, NoSuchObjectException
      {

        ...
                    
        DataDomain domain = dataContext.getParentDataDomain();
                    
        EventManager.getDefaultManager().addNonBlockingListener(
          snapshotEventListener,
          "snapshotsChanged",
          SnapshotEvent.class,
          domain.getSharedSnapshotCache().getSnapshotEventSubject(),
          domain.getSharedSnapshotCache()
        );

        dataContext.setDelegate(dataContextDelegate);
                    
      }

      ...

    }

    But, while dataContextDelegate is called as said, snapshotEventListener is
    never called.
    This is also true if dataContext is created as follow (my preferred method):

    ...
      private transient DataContext dataContext =
    CayenneUtility.createDataContext();
    ...

    where

    public class CayenneUtility {

      ...

      /**
       * Returns a newly created ..ink DataContext} with a specially managed
       * cache in order to reduce memory consuption and performance degradation.
       */
      public static DataContext createDataContext ( ) {
                    
        DataDomain domain = Configuration.getSharedConfiguration().getDomain();
        ObjectStore os = new
    ManagedObjectStore(domain.getSharedSnapshotCache());
                    
        return new DataContext(domain, os);
                    
      }

    ...

      private static class LRUHashMap
        extends LinkedHashMap
      {

        private static int MAX = 1000;
                    
        private LRUHashMap ( ) {
          super(MAX);
        }

        protected boolean removeEldestEntry ( Map.Entry eldest ) {
          return ( size() > MAX );
        }

      } // class LRUHashMap
            
      public static class ManagedObjectStore
        extends ObjectStore
      {
        public ManagedObjectStore ( DataRowStore cache ) {
          super(cache);
          super.objectMap = new LRUHashMap();
        }
      } // class ManagedObjectStore
            
    } // class CayenneUtility

    Claudio

    ------------------------------------
    Advanced Computer Systems S.p.A.
    Claudio Rosati
    Project Manager
    claudio.rosat..csys.it
    via Della Bufalotta 378
    00139 Roma, RM
    Italy
    tel: +39 06 8709 0516
    fax: +39 06 8720 1502
    ------------------------------------
     

    > -----Messaggio originale-----
    > Da: Andrus Adamchik [mailto:andru..bjectstyle.org]
    > Inviato: sabato 17 dicembre 2005 22.52
    > A: cayenne-use..bjectstyle.org
    > Oggetto: Re: cache synchronization
    >
    > I think you have more than one DataContext in the app? If so
    > the delegate method will be tripped by an update in a peer
    > DataContext in the SAME VM.
    >
    > If this is the case you may replace a delegate with a
    > SnapshotEventListener (so you'd know whether the event was
    > local or remote by looking at the event object "source" and
    > "postedBy"
    > properties).
    >
    > In 1.1 you'd do something like this:
    >
    > class MyListener implements SnapshotEventListener { ...
    > }
    >
    > DataDomain domain = ...
    > EventManager.getDefaultManager().addNonBlockingListener(
    > new MyListener(),
    > "snapshotsChanged",
    > SnapshotEvent.class,
    > domain.getSharedSnapshotCache
    > ().getSnapshotEventSubject(),
    > domain.getSharedSnapshotCache());
    >
    >
    > BTW, debugging this issue helped me to identify and fix an
    > evil event
    > loop in 1.2 (it is not present in 1.1.3) - http://objectstyle.org/
    > jira/secure/ViewIssue.jspa?key=CAY-410
    >
    > Andrus
    >
    >
    > On Dec 15, 2005, at 4:58 PM, Claudio Rosati wrote:
    >
    > > Hi Andrus.
    > >
    > > Here what you asked (I've just made a remote modification first,
    > > followed by
    > > a local one. Each modification called 5 times the shouldMergeChanges
    > > method.):
    >



    This archive was generated by hypermail 2.0.0 : Fri Dec 23 2005 - 08:39:42 EST