Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have to develop a banking system which allows users to deposit or withdraw in accounts.
I have implemented the whole logic,now the requirement is that an account cannot have negative balance,I am stuck here.

Multiple users can with-draw from a single account. How can I implement the negative balance in-case of multiple concurrent users(or requests)?
eg User A and B with-draws at same time, User A's request finish first and the account got negative balance,then how can I give error to user B?

What I have tried:

I have created WCF Restful service with two methods for deposit and withdrawal. I am checking account balance while with-drawing.
Posted
Updated 9-Jun-17 9:28am
Comments
Afzaal Ahmad Zeeshan 7-Jun-17 15:48pm    
Then that is the part of the data layer, where you make sure that the data with negative values never gets stored. What is wrong?

Also in the case where multiple users are connecting and performing some tasks on the data source, make sure you are implementing any mechanism of locks, so that only one account (or user, or thread, or process) can access the content. Otherwise data might be left inconsistent.
Member 13246559 7-Jun-17 16:20pm    
Can you please provide some sample code for this?

1 solution

As Afzaal pointed out, you need to wrap your each transaction inside a unit-of work and lock it, to make it truly ACID transactions. At the end of each transaction, you need to watch whether the balance has reached below specified value (or negative). Even though there is concurrency, the subsequent transactions will wait until the current is finished.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900