#1 2016-02-25 21:08:16

fabioxgn
Member
Registered: 2015-11-06
Posts: 34

Rollback a transaction if client timeout

I'm implementing an interface based service and would like to run each request in a database transaction.

My first attempt was to use an interceptor, it worked, something like this:

procedure TRESTServer.TransactionInterceptor(Sender: TServiceMethodExecute; Step: TServiceMethodExecuteEventStep);
begin
  case Step of
    smsBefore: BeginTransaction;
    smsAfter: Commit;
    smsError: Rollback;
  end;
end;

The problem is that if the client timesout and is not able to process the result, the `smsAfter` is executed and the transaction is commited.

From what I've noticed in the code, when the timeout occurs the exception is handled in this code:

on E: Exception do
            // handle any exception raised during process: show must go on!
            if not E.InheritsFrom(EHttpApiServer) or // ensure still connected
               (EHttpApiServer(E).LastError<>HTTPAPI_ERROR_NONEXISTENTCONNECTION) then
              SendError(STATUS_SERVERERROR,E.Message,E);

The problem is that in the case of client disconnect, it just ignores the error and I can't rollback the transaction.

Is it possible to do this somehow?

Offline

#2 2016-02-25 21:22:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Rollback a transaction if client timeout

Currently, the idea is to use a TSQLRestBatch and not use manual transaction.
With such a Batch, the transaction is alive during the process of all items, in a very short time, and with explicit and immediate error/rollback process.

To handle such timeout issues, and let several transactions open concurrently is very difficult to manage and do properly...
This is why we usually go into the UnitOfWork pattern, implemented via a Batch.

Online

#3 2016-02-26 05:30:59

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Rollback a transaction if client timeout

Yes, the batch mode is very efficient and useful!


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2016-02-26 13:10:05

Roberto Schneiders
Member
From: Santa Catarina, Brazil
Registered: 2012-09-19
Posts: 127
Website

Re: Rollback a transaction if client timeout

Unfortunately, this wouldn't work for us.

We need to control the database transaction on the server-side.

We have multiple mormot servers running in parallel (AWS AutoScaling) and receiving the load from a Elastic Load Balancer, so, client requests can hit different servers.

The problem that we are trying to solve is this: A client made a destructive call to the server (Insert/Delete/Update) but the connection was interrupted (client timeout, etc.). In that case, the client would assume that none of the changes are applyed on the database, so, we would like to rollback the transaction if anything goes wrong.

Offline

#5 2016-02-27 10:44:20

Leslie7
Member
Registered: 2015-06-25
Posts: 248

Re: Rollback a transaction if client timeout

AB,

Can TSQLRestBatch be used with the crossplatform units?

Offline

#6 2016-02-27 11:00:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Rollback a transaction if client timeout

Yes, via the BatchStart(), BatchAdd(), BatchUpdate(), BatchDelete() and BatchSend() methods of the cross-platform TSQLRest class.

Online

#7 2016-02-27 16:53:41

Leslie7
Member
Registered: 2015-06-25
Posts: 248

Re: Rollback a transaction if client timeout

Thanks! smile

I have searched the crossplatform units for 'TSQLRestBatch  and came up empty. I should have searched just for 'batch'.

Last edited by Leslie7 (2016-02-27 16:53:53)

Offline

#8 2016-02-29 12:52:01

Roberto Schneiders
Member
From: Santa Catarina, Brazil
Registered: 2012-09-19
Posts: 127
Website

Re: Rollback a transaction if client timeout

So, I can't handle transactions manually on the server side in case of client timeouts?

Offline

#9 2016-02-29 12:53:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Rollback a transaction if client timeout

No, there is no such timeout handling yet, since the pattern is to use TSQLRestBatch for such needs.

Online

Board footer

Powered by FluxBB