kanaram bhari

Greenhorn
+ Follow
since Sep 02, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by kanaram bhari

Why I am getting deadlock in this code?

I tried to debug it and also read many article about deadlock prevention but could not get this. I have used synchronization, to make thread safe a block of code on the basis of `accountNumber`.

I am getting this `Transaction` object from an API and I want to lock my code on the basis of what the `Transaction` object contain. `Transaction` object contains info like debit/credit account number, amount etc.

Two threads should not be executed `executeTransaction` method simultaneously if there is any common `accountNumber` between them.

Here, `lockedAccount` is storing all accounts that are currently locked and two methods for locking and unlocking an `accountNumber`.

DAO / Repository layer.



   

for more information ,
my **accountDetails** table in database have three columns,

**id(int,primary key)**  

**AccountNumber(String,unique)**  

**amount(double)**


this is Service layer
where i am using `@Transactional` annotation for `executeTransaction` method.




but i am getting database deadlock in this code.
below is my error.