What is optimistic and pessimistic LOCK | How to use this lock in spring boot 3 with example

Поділитися
Вставка
  • Опубліковано 28 лип 2023
  • In this tutorial explained how can we use optimistic and pessimistic locking. Also explained how and when to use pessimistic and optimistic locks?
    Locking is a mechanism that allows us to do parallel work with the same data in the database. When more than one transaction tries to access the same data at the same time, locks come into play, which ensures that only one of these transactions will change the data.
    JPA supports two types of locking mechanisms:
    1. Optimistic Locking
    2. Pessimistic Locking.
    Explained both locking mechanism with one example (Reservation System). Booking a ticket with multi thread environment.
    Source Code available here: github.com/JavaaTechSolutions...
    #javatechsolutions #springboot #java #java17 #springboot3 #h2database
    #optimisticlocks #pessimisticlocks
    If you guys like this video please do subscribe now and press the bell icon to not miss any update from Java Tech Solutions
    For Subscribe: / @javatechsolutions

КОМЕНТАРІ • 33

  • @rmkulanthaivelu
    @rmkulanthaivelu 10 днів тому +1

    Great Explanation. Thank you so much.

  • @djkumar9726
    @djkumar9726 8 місяців тому +1

    Thanks for your real-time examples, all your videos are very much useful for real time implementation of different micro service concept. Keep uploading such kind of informative videos

  • @meghanak8854
    @meghanak8854 4 місяці тому

    Thank you for your videos with real time projects. Please make more videos on Spring boot and spring cloud technologies.

  • @chandan07talreja
    @chandan07talreja 9 місяців тому

    Excellent explanation keep videos on spring boot coming 🙌🏻

  • @DurgeshKumawatdk
    @DurgeshKumawatdk 7 місяців тому

    you picked a good example to explain the locking mechanism

  • @kaushikmitra1982
    @kaushikmitra1982 10 місяців тому +1

    good explanation !!!

  • @theunusual4566
    @theunusual4566 8 місяців тому +1

    Hello sir,
    I didn't understand one thing :
    How ticket was saved in db with "BusId" ? Because In the code busDetails was not saved in ticket object..
    Yes.. ticket was added in Bus entity , but still this Bus entity was not saved..
    Pls suggest

  • @Vinoth_Subramani
    @Vinoth_Subramani 4 місяці тому +1

    In the case of rollback scenario which lock type is suitable

  • @jinalmodi4636
    @jinalmodi4636 3 місяці тому +1

    Can this be useful when we have the same application deployed on multiple instances?

  • @nenuanenenuane6645
    @nenuanenenuane6645 8 місяців тому +1

    Hello great vedio is it same process for old springboot versions also?

    • @JavaTechSolutions
      @JavaTechSolutions  8 місяців тому +1

      It should work. Only little bit modification is required as in Java 17 and spring boot 3 modified lots of thing

    • @nenuanenenuane6645
      @nenuanenenuane6645 8 місяців тому

      @@JavaTechSolutions thank you

    • @nenuanenenuane6645
      @nenuanenenuane6645 8 місяців тому

      @@JavaTechSolutions one more doubt is @version annotation and version field needed if we are using Pessimistic locking?
      Can we remove that field and annotation in entity?

  • @mehmetnamver914
    @mehmetnamver914 4 місяці тому

    As i understand This lock mechanism only work for one instance ?
    İf we have 2 instance we should use saga ?

    • @JavaTechSolutions
      @JavaTechSolutions  4 місяці тому

      If any transaction started in any instance then it will complete for that instance only

  • @Vinoth_Subramani
    @Vinoth_Subramani 4 місяці тому +1

    @Version is needed for pessimistic lock also?

    • @JavaTechSolutions
      @JavaTechSolutions  4 місяці тому

      For applying optimistic lock version annotation is required. But for pessimistic locking version annotation not needed

  • @puneetsaneja3007
    @puneetsaneja3007 7 місяців тому +1

    Hi. When should we use saga design pattern or when should we use locking mechanisn ?

    • @JavaTechSolutions
      @JavaTechSolutions  6 місяців тому

      Its depends. Looking mechanism should be used if you want to maintain the data consistency then locking should be used. Even if same data used by multiple thread then locking can be used. Regarding saga design pattern is used based on scenario. It will be used if we want to maintained transaction in multiple microservices.

    • @puneetsaneja3007
      @puneetsaneja3007 6 місяців тому +1

      @@JavaTechSolutions we cant use locking mechanism when there is 3rd party post api. As we dont have source code in that case. Is that make sense ?

    • @JavaTechSolutions
      @JavaTechSolutions  6 місяців тому

      @@puneetsaneja3007 yes... True...

  • @ThanhPhamTien-vr4gf
    @ThanhPhamTien-vr4gf 3 місяці тому

    Waste time , first 40 mins was for project creation , eventually just add 1 annotation and dont explain what it does

    • @JavaTechSolutions
      @JavaTechSolutions  3 місяці тому +1

      Thank you for your concern... But it's in from beginning