• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA Optimistic Locking Test

 
Markus Schmider
Ranch Hand
Posts: 132
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to write a simple test for Optimistic Locking





I would have expected that the entity in the DB has the name "bla" but it has "fasel".
And no OptimisticLockException has been thrown.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Markus Schmider wrote:I would have expected that the entity in the DB has the name "bla" but it has "fasel".
And no OptimisticLockException has been thrown.

Some questions:
- is the version number increased after the first update?
- how many SQL UPDATE statements are executed?
 
Markus Schmider
Ranch Hand
Posts: 132
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see two updates in the log


The Version number is 2 after the test (I delete all entries before each test)

Btw I am using Wildfly 8.2
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Markus Schmider wrote:I see two updates in the log

And what about the parameters used for this query? What's the version of the entity after the first update? And what's the version of the Eintrag instance referred by eintrag2 before the update is executed?
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a single Transaction, so both those updates are part of the one transaction, so both apply.

It's no different to doing an UPDATE followed by another UPDATE then finally committing, directly in the database.
 
Markus Schmider
Ranch Hand
Posts: 132
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave, you're right. Thanks



This works as expected

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Markus Schmider wrote:This works as expected

It might be useful for other ranchers if you provided the (relevant) portion of the EintragDAO class too (as it contains the transaction configuration).
 
Markus Schmider
Ranch Hand
Posts: 132
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator










Because this is an EJB each method has the default TransactionAttribute Required. This means that a Tx begins with each method and ends when the method returns.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Markus Schmider wrote:Because this is an EJB each method has the default TransactionAttribute Required. This means that a Tx begins with each method and ends when the method returns.

Thanks for coming back and sharing your solution. It might be helpful for other ranchers. Have a cow!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic