Subject RE: [IBO] How do I post multiple datasets in a single transaction?
Author IBO Support List
Mason,

Sorry for my delayed response.

With IBO you have the option to use the inherent and intrinsic capacity for
automated transaction support the database itself provides. You also have
the capacity to use the client-side mechanism of cached updates too, as
there are some instances where this remains a more sensible way, even when
using a transaction enabled back end.

You are mixing the two methods together in the example code you are using
below, which is going to result in nothing but headaches.


You should either use transactions explicitly like this:

...
Dataset1.CachedUpdates := false;
Dataset2.CachedUpdates := false;
...
MyTransaction.BeginTransaction;
try
dataset1.Edit;
...
dataset1.Postt;
...
dataset2.Edit;
...
dataset2.Post;
...
Etc.
Etc.
Etc.
MyTransaction.Commit;
except
MyTransaction.Rollback;
Raise;
end;

Or, you should use cached updates like this:

...
Dataset1.CachedUpdates := true;
Dataset2.CachedUpdates := true;
...
try
dataset1.Edit;
...
dataset1.Postt;
...
dataset2.Edit;
...
dataset2.Post;
...
Etc.
Etc.
Etc.
except
dataset1.CancelUpdates;
dataset2.CancelUpdates;
Raise;
end;
MyTransaction.ApplyUpdates( [] );

Inside of the call to ApplyUpdates() IBO takes care of starting a
transaction on the server for you, then it goes through and performs all of
the updates you had cached up. Then, if everything is successful, it will
commit them and flush out the cache. If not then a rollback will be
performed on the server and your cached updates will remain. So, you go back
where you started with the updates still in your buffers that you can clear
out or do whatever with.


The benefit of this last method is you avoid the possibility of having a
transaction get stuck open for a lengthy period of time while the user is
doing something in your forms. When you used cached updates like this there
is only an open transaction during the time you are calling ApplyUpdates().

Hope this helps.

Jason

-----Original Message-----
From: IBObjects@yahoogroups.com [mailto:IBObjects@yahoogroups.com] On Behalf
Of masonwheeler
Sent: 24 September 2012 03:21 PM
To: IBObjects@yahoogroups.com
Subject: [IBO] How do I post multiple datasets in a single transaction?

If I was using DBX, and I wanted to ensure that changes from multiple
datasets were committed in a single transaction, it would go like this:

tran := connection.BeginTransaction;
try
dataset1.ApplyUpdates;
dataset2.ApplyUpdates;
connection.CommitFreeAndNil(tran);
except
connection.RollbackFreeAndNil(tran);
end;

IBO doesn't seem to work that way. And in fact, as I've just discovered, it
doesn't seem to work *at all* when trying to post multiple datasets in one
transaction.

Here's what I'm trying, using datasets of type TIBOTable:

tran := TIB_Transaction.Create(nil);
try
try
tran.IB_Connection := connection;
tran.StartTransaction;
dataset1.IB_Transaction := tran;
dataset2.IB_Transaction := tran;
dataset1.ApplyUpdates();
dataset2.ApplyUpdates();
tran.Commit;
except
tran.Rollback;
raise;
end;
finally
tran.Free;
end;

The problem with this is that, for some bizarre reason, assigning the
transaction object to the dataset SILENTLY CLOSES THE DATASET, which causes
all the changes that I'm trying to commit to be lost!

What in the world is going on here, and how do I get this to work properly?



------------------------------------

___________________________________________________________________________
IB Objects - direct, complete, custom connectivity to Firebird or InterBase
without the need for BDE, ODBC or any other layer.
___________________________________________________________________________
http://www.ibobjects.com - your IBO community resource for Tech Info papers,
keyword-searchable FAQ, community code contributions and more !
Yahoo! Groups Links