I'm working on an application that is split into two modules, each in a language (.NET and Java).
The A (.NET) application will manipulate the records of a table while the B (Java) application will handle it at the same time.
Scenario: Imagine that I have a Client and Balance structure in the account.
When the client does not exist, both applications can register it. In order for the client to not be duplicated in the database, a unique one was created in the database (Oracle).
So far so good, there will be no customer twice. The problem begins when both applications are moving the customer account.
Example: Imagine that the .Net application makes a $ 50 deposit. At this moment the record in the table will have 50 balance. But if at the same time of this deposit, a routine was done that removes $ 10.00 from this client's balance.
I would need to control the transaction between two applications, ie if a deposit of $ 50.00 and a cash of $ 10.00 at the same time (hypothetically speaking) was made, the client's final balance should be R $ 40.00.
The scenario I have today, the applications retrieve the customer record, perform the calculation and return the updated value to the table, but there is an overlap of the data. For example, the .NET transaction launches balance of $ 50.00 and the Java application launches $ 10.00 withdrawal at the same time. As far as the .NET application recovers the centralized registry of the client, it has 0 balance as well as the Java application as well. If the .NET application finishes first, it throws a balance of 50, and then the Java application launches a payout of 10, leaving the record in the -10 table.
Is there any way to control this between the two applications only with each application's transaction with yours? I've been thinking and imagined a scenario where there should be a service using LOCK to handle this type of operation. Can you imagine another way to solve it?
Note: It is a legacy system, changing the way the balance is stored would be virtually unfeasible.
Thanks!