About Vlad Mihalcea

Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.

MongoDB optimistic locking

When moving from JPA to MongoDb you start to realize how many JPA features you’ve previously taken for granted. JPA prevents “lost updates” through both pessimistic and optimistic locking. Optimistic locking doesn’t end up locking anything, and it would have been better named optimistic locking-free or optimistic concurrency control, because that’s what it does anyway.

So, what does it mean to “lose updates”?

A real-life example would be when multiple background tasks update different attributes of some common Entity.
 
 
optimisticlocking1In our example we have a Product Entity with a quantity and a discount which are resolved by two separate batch processors.

  1. the Stock batch loads the Product with {quantity:1, discount: 0}
  2. the Stock changes the quantity, so we have {quantity:5, discount: 0}
  3. the Discount batch loads the Product with {quantity:1, discount: 0}
  4. the Discount changes the discount, so we have {quantity:1, discount: 15}
  5. Stock saves the Product {quantity:5, discount: 0}
  6. Discount saves the Product {quantity:1, discount: 15}
  7. the saved quantity is 1, and the Stock update is lost

In JPA you may provide the @Version field (usually an auto-incremented number) and Hibernate takes care of the rest. Behind the scenes there is a safety mechanism that checks the updated rows number when given a specific version. If no row was updated, then the version has changed and an optimistic locking exception is thrown.

UPDATE Product
SET quantity=1, discount=15
WHERE version=1;

But if your storage is not a RDBMS system but a Mongo database instead, you still want to prevent lost updates. Luckily Spring Data comes to the rescue, as it provides a set of Document oriented annotations, among which you can find a @Version annotation with the same semantic as its JPA counterpart.

An automatic retry mechanism should also be employed, as an optimistic locking exception is a recoverable one. It only needs to reload the latest Entity snapshot, merge the specific attributes and update.

So, Spring Data offers more than base repository support and simple query automation. The optimistic locking add-on provides the proper level of write consistency required by your application QoS.

 

Reference: MongoDB optimistic locking from our JCG partner Vlad Mihalcea at the Vlad Mihalcea’s Blog blog.
Related Whitepaper:

Professional NoSQL

A hands-on guide to leveraging NoSQL databases!

NoSQL databases are an efficient and powerful tool for storing and manipulating vast quantities of data. Most NoSQL databases scale well as data grows. In addition, they are often malleable and flexible enough to accommodate semi-structured and sparse data sets. This comprehensive hands-on guide presents fundamental concepts and practical solutions for getting you ready to use NoSQL databases. Expert author Shashank Tiwari begins with a helpful introduction on the subject of NoSQL, explains its characteristics and typical uses, and looks at where it fits in the application stack. Unique insights help you choose which NoSQL solutions are best for solving your specific data storage needs.

Get it Now!  

Leave a Reply


two + = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books