Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Does Hibernate check dirty objects with database or only with session?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I have a question regarding the way Hibernate works with dirty checking.

Assume I have 2 users A and B performing the same action: retrieving a list of all records in a table, say APPLICATION table. They will get 2 lists which contain the same data.

Now user B changed some data and committed it to database. After that, what will happen if user A also change some data and commit it to database?

I know that Hibernate will do dirty checking, but does it only check with user A's session (which doesn't represent current data in database) or does it check with data in the database? Does it know that user B has changed some data since the time data is retrieved for user A?

(Afaik, session is just a cache, so it which doesn't represent current data in the database)

I'm asking this because I want to force user A to refresh the page to see latest data before changing anything.

Thanks in advance.



 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert Roberto,

First of all, a warm welcome to CodeRanch!

Robert Roberto wrote:I'm asking this because I want to force user A to refresh the page to see latest data before changing anything.


Sounds like you could use some optimistic locking.

Kind regards,
Roel
 
Robert Roberto
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel. That's exactly what I want.

So it looks like I need a "version" column in every table that I want to ensure this condition?

And Hibernate only checks with session and not the database data?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Roberto wrote:So it looks like I need a "version" column in every table that I want to ensure this condition?


Yes!

Robert Roberto wrote:And Hibernate only checks with session and not the database data?


From your example: user A and B get a list of products and both want to update product 5. When user B updates product 5, the value in the version column is updated with value 2 (was 1). user A still sees product 5 with version 1 (and doesn't know about the update of user B). When user A hits the save button of product 5, hibernate tries to execute an update query like UPDATE product SET name = 'New name' WHERE id = 5 AND version = 1. But this query will fail, because this row doesn't exist anymore (only the row with id=5 and version=2 exists). So an exception will be thrown and you can handle it appropriately (inform user A the update failed and show the most recent version of the product).

I used it in one of my previous projects I worked on and it works pretty well
 
Robert Roberto
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarification.
 
Seriously? That's what you're going with? I prefer this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic