How to use synchronize blocks in Servoy

Hi,

How is it possible to use synchronized blocks in Servoy? For example, let us say there is an ATM system.
A user should only be allowed to withdraw money from the account after a check to see whether there are sufficient funds available. Hence, the checking of the account balance and withdrawal must happen together. Otherwise, if there are two people accessing the system at the same time, there is the possibility of the account being overdrawn.
To prevent this, we use synchronize blocks in Java so that only one person can access the code at a time.
What I want to know is, what is the method used in Servoy to implement similar functionality?
Thank you.

Regards,
Hareendra

You can lock the account record before doing your actions. When a record is locked only the user that has the lock can work on it.
In your case:

  1. Acquire lock
  2. Check balance
  3. Withdrawal
  4. Release lock

Take a look at databaseManager.acquireLock() and databaseManager.releaseAllLocks().

Hi Joas,

We tried to implement what you have suggested. But still it doesn’t seem to function the required way. The engineer who did this also made a seperate post on the forum as follows:

I developed a simple program to store the click count in a database table. Multiple users click “Click” the same button simultaneously. Then Program counts the number of clicks and store in the database table. Specific user’s clicks can be seen by clicking view clicks button.

Problem

User A clicks “Click” button 10 times and user B clicks “Click” button 10 times simultaneously. At the end, click count value which was stored in database table, should be 20. But the actual value is less than 20. When we check the user clicks, by clicking “view Click” button, then the number of clicks are correct for when totaled for both users.

Click button code

function countClick()
{
var success=false;
if(foundset.find())
{
foundset.rec_no = 1;
foundset.search();
}
databaseManager.startTransaction();
while(!success){
success=databaseManager.acquireLock(foundset,-1);
}
if(success){
foundset.relookup();
foundset.click_count += 1;
clickCounter++;
arr+=‘,’+clickCounter;
if(databaseManager.saveData()){
databaseManager.commitTransaction();
}else{
databaseManager.rollbackTransaction();
}
databaseManager.releaseAllLocks();
}
}

View Click button code

function viewArray()
{
plugins.dialogs.showInfoDialog('Number of Clicks,'Array is : '+arr);
}

Please show me the correct way to get the click count in function countClick() in a multi-user environment.

Please help us sort this issue out. As we have a live system which is becoming quite painful to manage without this feature.

Thanks in advance.

Best Regards,

Nimeshe

Hi Nimeshe,

Other people are trying to get this sorted out, please continue the discussion in the other thread to keep it in one place.