-1

I was reading about synchronization in java and have some questions. I understand when multiple threads access same object it leads to data inconsistency. As a solution, whichever thread that own the Object's lock will execute

An example I have seen in many tutorial is online banking; A Husband and Wife spend money from same bank account at same time for $300 each. But the account balance is only $500, so one person's transaction will fail.

My Question: When the husband and wife try to do a transaction on 2 different laptops, each request creates a separate thread and each thread will create a separate bank account object from database (2 threads and 2 bankaccount objects). How synchronization is achieved in this scenario?

Thanks in Advance

Impurity
  • 1,037
  • 2
  • 16
  • 31
Guest
  • 21
  • 8
  • 3
    There are a lot of ways to achieve this. Pessimistic locking, optimistic locking in combination with data versioning, ... – Robby Cornelissen Aug 17 '18 at 01:11
  • 1
    *each thread will create separate bank acount object from database* - prevent this from happening – Scary Wombat Aug 17 '18 at 01:33
  • @Scary Wombat : How to do that? If only one bank object is created for each account holder how long it will be in memory, doesn't it causes performance issues – Guest Aug 17 '18 at 03:59
  • well you can either lock it at the DB level or at the application level - but a bit too broad to discuss in this forum – Scary Wombat Aug 17 '18 at 04:04
  • @ Scary Wombat: if it is application level what design pattern we need to follow so that I will google it and try to understand – Guest Aug 17 '18 at 04:33

2 Answers2

1

Overview

Having N number of BankAccount objects should not matter in this scenario. There are a few situations that this implementation much be handled for so I will step through each one and explain the process.

Single-Server Application

The design would consist of Client's (the local application on the laptop) and the Server (the remote application handling the requests). Having multiple Client applications does not result in multiple Server applications thus allowing for synchronization to be handled on the Server. This way, a FIFO queue of incoming requests can be created, maintained, and executed as the Server sees fit.

In your family example above, the first arriving request will be processed first and receive a success notification. The other will be rejected and receive a failure notification.

Multi-Server Application

This is handled through Load Balancing. Luckily, there are implementations of this for Java. This will abstract away the multiple servers such that it looks as though there is a single server and the behavior is the same as the Single-Server Application. For a bit more on this issue, see this thread.

Database

The Database (DB) should implement either Optimistic or Pessimistic. Although Pessimistic has more integrity, it requires careful design, constant DB connection, and or a transaction id implementation in place of the DB connection. Optimistic could be used for application with a high-volume and no need for sessions, but in a banking scenario Pessimistic might be preferable.

Impurity
  • 1,037
  • 2
  • 16
  • 31
0

You need to use Pessimistic Locking on database. Google it for more details. But in short, you only proceed with second request if no changes were done to original object.

1. First request received
2. Second request received
3. First request loads object from db into accountobject
4. Second request loads the same object from db into accountobject
5. Both request manipulate the object (-300)
6. First request saves object but it checks if no modifications were done, 
   in sql code it may look like: 
       update account set amount = 200 where id = 123 and amount = 500
7. Second request tries to save an account and executes the same query.
   As filter requirement is not fulfilled(amount /= 500) the command isn't executed
denismo
  • 1
  • 1
  • My question is... in web application there will be 2 bank account object which will be created by each thread then how lock mechanism works? or How many bank account object will be created regards to each thread – Guest Aug 17 '18 at 08:36
  • Yes, I missed this 2 bank account objects requirement, I update my original answer – denismo Aug 17 '18 at 15:17