Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dirty Checking and Change detection

 
Jana reddyMadam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question regarding Dirty checking. When we modify the session Objects and commit the session , Hibernate automatically finds the change and do the update / insert based on the state.
My question here is - how hibernate will find the difference between the data base state and session object while commit the transaction / session. is it hitting DB ? or it actually stores a copy (which we load from DB) and compare with that copy for a change detection ?

if its storing a copy then someone in DB can change the data while transaction is still in progress/ So still dirty checking works fine ?

Thanks
Jana

 
Richard Chesterwood
Virtual Pair Programmers Rep
Greenhorn
Posts: 29
7
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jana,

Hibernate keeps an "old" copy in the session and when the transaction commits, it compares the state of the two. If the object has changed then an update statement is issued.

If someone (another transaction) has changed the database at the same time, then you need to study at least how optimistic locking works to understand what is going to happen.

Assuming that your database has a loose level of transaction isolation (read committed or repeatable read), THEN by default, the last transaction to commit will simply overwrite the previous transaction.

If you switch versions on (this is where you need to study optimistic locking), then the second transaction to commit will throw an exception which can be caught by the programmer and handled "however" - eg abort the transaction with a warning to the user (sorry, someone got there first) or you can restart a new transaction and go again.

Usually proper locking (pessimistic locking) isn't used because of the deadlocking/liveness/performance problems it causes but it is worth studying this once you've understood what happens in the optimistic case.

Richard.
 
Jana reddyMadam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Very much Richard. I will read about that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic