views:

168

answers:

2

how to decide whitch CacheConcurrencyStrategy to use? NonstrictReadWriteCache, ReadOnlyCache, ReadWriteCache, TransactionalCache . I read https://www.hibernate.org/hib%5Fdocs/v3/api/org/hibernate/cache/CacheConcurrencyStrategy.html but doesn't explain in detail enough

+3  A: 

Reading API Docs is good thing, but you should also read the documentation (its awesome) also, Second Level Cache - Strategies.

Adeel Ansari
+3  A: 

The Hibernate documentation does a pretty good job at defining them:

19.2.2. Strategy: read only

If your application needs to read, but not modify, instances of a persistent class, a read-only cache can be used. This is the simplest and optimal performing strategy. It is even safe for use in a cluster.

19.2.3. Strategy: read/write

If the application needs to update data, a read-write cache might be appropriate. This cache strategy should never be used if serializable transaction isolation level is required. If the cache is used in a JTA environment, you must specify the property hibernate.transaction.manager_lookup_class and naming a strategy for obtaining the JTA TransactionManager. In other environments, you should ensure that the transaction is completed when Session.close() or Session.disconnect() is called. If you want to use this strategy in a cluster, you should ensure that the underlying cache implementation supports locking. The built-in cache providers do not support locking.

19.2.4. Strategy: nonstrict read/write

If the application only occasionally needs to update data (i.e. if it is extremely unlikely that two transactions would try to update the same item simultaneously), and strict transaction isolation is not required, a nonstrict-read-write cache might be appropriate. If the cache is used in a JTA environment, you must specify hibernate.transaction.manager_lookup_class. In other environments, you should ensure that the transaction is completed when Session.close() or Session.disconnect() is called.

19.2.5. Strategy: transactional

The transactional cache strategy provides support for fully transactional cache providers such as JBoss TreeCache. Such a cache can only be used in a JTA environment and you must specify hibernate.transaction.manager_lookup_class.

In other words:

  • Read-only: Useful for data that is read frequently but never updated (e.g. referential data like Countries). It is simple. It has the best performances of all (obviously).

  • Read/write: Desirable if your data needs to be updated. But it doesn't provide a SERIALIZABLE isolation level, phantom reads can occur (you may see at the end of a transaction something that wasn't there at the start). It has more overhead than read-only.

  • Nonstrict read/write: Alternatively, if it's unlikely two separate transaction threads could update the same object, you may use the nonstrict–read–write strategy. It has less overhead than read-write. This one is useful for data that are rarely updated.

  • Transactional: If you need a fully transactional cache. Only suitable in a JTA environment.

So, choosing the right strategy depends on the fact that data are being updated or not, the frequency of updates and the isolation level required. If you don't know how to answer these questions for the data you want to put in cache, maybe ask some support from a DBA.

Pascal Thivent
@Pascal, for relibility criteira "Transactional" is the best but perforamce is bad right? can you elaborate what is JTA environment? Is it EJB?
cometta
No. you can use JTA out-of-EJB-container.
Adeel Ansari
to use Transactional, need to have cache provider that support it? ehache can be used? i'm using ehcahe+spring hibernatetemplate+Jpa entity , does that consider JTA environment?
cometta
@cometta I wouldn't say that performance are bad but rather that you get the most overhead with it.
Pascal Thivent
@cometta No, EHCache doesn't support transactional strategy as documented in http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-cache-compat-matrix
Pascal Thivent