Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are using the same EntityManager instance. The cache settings apply to the shared cache, while the EntityManager is required to keep the same object in memory until it is closed or cleared - otherwise a simple find operation might wipe out changes in a running transaction. Get a new EntityManager or clear the existing one will cause the EntityManager to go to the shared cache and/or the database as necessary. </p> <p>If you do need the object refreshed in the current EM context, you will have to explicitly call em.refresh or use a query hint so that it knows to wipe out any existing changes with what is in the database. </p> <p>Each EM represents a transactional context, and since each thread should have its own EntityManager, there will already be multiple copies of your entity in the application. You will run into trouble if you rely on a particular instance read from an EntityManager always reflecting the current changes - it can only really reflect what is in the database at the moment it was read. This is why it is important to merge in changes when necessary, and is probably not a good idea to cache objects in the application - access them from EntityManagers as needed instead. </p> <p>ut if you want the object and are not making changes, mark it as read-only which will pull it from the shared cache instead of the EM, and so reflect the cache settings: <a href="http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Shared_and_Isolated#Read-Only_Entities" rel="nofollow">http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Shared_and_Isolated#Read-Only_Entities</a> . If the entity is cached in the shared cache, this will be the instance you get back, and all EMs will return the same object instance - it is up to your app to manage concurrency issues on the entity itself.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for the explanation. I'm starting to grasp the whole problem of the situation. I can't set the entities "read-only" because in rare cases I need to write them. The current situation is that I create a module to read/write our master data. While it is not critical I would like to reduce the memory footprint by only holding objects once. But it still doesn't expain why the @Cache annotation isn't working. This is also where I would define if the entity is shared or isolated.
      singulars
    2. COThe @Cache annotion is likely working, it just doesn't apply to your EM. It is a common design flaw for users to hold onto an EM and make it long lived as they seem to have been designed to be short term light weight sessions. The entities read in can only represent the data at the point the entity was loaded. After that, the EM gives you consistently the same data unless you explicitly refresh it, or clear the em. This allows the app to make calculations and changes without worry that the data is changing underneath - it can use optimistic locking to ensure it will not write stale data.
      singulars
    3. COBut than what exactly is the @Cache annotation doing? Also it seems that every EM uses its own Database connection which makes it quite expensive to create and tear it down. What really annoys me about jpa/eclipselink is that there is documentation, but when I copy an example it just doesn't work and I get no errors. Having a similar problem with lazy loading right now. Eclipselink just ignores the Annotation and does eager-loading. No error, warning or what so ever. Is there any good introduction to JPA's persistence strategy. Maybe I'm missing some basics ...
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload