Reputation: 651
I want to know about the hibernate cache :-
When we update the object in Cache does it automatically get updated in the database. If not then how will it get updated?
If the database is updated will it get updated in cache instantaneously, if not then again how will it get updated?
Upvotes: 2
Views: 893
Reputation: 154120
Hibernate has a first and an optional second level cache. The first level cache is mandatory and it's one storing your attached Entities that are to be synced with the DB.
So at flush time, the 1st level cache and the current db transaction are in sync, while at commit time the changes are propagated to other connections as well.
The 2nd level cache comes in many flavours:
Some implementations are also clustered and have additional sync options.
The more safe the sync mode the less performance you'll get, so make sure you choose the right one for your application use cases.
Upvotes: 1
Reputation: 6540
Have a read of this post: What are First and Second Level caching in Hibernate?
Basically Hibernate manages these cache's transparently. So if you are using a SessionFactory, you don't have to worry about what is updated and where it is stored, Hibernate manages that for you. The cache (at both levels) exists simply to try and reduce the number of database calls that Hibernate has to make. You shouldn't be trying to directly control the first level cache, and you should only be trying to configure the second level cache explicitly if you are performing some serious optimisation of your system.
But to answer your questions:
To the best of my knowledge, you don't update a cached instance. If you are performing an update Hibernate will update the database and persist the new object state to the 1st level cache.
1st level cache yes, 2nd level cache no, You can configure your second level cache to regularly expire persisted data, but as the documentation says, the cache is not aware of changes made to the persisted store.
Have a read of the documentation and see if that answers your question further.
Upvotes: 0