Thread Synchronisation

Thread Synchronisation

  

We are developing a mobile application in which we do a lot of API calls to remote server with OAuth token authentication. Now we are facing a problem with refreshing the token multiple times when paralel requests are made to the outsystems platform.

When we make a request, we first check if the access token has expired, and if so, we use the refresh token to request a new access token.

But when 2 requests are made at the same time, both requests see that the token has expired and try to refresh the token.

In any other programming language I would use a thread synchronisation technique like a waithandle or mutex to make the second call wait for the first request to finish the refresh of the token, and use the renewed token. How can I achieve this kind of locking ( prefferable in the current user scope ) in outsystems?

So I've got a server action "EnsureToken" which I want only one paralel instance to be able to be running, and other threads to block.

Solution

Hi Roy,

The usual method is to use a GetXxxForUpdate Entity Action, which works like a mutex (but using the database). The Entity should contain an indication whether a refresh is needed: the first one sees it's needed, refreshes, and writes back it's no longer needed. Then the second one, after blocking, sees it's no longer needed.


Solution

Ok, that is an option indeed. But is there a way to release the lock of the record without updating it? I could ofcourse do a update with the old version, but 99% of the time I don't really need to update the record, so it would be better to just release the lock.

The lock is released automatically when the database session ends, or when you explicitly call AbortTransaction.