How to handle transaction using Local Data Adapter

hi,

i’m using localdataadapter to develop a 2-tier client/server program, but i don’t know how to handle transaction.

i’ve read about wiki, but still have no idea. any guideline or example about this topic ?

thanks

Hello,

You can handle transaction from DataAbstractService using onUpdateDataBeginTransaction, onUpdateDataRollBackTransaction, onUpdateDataCommitTransaction events. Also you could access IDAConnection interface from DataAbstractService:
Connection.BeginTransaction;
Connection.CommitTransaction;
Connection.RollBackTransaction;

Read about it http://wiki.remobjects.com/wiki/Transaction_Handling .
http://wiki.remobjects.com/wiki/IDAConnection_Interface

Hope this helps.

yes, i’ve read those article before, but any samples related to this topic ?

because i even don’t know how to handle the business logic/rules when using Local Data Adapter Wizard.

thanks for reply.

Hello,

When you do some changes in your tables and then do either

MemDataTable.ApplyUpdates; or 
LocalDataAdapter.ApplyUpdates;

transactions are apllied automatically, committed or rolled back as needed, depending on whether updates succeeded.
So you don’t need to take care about transactions by default.

If nevertheless you wish to control transactions by yourself then you should handle DataService events onUpdateDataBeginTransaction, onUpdateDataRollBackTransaction, onUpdateDataCommitTransaction that allow you to do this. For example:

procedure TDataService.DataAbstractServiceUpdateDataBeginTransaction(
Sender: TObject; var aUseDefaultTransactionLogic: Boolean);
begin
if Connection.InTransaction then
begin
// check necessary conditions

Connection.BeginTransaction;
aUseDefaultTransactionLogic:=false;
end;
end;

because i even don’t know how to handle the business logic/rules when using Local Data Adapter >Wizard.

In order to use business rules scripting on the server you should drop DAEcmaScriptProvider component on the DataService module and set DataService ScriptProvider property to this component. Thus you will be able to define server side business rules in schema modeler.

For a client side business rules it is necessary to drop TDAEcmaSciptProvider component near the MemDataTable and assign the ScriptingProvider property on that component to the EcmaScriptProvider Class.

Read more here Documentation | RemObjects Software .

If you need more detailed explanation or samples please describe your tasks.

Hope this helps.

hi,

elenap said: transactions are apllied automatically, committed or rolled back as needed, depending on whether updates succeeded.
So you don't need to take care about transactions by default.

what about if there’re two separate dataset (no relationship), i need to applyupdate datasetA, then do some calculation to applyupdate datasetB. how to handle transaction if automatically ? and how to rollback if applyupdate datasetB fail ?

thx & regards

any idea ?

Hello,

Sorry for belated answer.

what about if there're two separate dataset (no relationship), i need to applyupdate datasetA, then do some calculation to applyupdate datasetB. how to handle transaction if automatically ? and how to rollback if applyupdate datasetB fail ?

thx & regards

This case is not trivial as there is no relationship between two datasets.

I attached a testcase with the situation you described, it works with PCTrade Firebird database that you can find in /RemObjects Samples/Data Abstract for Delphi/Server/Bin/Data/PCTrade.fdb . The main idea is to handle transaction manually by the means of onUpdateDataBeginTransaction, onUpdateDataRollBackTransaction, onUpdateDataCommitTransaction events as I said before. But there are some other settings.

Please examine the code and feel free to ask for additional assistance.

Best regards.

Hello,

I’m struggling with transactions too. I tried the VCLApplication example and it works but why can’t you a normal ApplyUpdates for the ORDERS table? Like this:

ServerDataModule.cf.CreateInstance(ServerDataModule.LocalDataAdapter.SessionID,
instance);
(instance as IDataService).StartTransaction;
try
if ServerDataModule.tbl_ORDERS.State in [dsEdit, dsInsert] then
ServerDataModule.tbl_ORDERS.Post;

ServerDataModule.tbl_ORDERLS.ApplyUpdates;
ServerDataModule.tbl_ORDER_DETAILS.ApplyUpdates;

(instance as IDataService).CommitTransaction;
ShowMessage('Transaction commited');

except
(instance as IDataService).RollbackTransaction;
ServerDataModule.tbl_ORDER_DETAILS.CancelUpdates;
ServerDataModule.tbl_ORDERS.CancelUpdates;
ShowMessage(‘Transaction rollbacked’);
end;

Kind Regards,

Michel

Hello,

kasteren said: I'm struggling with transactions too. I tried the VCLApplication example and it works but why can't you a normal ApplyUpdates for the ORDERS table?

This is done in order to return data on the client in it’s original state after rolling back transaction.
If you do tbl.Orders.ApplyUpdates successfully, then tbl_Orders.Delta.Count will be zero and CancelUpdates will do nothing in the case of errors on tbl_Order_Details.ApplyUdates.
So we just split updating data on the server and merging Delta for Orders table.

You wil have the same effect if you do applying updates for two tables in a single call:
RemoteDataAdapter.ApplyUpdates(tbl_Orders, tbl_Order_Details);

But you won’t be able to do some intermediate calculations between applying tables in this case.

Best regards.