Code Monkey home page Code Monkey logo

jpa-locking's Introduction

Concurrency control with JPA

A very simple Spring Boot application with a couple of tests to show the Lost Update anomaly and the possibilities to handle it with Optimistic Locking in JPA and Hibernate.

The application was written as a showcase for the following article: https://blog.arnoldgalovics.com/optimistic-locking-in-jpa-hibernate/

LostUpdateTest

The test shows the lost update anomaly. Two concurrent transactions are updating the very same entity and the update made earlier is overwritten by the latter one.

OptimisticLockingTest

The test contains 4 different use-cases

  • Optimistic locking with versioning
  • Optimistic locking with versioning when non-overlapping changes were made to the entity
  • Versionless optimistic locking
  • Versionless optimistic locking when non-overlapping changes were made to the entity

PessimisticLockingH2Test

The test shows how an exclusive lock can be acquired with H2 and also demonstrates that H2 doesn't support shared locks thus Hibernate is falling back to requesting an exclusive lock even in case of an exclusive request.

PessimisticLockingPostgreSQLTest

The test shows off the following use-cases with an embedded PostgreSQL database

  • Shared locks are supported via the FOR SHARE clause
  • Multiple readers can acquire shared lock to the same row
  • One shared lock is already applied and then an exclusive lock tries to be acquired to the same row
  • Locking a row after fetching the entity
  • Locking the result of a query

jpa-locking's People

Contributors

galovics avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.