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

  Рет қаралды 11,451

Java Tech Solutions

Java Tech Solutions

Күн бұрын

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/Jav...
#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

Пікірлер: 44
@djkumar9726
@djkumar9726 Жыл бұрын
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
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
Thanks
@ramanandkumar7780
@ramanandkumar7780 4 ай бұрын
Thank you, you have explained very well optimistic n pessimistic locking using real time example .
@JavaTechSolutions
@JavaTechSolutions 4 ай бұрын
@@ramanandkumar7780 thank you
@techianki
@techianki Ай бұрын
How can we have the lock if we are using custom queries using Criteria API
@JavaTechSolutions
@JavaTechSolutions Ай бұрын
Lock can happen on the table level
@meghanak8854
@meghanak8854 Жыл бұрын
Thank you for your videos with real time projects. Please make more videos on Spring boot and spring cloud technologies.
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
Thanks
@Vinoth_Subramani
@Vinoth_Subramani Жыл бұрын
In the case of rollback scenario which lock type is suitable
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
Both approach are fine... Depends on your needs
@DurgeshKumawatdk
@DurgeshKumawatdk Жыл бұрын
you picked a good example to explain the locking mechanism
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
Thank you
@rmkulanthaivelu
@rmkulanthaivelu 8 ай бұрын
Great Explanation. Thank you so much.
@JavaTechSolutions
@JavaTechSolutions 8 ай бұрын
You are welcome!
@chandan07talreja
@chandan07talreja Жыл бұрын
Excellent explanation keep videos on spring boot coming 🙌🏻
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
Sure.. Thanks
@kaushikmitra1982
@kaushikmitra1982 Жыл бұрын
good explanation !!!
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
Thanks
@manjunathak4675
@manjunathak4675 3 ай бұрын
you are always fetching 1L record only with the method findwithLocationById(2) can we use like this ? I mean can we pass 3L
@JavaTechSolutions
@JavaTechSolutions 3 ай бұрын
You can pass the I'd in dynamic nature
@nenuanenenuane6645
@nenuanenenuane6645 Жыл бұрын
Hello great vedio is it same process for old springboot versions also?
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
It should work. Only little bit modification is required as in Java 17 and spring boot 3 modified lots of thing
@nenuanenenuane6645
@nenuanenenuane6645 Жыл бұрын
@@JavaTechSolutions thank you
@nenuanenenuane6645
@nenuanenenuane6645 Жыл бұрын
@@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?
@jinalmodi4636
@jinalmodi4636 11 ай бұрын
Can this be useful when we have the same application deployed on multiple instances?
@JavaTechSolutions
@JavaTechSolutions 10 ай бұрын
I believe so
@Akash-yo4nj
@Akash-yo4nj 5 ай бұрын
If possible can we give priority for transaction for eg bookTicket1 have high priority
@JavaTechSolutions
@JavaTechSolutions 5 ай бұрын
We can set the priority for the transaction
@mehmetnamver914
@mehmetnamver914 Жыл бұрын
As i understand This lock mechanism only work for one instance ? İf we have 2 instance we should use saga ?
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
If any transaction started in any instance then it will complete for that instance only
@Vinoth_Subramani
@Vinoth_Subramani Жыл бұрын
@Version is needed for pessimistic lock also?
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
For applying optimistic lock version annotation is required. But for pessimistic locking version annotation not needed
@puneetsaneja3007
@puneetsaneja3007 Жыл бұрын
Hi. When should we use saga design pattern or when should we use locking mechanisn ?
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@puneetsaneja3007 yes... True...
@theunusual4566
@theunusual4566 Жыл бұрын
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
@JavaTechSolutions
@JavaTechSolutions Жыл бұрын
It's a cascading save
@turxandunya4010
@turxandunya4010 5 ай бұрын
Creating entity is not a part of lesson. Pls do it before video. Not in the video
@ThanhPhamTien-vr4gf
@ThanhPhamTien-vr4gf 11 ай бұрын
Waste time , first 40 mins was for project creation , eventually just add 1 annotation and dont explain what it does
@JavaTechSolutions
@JavaTechSolutions 11 ай бұрын
Thank you for your concern... But it's in from beginning
@mercelessmonk957
@mercelessmonk957 7 ай бұрын
you should switch off your clicks buddy
@JavaTechSolutions
@JavaTechSolutions 7 ай бұрын
@@mercelessmonk957 Already taken care.. Thank you
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
DeepSeek R1: the aftermath
27:54
AIcademia
Рет қаралды 3
System Design: Concurrency Control in Distributed System | Optimistic & Pessimistic Concurrency Lock
1:04:45
Using Interceptor in Spring Boot
22:13
The Tech Mojo
Рет қаралды 26 М.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 228 М.
JPA Auditing for Spring boot and Spring Security
26:00
Bouali Ali
Рет қаралды 18 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 919 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН