Invalid java service plus synchronized

as mentioned, I am a distributed project. Now we need to do a card grabbing function. Want to modify a method with synchronized and lock it, but the result is not optimistic

clipboard.png

clipboard.png

I call the receiveVipCard () method of another service from one service, and at first I will determine whether I can get it or not.
my test method is: change the number of cards that can be picked up to 1, and then grab the two phones together, and get both (after the grab, I will take the number of cards that can get-1), but the number of cards that can be received is 0, that is, it is reduced only once, why? Can"t distributed locks be like this?

Apr.10,2021

you have to belong to a database transaction


= my answer =
this is a distributed transaction, not with synchronized, but with distributed locks. There are mainly database-level, redis and zookeeper locks. This time I used redis to implement.

Menu