Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate cache

 
sanjey asok
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In hibernate caching, If I execute a query ,that result set will be stored in Cache,
Later ,it is used if the same query is executed

1)If I have a table with 50 Records and I used select statement to select all records in the table, after the query executed, if result set is cached by Hibernate, then I add some data into the table, again I execute the same query, how the hibernate will handle this scenario, whether it will execute query fresh and cache the result ? or some other best way???
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate only uses a query cache if you configure it to do so, and if you do you also need to provide a second level cache. The query cache does not store the state of your results it stores a result set of identifiers, the state is held in the second level cache. Using the query cache and the second level cache are not things you would normally do, the query cache in particular would be one of the last mechanisms you would probably look to to improve performance.


1)If I have a table with 50 Records and I used select statement to select all records in the table, after the query executed, if result set is cached by Hibernate, then I add some data into the table, again I execute the same query, how the hibernate will handle this scenario, whether it will execute query fresh and cache the result ? or some other best way???

Hibernate will go to the cache until it is flushed. If you udate data outside of Hibernate there is no way it can know about this update, such are the dangers of using any caching mechanism with a database. you need to choose carefully when to use one.
 
sanjey asok
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you udate data outside of Hibernate there is no way it can know about this update



if it is updated using Hibernate ,please explain such scenario
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Hibernate is managing the sate it can manage the cache, so there is no issue if Hibernate is the only mechanism that updates your data. In the real world however this is not normal behaviour, databases are shared so multiple client can update the data in multiple ways. Hence thinking long and hard about what aspects of caching you might use and whether it is appropriate is key.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic