tags:

views:

436

answers:

5

Do you always use a second-level cache in Hibernate or do you first try it without and only use it when the performance goes down?

+1  A: 

My usage of Hibernate has always been in the context of another framework (Spring, for example), where enabling caching is nearly trivial. A number of these projects have made use of caching via ehcache for some critical domain classes.

Having said that, this is another area where we have to make trade-offs between resources - balancing retrieval performance against memory usage. Optimization without measurement has been shown again and again to be poor practice.

Collect metrics about the performance of your application. Then decide how to address the slow spots. Caching may be the least of your worries.

Ken Gentle
+2  A: 

In the applications where I've been, the database is shared among several applications, some of them are not Java at all. So, the second-level cache is not an option for me in these situations, because I never know when some other app can update the db.

Miguel Ping
+1  A: 

If you do things right (beware of select N+1, etc) performance should be acceptable without second-level cache for the vast majority of cases

Mauricio Scheffer
+1  A: 

We first try it without and only use it when the performance goes down.

ngn
+5  A: 

Make it work first, then make it fast. If you don't need caching, don't implement it.

jon077
Agreed. Performance is never an issue until it become's one...
Frederic Morin
That "begs the question" ;)
jklp