Can EclipseLink cache be removed automatically - eclipselink

If I am using EclipseLink for entity cache or named query cache with a timeout, I suppose it will not be automatically removed when the cache is timed out in order to save memory. Does it?
If I have a memory problem like Heap memory is at critical level, will EclipseLink cache be automatically removed (does it use WeakReference or similar mechanism to manage cache map instead of strong reference) ?
Thanks,

EclipseLink has both an identity map and cache at the factory level, allowing a set cache size and an expensive identity map that allows for garbage collection. The documentation explains it and the options available to control or disable it depending on your application needs

Related

BinaryObject and Cache Eviction/Expiration

When using BinaryObject for off-heap in-memory-only cache values, do we need to do anything to protect against the cache entry being evicted or expired while accessing fields via BinaryObject::field(String)?
For example, if the cache's data region has the default memory-size eviction (90% full?), or if the cache uses a creation expiry policy, and the region happens to evict entries or the cache expire entries while the code is making several calls to BinaryObject::field(String). Does Ignite automatically ensure that BinaryObject won't access invalid off-heap memory (throwing an exception perhaps), or can the developer use locking / transactions or a "touched" expiry to help prevent this?
Thanks!
BinaryObject instances returned from the Ignite API and accessed by the user code are copies. They do not reference Ignite storage memory directly.
You can work with BinaryObject even after the corresponding cache entry gets evicted.
When a cache object becomes eligible for eviction, it will be removed from memory.
Ignite has several Eviction policies:
Random-LRU
Random-2-LRU
Explained here
This means that if you recently used a cache value(or are using it at the moment that the eviction is taking place), either in BinaryObject representation or not, it will not be evicted. All Eviction algorithms use a LEAST RECENTLY USED algorithm.

The meaning of evict() in infinispan cache

According to the docs for infinispan: http://docs.jboss.org/infinispan/5.0/apidocs/ the evict() API does not remove the entry from any other cache stores in the cluster, on the cache store it was invoked on.
If using "replication" mode, where the data is replicated across the caches, surely it has to be consisted and using the evict() API will make it inconsistent.
How then is the inconsistency resolved?
Thanks
Evict removes the entry only from the memory on the node where you call it. It does not make the cache inconsistent, because if you call cache.get() and the entry is not found in memory, it is loaded from cache store.
As the documentation states, the purpose is to inform cache that it won't use the entry for some time and the node can free some memory.

Remove deleted items from first level cache in NHibernate? Or: how to check if cached items have been deleted?

We have a brownfield multi-user application (99% Delphi, 1% .net) which uses NHibernate for the persistency of the .net modules. In my application I can add categories to some entity. If I select one and decide to not use it (thus removing the category again) I has been loaded by NHibernate and will stay in the session's first level cache. Now, if some other user deletes this category and I try to save my entity my application throws an exception because the object loaded doesn't exist anymore.
my question: is there a way to check if my cache has items loaded which don't exist anymore? and if so, is there a way to remove non-exist entities from my cache?
So what happens:
I load an entity (added to session cache)
I add a category (added to session cache)
Someone else deletes the category from the database.
I save my entity and the exception occurs because the category doesn't exist anymore.
It's still in the session cache. It would be nice if I could (automatically) remove it from my session's cache? is there a way to clean up the cache and remove objects that don't exist anymore?
Regards, Ted
There's no option in NHibernate to do it automatically, at least not with ISession. You could use IStatelessSession for loading, since it doesn't have first-level cache, but you'll lose many other features that ISession provides.
You could also call ISession.Clear() to clear the session (first-level) cache, or ISession.Evict() to evict certain entities from session, but that's not automatic.
How long do you keep your session object? Maybe you need a different session management context.
If the lifespan of your session is shorter, you can still achieve entity caching, but with second-level cache. SysCache2 is one of second-level cache providers that has a support for SqlCacheDependency. This means that you could set cache expiration when some objects in database change.

nhibernate second level cache with collections

I have a class which has a collection, Category.ChildCategoryLinks
I have set this as cached, using a Read-Write cache policy. However, whenever a new ChildCategoryLink is added that references the same Category, the cache is not updated, and thus it is resulting in stale data.
I am using Fluent NHibernate to configure NH. I am not specifying any cache regions. Any idea why the cache is not being flushed / updated, once the new ChildLink is created?
NHibernate will not invalidate the cache unless you update the collection in code too; changing the inverse side (the many-to-one) is not enough.
Also, make sure you do everything inside a transaction.

