You are not logged in.
Pages: 1
How to use Multi table transaction In the Master/Detail Situation?
I found TransactionBegin/BatchStart both need a TSQLRecordClass parameter, Is that means it supports one table transaction only?
I plan to use servcies, implement transaction on server side.How can I do?
Offline
Pass "nil" for the record class parameter to enable multi table transaction.
Note, if you are using client-side trans, it's strongly recommended to use the batch-mode instead.
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
Pass "nil" for the record class parameter to enable multi table transaction.
Note, if you are using client-side trans, it's strongly recommended to use the batch-mode instead.
thanks. I'll try this.
Offline
Note, if you are using client-side trans, it's strongly recommended to use the batch-mode instead.
Why?
Offline
Sorry. I still have some problems. I am using server-side transaction. I tried two ways, both have problems.
aRestServer.TransactionBegin(nil, 0);
try
aRestServer.Add(aMasterRecord, True);
aDetailRecord.MaserId := aMasterRecord.AsTSQLRecord;
aRestServer.Add(aDetailRecord, True);
aRestServer.Commit(0);
except
aRestServer.RollBack(0);
end;
In this way i can get and set the MasterId property.
But After aRestServer.Add(aMasterRecord, True), data has been written into database already. Commit and RollBack won't do nothing.
aSQLRestBatch.Add(aMasterRecord, True);
aSQLRestBatch.Add(aDetailRecord, True);
aRestServer.BatchSend(aSQLRestBatch, Res);
In this way, transactions works fine. But I don't konw how to set aDetailRecord.MasterId property.
The real problem is that I want use transactions in the Master/Detail situation, and set Master/Detail relationship correctly.
Any help?
Offline
Hi,
Why don t use array of record (detail) as tsqlmasterrecord s property?
Offline
@fubail@qq.com,
I think you at least have two options:
1 - As @lele9 said, re-design your data model, make the "detail table" as a property of the master record, in this case I prefer TObjectList as the type of the property, that way, the "detail records" are stored in JSON in the db, you can then take advantage of the SQLite's JSON feature. If you use another db system, I believe most modern db system has support for JSON. ref: (https://synopse.info/files/html/Synopse … ml#TITL_26)
2 - Control the generation of the ID value for your master records.
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
Hi,
Why don t use array of record (detail) as tsqlmasterrecord s property?
It's not a new project. It should works fine with old data.
And this way is not so SQL, is it?
Offline
@edwinsn
Thanks, I can try option 2.
I want to have an easier way. How do you solve this problem?
Offline
@edwinsn
About data sharding and nesting object lists, we tend to use T*ObjArray, which are freed by the framework automatically, and also stored as JSON.
@fubail
It is in my todo list to add reference of IDs generated during the batch.
For instance, having TSQLREstBatch.Add returning a fake negative ID for the master, which will be recognized when the detailed record is inserted, and replaced by the actual real just-created ID.
But it is not implemented yet.
Offline
@ab
Thanks. It's a good news.
Offline
I'm currently using a timestamp-based UUID generated at client sides, so far so good: https://synopse.info/forum/viewtopic.php?id=2522
@ab,
That's a great proposal! And I'm looking forward to it.
Last edited by edwinsn (2017-01-22 10:46:29)
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
ab,
+1 !!
Offline
For ID generation, you have the TSynUniqueIdentifierGenerator class which was designed for that.
In respect to your Generate64BitUUID, it doesn't use random, so is much safer, and has a process ID, so you can generate unique IDs among several processes/services.
And it is also faster.
In production, for cases with a batch insertion with linked IDs, we use TSynUniqueIdentifierGenerator.
And it also fits very well the need of gathering some information from several uncoupled nodes into a shared MongoDB bigdata storage, as we designed.
You can obfuscate the ID into some hexadecimal value: we use it for public HTTP URI generation, to safely return some resources (like photos or videos) by convention.
Offline
Pages: 1