ghostrider
ghostrider

Reputation: 2238

Synchronisation in java

I was asked a use case in one of the interviews where the interviewer asked:

Suppose you have a bank account and you are doing online shopping Your brother has your ATM card and he is about to make a transaction Your father has gone to give a withdrawal cheque in the bank. All these 3 transactions happen at the same time.

How will these transactions be managed such that the balance is not overdrawn?

Me: I said I would use synchronisation on the account balance object

Interviewer : Not satisfied. Next question.

Can someone please explain what could have been the answer? Would a database lock or transaction isolation be a better approach?

I am a beginner in JAVA so forgive my naivety.

Upvotes: 1

Views: 116

Answers (2)

bit-shashank
bit-shashank

Reputation: 921

Concurrency control is a database management systems (DBMS) concept that is used to address conflicts with the simultaneous accessing or altering of data that can occur with a multi-user system. concurrency control, when applied to a DBMS, is meant to coordinate simultaneous transactions while preserving data integrity. 1 The Concurrency is about to control the multi-user access of Database

Basic timestamping is a concurrency control mechanism that eliminates deadlock. This method doesn’t use locks to control concurrency, so it is impossible for deadlock to occur. According to this method a unique timestamp is assigned to each transaction, usually showing when it was started. This effectively allows an age to be assigned to transactions and an order to be assigned. Data items have both a read-timestamp and a write-timestamp. These timestamps are updated each time the data item is read or updated respectively.

Problems arise in this system when a transaction tries to read a data item which has been written by a younger transaction. This is called a late read. This means that the data item has changed since the initial transaction start time and the solution is to roll back the timestamp and acquire a new one. Another problem occurs when a transaction tries to write a data item which has been read by a younger transaction. This is called a late write. This means that the data item has been read by another transaction since the start time of the transaction that is altering it. The solution for this problem is the same as for the late read problem. The timestamp must be rolled back and a new one acquired.

Adhering to the rules of the basic timestamping process allows the transactions to be serialized and a chronological schedule of transactions can then be created. Timestamping may not be practical in the case of larger databases with high levels of transactions. A large amount of storage space would have to be dedicated to storing the timestamps in these cases.

Source:-http://databasemanagement.wikia.com/wiki/Concurrency_Control

Upvotes: 3

Lorelorelore
Lorelorelore

Reputation: 3393

Normally, when you start a project there are some kind of behaviour that must be handled. Transaction isolation is one of these, so I would evaluate the most appropriate transaction level in this case. To know more, I would recommend you to read about view serialization. This is one of the way of doing this.

Upvotes: 1

Related Questions