What are the First and Second Level caches in (N)Hibernate?

Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate are?
1.1) First-level cache
First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one
transaction after another one, means wont process one transaction many
times. Mainly it reduces the number of SQL queries it needs to
generate within a given transaction. That is instead of updating after
every modification done in the transaction, it updates the transaction
only at the end of the transaction.
1.2) Second-level cache
Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the
objects at the Session Factory level, so that those objects will be
available to the entire application, not bound to single user. Since
the objects are already loaded in the cache, whenever an object is
returned by the query, at that time no need to go for a database
transaction. In this way the second level cache works. Here we can use
query level cache also.
Quoted from: http://javabeat.net/introduction-to-hibernate-caching/
There's a pretty good explanation of first level caching on the Streamline Logic blog.
Basically, first level caching happens on a per session basis where as second level caching can be shared across multiple sessions.
Here some basic explanation of hibernate cache...
First level cache is associated with “session” object.
The scope of cache objects is of session. Once session is closed, cached objects are gone forever.
First level cache is enabled by default and you can not disable it.
When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.
If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.
The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method.
The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.
Second level cache is apart from first level cache which is available to be used globally in session factory scope.
second level cache is created in session factory scope and is available to be used in all sessions which are created using that particular session factory.
It also means that once session factory is closed, all cache associated with it die and cache manager also closed down.
Whenever hibernate session try to load an entity, the very first place it look for cached copy of entity in first level cache (associated with particular hibernate session).
If cached copy of entity is present in first level cache, it is returned as result of load method.
If there is no cached entity in first level cache, then second level cache is looked up for cached entity.
If second level cache has cached entity, it is returned as result of load method. But, before returning the entity, it is stored in first level cache also so that next invocation to load method for entity will return the entity from first level cache itself, and there will not be need to go to second level cache again.
If entity is not found in first level cache and second level cache also, then database query is executed and entity is stored in both cache levels, before returning as response of load() method.
First-level cache
Hibernate tries to defer the Persistence Context flushing up until the last possible moment. This strategy has been traditionally known as transactional write-behind.
The write-behind is more related to Hibernate flushing rather than any logical or physical transaction. During a transaction, the flush may occur multiple times.
The flushed changes are visible only for the current database transaction. Until the current transaction is committed, no change is visible by other concurrent transactions.
Due to the first-level cache, Hibernate can do several optimizations:
JDBC statement batching
prevent lost update anomalies
Second-level cache
A proper caching solution would have to span across multiple Hibernate Sessions and that’s the reason Hibernate supports an additional second-level cache as well.
The second-level cache is bound to the SessionFactory life-cycle, so it’s destroyed only when the SessionFactory is closed (typically when the application is shutting down). The second-level cache is primarily entity-based oriented, although it supports an optional query-caching solution as well.
When loading an entity, Hibernate will execute the following actions:
If the entity is stored in the first-level cache, then the cached object reference is returned. This ensures application-level repeatable reads.
If the entity is not stored in the first-level cache and the second-level cache is activated, then Hibernate checks if the entity has been cached in the second-level cache, and if it were, it returns it to the caller.
Otherwise, if the entity is not stored in the first or second-level cache, it will be loaded from the DB.
by default, NHibernate uses first level caching which is Session Object based. but if you are running in a multi-server environment, then the first level cache may not very scalable along with some performance issues. it is happens because of the fact that it has to make very frequent trips to the database as the data is distributed over multiple servers. in other words NHibernate provides a basic, not-so-sophisticated in-process L1 cache out of box. However, it doesn’t provide features that a caching solution must have to have a notable impact on the application performance.
so the questions of all these problem is the use of a L2 cache which is associated with the session factory objects. it reduces the time consuming trips to the database so ultimately increases the app response time.
First Level Cache
Session object holds the first level cache data. It is enabled by default. The first level cache data will not be available to entire application. An application can use many session object.
Second Level Cache
SessionFactory object holds the second level cache data. The data stored in the second level cache will be available to entire application. But we need to enable it explicitly.
In a second level cache, domain hbm files can be of key mutable and value false.
For example,
In this domain class some of the duration in a day remains constant as the universal truth. So, it can be marked as immutable across application.