views:

49

answers:

1

Env: Seam 2.2.0, JPA, Hibernate 3.3.x, ehcache-core 2.0.1

Here are some observations on using a cache along with Hibernate. We are already using the 2nd level cache but not quite comfortable with the response times.

The third option mentioned in the above reference (Direct access to EHCache, only use Hibernate on a cache miss: 20 second) talks about using ehcache in front of the persistence layer. We are already using Seam's EntityQuery framework for all our queries, is it possible to enable the above option where-in you access ehcache first before querying or loading objects. How does modification or deletion of entity gets handled in such cases, do you have to manually evict these entries from the cache then. Can someone shed some light on this?

+1  A: 

First of all, I find the data of the referenced article extremely weird (19s to get a connection suggests a contention on the connection pool). So I'd take it with a grain of salt.

We are already using Seam's EntityQuery framework for all our queries, is it possible to enable the above option where-in you access ehcache first before querying or loading objects.

Well, that's actually how Hibernate L2 cache does work. As mentioned in a previous answer, Hibernate Second Level cache is Read-Write-Through Cache where if cache miss occurs, entity is read from database and then handed over to cache for subsequent access.

Now, if what you mean is implementing the third option, this would mean bypassing entirely Hibernate L2 cache and doing everything manually. Sure, that's possible but you're on your own here. And as I said, I highly doubt that going through Hibernate introduces that much overhead so I would not base a decision on the mentioned article. Measure things.

How does modification or deletion of entity gets handled in such cases, do you have to manually evict these entries from the cache then.

Entirely manually. Good luck with transactions, error handling, concurrency, etc.

Pascal Thivent