Caching with Hibernate + Spring - some Questions

I'm working on developing a web application with Spring 3 and Hibernate 3.6. At the moment I try to understand how Caching with Spring and Hibernate works. I found some sources about Caching with Hibernate and some about Spring and I try to bring my information together now. I still got some questions to both frameworks and I'd be glad if someone could answer them or tell me if the facts listed here are correct.

Most of the time, short answers (yes/no) would be sufficient. I think that this list can be useful for others too, who want to understand how caching with spring and hibernate works.


1) Hibernate supports the following Caches: 1st Level Cache, 2nd Level Cache, Query Cache

2) Spring itself supports the following Caching possibilities: just Method Caching

1st Level Cache

3) The 1st Level Cache is part of EVERY Hibernate application.

4) The 1st Level Cache is created for EVERY hibernate-session.

5) What is saved in the 1st Level Cache? Objects or just the values of their properties? queries and their results?

2nd Level Cache

6) I found out: the 2nd Level Cache is used ONCE per application. isn't that false? isn't it used ONCE per SessionFactory? and: multiple sessionfactorys = multiple 2nd level caches possible?

7) what is saved in the 2nd Level Cache: in my opinion just the values belonging to one record, not the objects itself.

8) when storing values from one record in 2nd Level Cache, its possible to store related values (from objects connected over a foreign key) with it too?

9) when updating the values of one object in the 2nd level cache, it's possible to update the values of objects connected with it in the cache too?

10) when values of an object are changing, how can I update the 2nd level cache? flush? can I just update a part of the cache or must the whole cache be updated?

11) where does the 2nd level cache make sense and where doesn't it?

12) the Cache Mode: does each cache mode provide a different strategy of caching? for example with cache mode "read-only" no synchronization of database and cache is ever necessary? do other cache modes provide synchronization? I thought synchronization must be done by the developer himself?

Query Cache

13) what is the difference between the Query Cache and the 2nd Level Cache? in my opinion: in the Query Cache result sets are saved, but not with their values, just with their ids. when the query is used again and the result set is still "correct", the values belonging to the ids are queried from the 2nd Level Cache

14) For the Query Cache a 2nd Level Cache MUST be used?

15) where does the Query Cache make sense and where doesn't it?


16) Does Spring provide more Caching possibilities than method caching?

17) method caching is not linked to hibernate caching

18) but: for method caching the 2nd level is necessary, like Ehcache (which can be used by hibernate too)

19) can method caching be used without database queries?

Getting mixed up

20) if using ehcache for hibernate as 2nd level cache and Ehcache for spring for method caching, can I use the same Ehcache-instance? is there a chance that something gets mixed up?

21) when using 1st level cache and 2nd level cache, can they get mixed up? when querying the database, where does the result then come from, the 1st or 2nd level cache? does the 1st level cache work with the 2nd level cache?

22) anything else that can get mixed up by using the caches I mentioned? :-)

Thanks for answering, no matter what question! :-)

задан Govinda Sakhare 29 September 2019 в 19:14