0

Suppose user_1 and user_2 accessed an employee with id = 101, name = Rahul, accountBalance = 1500 at the same instant. so both user have employee with above mentioned values in their hand now. User_1 added a 1000 to accountBalance and updated employee. so his balance is 2500 now in the database.

And immediately after user_1 updates the employee, user_2 adds a 500 to account balance. so 500 will be added to employee's account balance which is in user_2's hand currently. so 1500+500 =2000.

But, actually it should be (1500+1000)+500 = 3000.

How this problem is handled in java with hibernate?

Do we want to check for the latest value again in the business method updateEmployee() and then add amount to that new accountBalance?

Please help me to get this situation handled in proper way. thanks and regards

amFroz
  • 95
  • 1
  • 12

2 Answers2

2

You do that by enabling optimistic locking, using a field annotated with @Version. At each update, Hibernate will check that the version is the same in database as it is in memory, and increment the version. If the versions don't match, it will throw an exception.

More about optimistic locking in the documentation.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Okay.So let me ask. am doing the app with JSF/primefaces in the presentation layer, Spring in the business layer and hibernate in the data access layer. am fetching the employee detail in the employeeEdit managed bean via employeeService. employeeService gets the employee object with id = 1 with the help of employeeDao. so, once both users read the employee in the same way, the employee account balance will be displayed in the web page as 1500. after adding 1000 by user_1 too it remains same(1500) in the user_2's view(that is, in the backing bean). – amFroz Jul 04 '12 at 01:19
  • so, what is the generally followed way to handle such app. user_2 adds his 500 to account balance of the employee object(with id = 1) that is already in the backing bean. what is the solution – amFroz Jul 04 '12 at 01:19
  • That's aJSF question, and I don't know much about JSF. I guess the backing bean is stored in session, and thus displays stale data. – JB Nizet Jul 04 '12 at 05:41
  • Okay. but leave the JSF part in my question. In spring MVC, struts and all for every view there will be a backing bean like java class controllers I mean. What I'm trying to understand is, once we load the employee object with id =1 using hibernateTemplate.get(Employee.class, 1), user_1 and user_2 both have their own employee object with same identifier. right? – amFroz Jul 04 '12 at 07:29
  • so, if we are taking the case of a struts, the view will have a backing bean. the backing bean will have the employee object as its property. this employee will the employee we fetched using hibernate method. at the time of submitting the changes to DB by user_2, should we have to check the employees most recent field values by hitting the database aging in the business layer? – amFroz Jul 04 '12 at 07:41
  • Sorry Nizet, don't get fed up with my question. very difficult to make you understood of my problem. ca I send you a sample application? :) I'm sitting with this problem for many hours and days. please help if you can. – amFroz Jul 04 '12 at 07:44
  • Hibernate will check for you that nobody else has updated the employee since you loaded it. That's the point of optimistic locking. So user 2 will get an exception because it tries to update the account, and user1 has already updated it since user2 loaded the account. – JB Nizet Jul 04 '12 at 07:57
  • am using hibernateTemplate.get() method. so the proxy is lost. right? so, the dirty checking too is lost. I'm not sure about it, if I'm, then how the changed value will be known to the hibernate? Also, in the situation, I have to use get method and not the load method, coz, user need thinking time in this page for updating the account balance. – amFroz Jul 04 '12 at 08:00
  • Read my answer. And read the documentation I linked to. It explains how it works. It uses a version field in the *entity* itself. Proxies have nothing to do with this matter. – JB Nizet Jul 04 '12 at 08:10
1

User versioning concept of Hibernate. In that case if user_2 updates the same record, hibernate checks that same record has been modified before updating, if so, throws exception.So, your app has to handle that exception and let the user_2 informed of changes in record and to reload the record again.

Pokuri
  • 3,072
  • 8
  • 31
  • 55
  • Okay.So let me ask. am doing the app with JSF/primefaces in the presentation layer, Spring in the business layer and hibernate in the data access layer. am fetching the employee detail in the employeeEdit managed bean via employeeService. employeeService gets the employee object with id = 1 with the help of employeeDao. – amFroz Jul 04 '12 at 01:05
  • so, what is the generally followed way to handle such app. user_2 adds his 500 to account balance of the employee object(with id = 1) that is already in the backing bean. what is the solution – amFroz Jul 04 '12 at 01:20