I have a datatable that I update on client side, and after calling it I need to call a method on the server that do an insert to another table, but both using the same connection.
I need to know how can I start transaction from client side, to do Applyupdates to the DataTable then call the insert method of server in one transaction.
To handle transaction from the client side you need to define service methods from where you should call
Connection.BeginTransaction;
Connection.CommitTransaction;
Connection.RollBackTransaction;
in such a way:
procedure TDataService.StartTransaction;
begin
if Connection.InTransaction then
begin
Connection.RollbackTransaction;
Connection.BeginTransaction;
end
else
Connection.BeginTransaction;
end;
procedure TDataService.CommitTransaction;
begin
if Connection.InTransaction then
Connection.CommitTransaction;
end;
procedure TDataService.RollbackTransaction;
begin
if Connection.InTransaction then
Connection.RollbackTransaction;
end;
It is also necessary to set
aUseDefaultTransactionLogic:=false;
in onUpdateDataBeginTransaction and onUpdateDataBeginTransaction events of DataService.
So on the client side you will have something like this:
For this code to work correctly you should get aware that you call the same Connection instance. The most easy way for this is to off Connection Pooling (http://wiki.remobjects.com/wiki/Connection_Pooling_(Data_Abstract)) :
ConnectionManager.PoolingEnabled:=false;