#1 2025-06-19 16:02:10

FatimaEzzahraa14
Member
Registered: 2024-11-22
Posts: 9

Transaction with Multi-Table

As part of a 3-tier architecture (SOA), I need to implement a business function that operates on three different ORM classes, each corresponding to a different table in the database.

My goal is to ensure data consistency by using a transaction: if an error occurs while saving any of the entities, I want all changes to be rolled back.

However, in mORMot 2, the TransactionBegin(Table, SessionID) method appears to support only one class at a time.

Therefore, I am wondering:

         How can I manage a transaction that spans multiple ORM classes (tables) in mORMot?

         Does using transactions in mORMot block other users from accessing or modifying the database during the transaction?

Offline

#2 2025-06-19 16:11:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,069
Website

Re: Transaction with Multi-Table

Multiple ORM classes transaction is indeed not supported.

What you should do instead of ORM-level transaction is to use a TRestBatch, which will write everything at once, could involve several ORM classes, and will create the SQL transactions for you.
And it will also be much faster, without any client-server round trip, or blocking other readers/writers.

Offline

#3 2025-06-19 16:23:47

FatimaEzzahraa14
Member
Registered: 2024-11-22
Posts: 9

Re: Transaction with Multi-Table

Is it possible to give me an example that explains how to use TRestBatch to insert 3 objects, where I need to get the ID of the first object and use it in the second object?

Offline

#4 2025-06-20 06:22:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,069
Website

Re: Transaction with Multi-Table

You can't do this directly, but you can pre-compute the ID e.g. via TSynUniqueIdentifierGenerator.ComputeNew.

Offline

#5 2025-06-20 08:50:16

danielkuettner
Member
From: Germany
Registered: 2014-08-06
Posts: 391

Re: Transaction with Multi-Table

An alternative of TSynUniqueIdentifierGenerator.ComputeNew is using of sequences to compute the ID's before building the Batch.

Offline

#6 2025-06-20 09:14:07

FatimaEzzahraa14
Member
Registered: 2024-11-22
Posts: 9

Re: Transaction with Multi-Table

TSynUniqueIdentifierGenerator — in which unit is it located?

Offline

#7 2025-06-20 13:39:23

flydev
Member
From: France
Registered: 2020-11-27
Posts: 94
Website

Re: Transaction with Multi-Table

@fatimaEzz, unit is "mormot.crypt.secure". 

protip: consider using a specialized tool to perform recursive content searches within a specified folder (/mormot/src), eg. Agent Ransack (free).

Last edited by flydev (2025-06-20 13:39:59)

Offline

#8 2025-06-20 20:05:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,069
Website

Re: Transaction with Multi-Table

Lazarus has an amazing search feature, very fast and simple to use.
You can search within the *.md if you prefer to find a high-level type name.

Offline

Board footer

Powered by FluxBB