• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about version number in writeLock example in chapter 12

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, in chapter 12, writeLock:







Total cost: $4.7 with uniforms: 1 version: 0
2016-10-29T20:33:45.601-0400|Info: employee :1 add a uniform: 2 version: 1
2016-10-29T20:33:45.678-0400|Info: Total cost: $9.4 with uniforms: 1 version: 1

2016-10-29T20:33:45.699-0400|Warning: Local Exception Stack:
Exception [EclipseLink-5006] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [Employee id: 1 name: Sarah] cannot be updated because it has changed or been deleted since it was last read.
Class> examples.model.Employee Primary Key> 1
at org.eclipse.persistence.exceptions.OptimisticLockException.objectChangedSinceLastReadWhenUpdating......


But as I check on the database table, employee, I saw this:
id  |  name | cost | version
-------------------------------
1     sarah     4.7       2

My question is : the second calculateCleanCost transaction with total $9.4 is rolled back  as the version number is updated from 0 to 1.
But why in the database , the version is  2?

It makes sense to me that the cost is still $4.7 as the second transaction is rolled back..
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I called the addUniform method two more times, I got this:

Total cost: $4.7 with uniforms: 1 version: 0
2016-10-29T22:00:37.597-0400|Info: employee :1 add a uniform: 2 version: 1
2016-10-29T22:00:37.683-0400|Info: employee :1 add a uniform: 3 version: 3
2016-10-29T22:00:37.806-0400|Info: employee :1 add a uniform: 4 version: 5
2016-10-29T22:00:37.831-0400|Info: Total cost: $9.4 with uniforms: 1 version: 1
2016-10-29T22:00:37.838-0400|Warning: Local Exception Stack:
Exception [EclipseLink-5006] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [Employee id: 1 name: Sarah] cannot be updated because it has changed or been deleted since it was last read.
Class> examples.model.Employee Primary Key> 1 ....


The database is showing:
id | name | cost | version
------------------------------
1    sarah     4.7      6

Why the version is 6 instead of 1?
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I assume  :

If two people have a joint bank account , they make transactions at the same time at 12:00 pm.
Person A is making a deposit transaction and Person B is making a withdraw transaction at the same time.
For example, B sees $1000 at the 12:00 pm and make a withdraw of $500.  The balance is $500.
But A  sees $1000 at the 12 :00 pm and make a deposit of $500 and the final balance is $1500 ($1000+$500).
B commits first due to faster network connection and A commits later than B !
Obviously, A overwrites B, and makes the final balance is $1500 ,which is entirely wrong.
The solution is to let B commit the $500 balance and let A knows the balance is updated and retry the transaction.

Back to our case in chapter 12:
Assume the fee management transaction is independent from employee management transaction, just like A and B are two independent transactions , which don't know each other.

Fee management Transaction                            |        Employee management Transaction
--------------------------------------------------------------------------------------------------------------
sees    uniform size = 1                                     |     sees    uniform size = 1
sees employee version  = 0                                |     sees employee version =0   
set cost = 4.7                                                    |
set employee version =1                                    |
as the actual version is still 0 in DB                      |   
-------------------------------------------------------------------------------------------------------------------------------------------------------------                                                                               
                                                                        |     add uniform 2 , sets employee version = 1
                                                                        |     save version+1 =  2 to DB due to OPTIMISTIC_FORCE_INCREMENT
                                                                        |     add uniform 3  ,sets employee version =3 
                                                                        |     save version+1 = 4 to DB due to OPTIMISTIC_FORCE_INCREMENT
                                                                        |     add uniform 4 , sets employee version = 5  
                                                                        |     save version +1= 6 to DB due to OPTIMISTIC_FORCE_INCREMENT
------------------------------------------------------------------------------------------------------------------------------------------------------------
still sees uniform size =1                                     |
still sees employee version =1                             |
Set cost =9.4                                                      |
attempt to set employee version =2, but               |
transaction rollback due to actual version is 6,       |
not 1 anymore!                                                   |
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic