#1 2022-10-28 15:54:39

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

TRestClientDB or direct access to TRestServerDB for a server

Looking an example from mormot2 I noticed that for standalone applications it exists also TRestClientDB that can be created to connect to external databases also through SQLITE.
I noticed that TrestClientDB can be created also with the following contructor:

    /// initialize the class, for an existing TRestServerDB
    // - the client TOrmModel will be cloned from the server's one
    // - the TRestServerDB and TSqlDatabase instances won't be managed by the
    // client, but will access directly to the server
    constructor Create(aRunningServer: TRestServerDB); reintroduce; overload;

Having all past problems with multiple threads trying to add to a database new records or updating others I am wondering:
When having multiple threads to use a TRestServerDB instance for the same tables, is it better to use TRestServerDB's orm interface directly ?
Or use one TrestClientDB instance for each thread with the above constructor called with the same TrestServerDB instance ?
What are the pros/cons?
Thank you in advance

PS I can not find the TSQLRestClientRedirect somewhere. Is it implemented?

Note: To help better understand my case:
I have clients that call a rest interface procedure but for a work that will take time and requests info from other API servers too. So the REST request is finished and the job is entered in a TSynThreadPool. So, later a thread will have to do all database work to provide the response through websockets. So, the above discussion is about threads running from a TSynThreadPool and accessing a main-central TRestServerDB instance.
I believe that a REST interface procedure in a server called by a client will not have problems to access a main-central TRestServerDB instance to do database work

Last edited by dcoun (2022-10-28 16:10:03)

Online

#2 2022-10-29 10:32:09

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 211

Re: TRestClientDB or direct access to TRestServerDB for a server

is it better to use TRestServerDB's orm interface directly ?

that's how you're supposed to use it.  The only reason you can still call Add/Update/Delete directly from the TRestServerDB instance is because it's there for backwards compatibility with mORMot 1. If you define PUREMORMOT2 flag for your project then you will see those direct methods no longer available.

I believe that a REST interface procedure in a server called by a client will not have problems to access a main-central TRestServerDB instance to do database work

By default there is a lock at orm level so all threads orm access will be properly synchronized, shouldn't have a problem.

Last edited by pvn0 (2022-10-29 10:34:05)

Offline

#3 2022-10-29 10:57:52

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: TRestClientDB or direct access to TRestServerDB for a server

pvn0 wrote:

is it better to use TRestServerDB's orm interface directly ?

that's how you're supposed to use it.  The only reason you can still call Add/Update/Delete directly from the TRestServerDB instance is because it's there for backwards compatibility with mORMot 1. If you define PUREMORMOT2 flag for your project then you will see those direct methods no longer available.

Thank you for response. I apologize, I am not sure I understand the response
Do you mean that I should use a TrestClientDB.Create(aRunningServer: TRestServerDB) ?
I already have enabled PUREMORMOT2, I create a TRestServerDB instance before the http server and I can still have access to TRestServerDB.orm.add

Online

#4 2022-10-29 11:08:01

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 211

Re: TRestClientDB or direct access to TRestServerDB for a server

dcoun wrote:

Do you mean that I should use a TrestClientDB.Create(aRunningServer: TRestServerDB) ?

No, you should use an instance of TRestServerDB directly on server side.

dcoun wrote:

I already have enabled PUREMORMOT2, I create a TRestServerDB instance before the http server and I can still have access to TRestServerDB.orm.add

Yes, that is the correct way.
example:
FDatabase.Orm.Add where FDatabase is an object instance of TRestServerDB class.

Last edited by pvn0 (2022-10-29 11:08:44)

Offline

#5 2022-10-29 11:09:28

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: TRestClientDB or direct access to TRestServerDB for a server

OK Thanks a lot

Online

Board footer

Powered by FluxBB