Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Stale Data Between GUIs Using EclipseLink JPA

I agree with James, from experience the best way to deal with the 2nd
Level Cache and stale data is to take extra care to always make sure
you have fresh data, by using refresh and using the correct cache
hints on queries.

I use a method like this to make sure my queries are fresh, in
addition to turning off the 2nd level cache like James said.

public static void setNoCacheHints(Query q)
        q.setHint(QueryHints.REFRESH, HintValues.TRUE);


On Mon, Jan 12, 2009 at 10:57 AM, James Sutherland <jamesssss@xxxxxxxxx> wrote:
> In JPA an EntityManager represents an extended persistence context, in that
> any object read into the EntityManager will remain until you either close()
> or clear() the EntityManager.  You can also call refresh() on an object to
> refresh it explicitly.  An EntityManager normally relates to a single
> transaction or request.  So to see changes made in another EntityManager, or
> another machine, you need to either create a new EntityManager, or clear()
> the existing one, or refresh() each object you want refreshed.
> EclipseLink also managed a 2nd level shared cache at the
> EntityManagerFactory level.  This cache also caches the objects that are
> read, so if you want fresh data you must also ensure this cache is
> refreshed.  Calling refresh() will also refresh the cache (unless you have
> flushed changes), the cache also has invalidation API and configuration, or
> can be disabled using persistence.xml
> "eclipselink.cache.shared.default"="false", or the @Cache annotation or
> orm.xml.
> I would recommend you continue working with JPA, and not use the native API
> or Mapping Workbench.
> MikeyL wrote:
>> I am playing with a Movie-Actor application to learn about persistence. I
>> have a Derby database with a Movies table with title, rating, release
>> year, etc., and an Actors table with name, birth date, etc. I have an
>> associative table joining a many-to-many relationship between Movies and
>> Actors. The associative table declares foreign key fields with references
>> back to the parent tables. I designed Swing GUIs to display the data. The
>> Movie GUI will display actors for a selected movie in a child table. The
>> Actor GUI will display movies for a selected actor in a child table. The
>> Entity Manager Factory and Entity Manager are instantiated once by the
>> main GUI at program start. The Movie and Actor DAOs get the Entity Manager
>> from the main screen. The problem is that changes made in one GUIs do not
>> get reflected in the other. E.g., if I add the movie 2 Days in the Valley
>> to actress Charlize Theron in the Actor GUI then click on 2 Days in the
>> Valley in the Movie GUI, Charlize is not listed as an actor in the child
>> table. Even pressing the Refresh button to reload the Movie table does not
>> help. How do I synchronize the data between the two GUIs?
>> In an attempt to solve the above problem, I took a look at EclipseLink
>> Sessions. It seems that I could have the main GUI invoke a server session
>> and have separate client sessions for the Movie and Actor GUIs. However,
>> when using Workbench to create entity source files from my database tables
>> the many-to-many relationship is not picked up by Workbench although the
>> foreign key reference constraints are on the associative table. I crawled
>> all over the User's Guide and can't find how to manually define these
>> relationships in the mapping XML file. Is this a bug in Workbench?
>> Any help and thoughts will be appreciated.
>> MikeyL
> -----
> ---
> James Sutherland
>  EclipseLink ,
> TopLink
> Wiki: EclipseLink ,
> TopLink
> Forums: TopLink ,
> EclipseLink
> Book: Java Persistence
> --
> View this message in context:
> Sent from the EclipseLink - Users mailing list archive at
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx

Back to the top