• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there a default cache?

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I am reading about hibernate caching, understood a very little bit of it. While reading I got few doubts, let me share my doubts with you all.

1. Is there any default cache mechanism? Suppose I dint set the "hibernate.cache.provider_class" in the config file.
2. Does hibernate loads the entire database into it, at the start of the application?
3. Suppose I inserted a record in a table, will it be updated with the hibernate copy which it loaded at the start of the application?
4. Suppose I issued a select query, from where will I get the records? Is it from hibernate or from database?
5. Suppose I closed the session object, will the records in cache be deleted?
6. If cache is cleared when session is closed, then why to close the session. I think its not a better idea to close the session.

To my understanding, I got these doubts. Can anyone please tell me about these?

Thank you all in advance.
 
Ranch Hand
Posts: 122
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi chaitanya ,

The question that you have asked covers 4-5 chapters of Hibernate in action about how objects are managed in hibernate session.

Well, I may not be able to answer all this question accurately, but i will at teast provide you some basic understanding based on which you can explore this further.


1. Is there any default cache mechanism? Suppose I dint set the "hibernate.cache.provider_class" in the config file.



yes there is a default cache that hibernate maintain. Its called persistence context. This context has a scope of session. it starts when you open session and ends when you close that session. This context cache of all persistence object you have accessed with that session.

Hibernate "hibernate.cache.provider_class" is used when you want to enable second level cache which is generally used for caching objects for entire process.
this cache is generally used for object that rarely gets updated.
Hint Think about synchronization when same object is accessed my multiple thread.

This also answer your question 6.


2. Does hibernate loads the entire database into it, at the start of the application?


No. hibernate loads object when you need them, when you call session.get(Class,pk) or you are browse interconnected objects. even then hibernate first checks the cache(first and second level) to check the presence of object and if not found, hits database.


3. Suppose I inserted a record in a table, will it be updated with the hibernate copy which it loaded at the start of the application?


Can you please elaborate what you mean by inserted.


4. Suppose I issued a select query, from where will I get the records? Is it from hibernate or from database?


refer 2
Note : when we issue hibernate query, hibernate synchronize the modified object within session, with database(issue update for that object) .


Suppose I closed the session object, will the records in cache be deleted?



yes, first level cache is cleared.

Regards,
Hemant
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hemant Thard wrote:Can you please elaborate what you mean by inserted.



Suppose I have loaded an object using get method. Then I inserted a record in the table associated with that object, will the object be updated with the new row?

PS

Suppose I loaded an object using session.get(class1,pk) then using the same session I loaded the another object say session.get(class2,pk), will the class1 remain in the cache?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hint Think about synchronization when same object is accessed my multiple thread.


Why? 2nd level cache implementations are required to be thread-safe (and can also be transactional).


Note : when we issue hibernate query, hibernate synchronize the modified object within session, with database(issue update for that object) .


No, it won't. You synchronize by flushing the session or updating an object (which will evict it from any second level cache). Issuing a query will not flush the session.
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Paul,


Why? 2nd level cache implementations are required to be thread-safe (and can also be transactional).



I agree with you. But my point of saying this is to answer Question 6 which is "If cache is cleared when session is closed, then why to close the session. I think its not a better idea to close the session. "


and well, to answer this Question

No, it won't. You synchronize by flushing the session or updating an object (which will evict it from any second level cache). Issuing a query will not flush the session.

.

I will quote the exact words written in Java Persistence with Hibernate Page 622 chapter 14 "querying in hibernate and JPA QL" "setting Query hints"

It says


Lets assume that you make modification to persistence object before executing a query. there modification are only present in memory so hibernate flushes the persistence context and all changes to the database before executing your query. This guarantees that the query runs on the current data and that no conflict between the query result and in-memory object can occur.

.







 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are correct. My mistake.

 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok after watching few videos on youtube I got the basic knowledge about caching. But few doubts are still revolving in my mind. Let me explain in detail.

1. What is meant by lazy loading?
2. Suppose when an object with primary key 25 is taken, then later I again asked the same object it is retrieved from the frst level cache. Suppose mean while the record with primary key 25 is updated, will the first level cache makes the update to the object?
3. Is session.clear() necessary?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi chaitanya,


What is meant by lazy loading?



lazy loading is a programming practice in which you only load or initialize an object when you first need it.
this technique is used to keep database connection to a minimum time in managed environment. like most of the time we are dealing with our own business logic rather then performing some database operation. so its bad to keep database connection engage till that time. so hibernate follows lazy loading.



Suppose when an object with primary key 25 is taken, then later I again asked the same object it is retrieved from the frst level cache. Suppose mean while the record with primary key 25 is updated, will the first level cache makes the update to the object?

.

To know this, you need to know how hibernate actually works,

when you retrieve the object, hibernate create 2 copies of the object in cache. Each objects has a version id / time stamp associated with it which is modified upon changes. The first object is returned to the user while the snapshot is maintain in cache.
now when hibernate is flush, hibernate compare the version/time stamp of both the object. if there is difference, hibernate know the object has been modified and the changes are to be synchronized with database. there after it issues an update command.
now to your question :
if from the same session objects are modified, hibernate knows about the change and every time you retrieve the object, modified object is returned.

for example


now second case: when same object is modified through some other way(not within same session)

in this case hibernate doesn't update the object present in first cache
for second level cache, you can configure some expiry time where it will try to refresh the object present in cache.



Is session.clear() necessary?



no its not necessary. its generally used to free first level cache space .
note: session.clear() doesn't flush object.



Hope this helps.
Regards,
Hemant














 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@hemant: Thanks dude, what i understood is

Suppose I opened a database connection, then wrote some business logic, say about 100 lines of business logic which takes milli seconds to execute, then I am using the connection. At the first instance only I dint use the connection, when the business logic is being executed the connection will not be active. Am I right?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Suppose I opened a database connection, then wrote some business logic, say about 100 lines of business logic which takes milli seconds to execute, then I am using the connection. At the first instance only I dint use the connection, when the business logic is being executed the connection will not be active. Am I right?



yes.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hemant.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I got a doubt regarding first level cache. Let me explain it in detail.

Suppose I have a web application(MVC) where there are 2 models. Model1 used to get/load object A, Model2 is used to update object A.

Suppose Model1 is like thisWe all know that for the second time the object is loaded from Session cache, not from database.


Suppose Model1 gets the object A for the first time from table A, in the middle Model2 updates the table A. Then in Model1 the remaining part of code is executed and Model1 tries get the object A. This time it gets the object A from the cache, not from database. But in database we have updated the table A. The update is not reflected in Model1 session.

What about such kind of problems?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi chaitanya,

this is where transaction comes into play and its upto you what kind of transaction strategy you implement.

Regards,
Hemant
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please elaborate?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi chaitanya,

look at the link i posted you on my previous message.
and since this question is different then your original post, would recommend you to create a new topic.

Regards,
Hemant
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hemant, I read.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic