Lazarus

Programming => Databases => Topic started by: incendio on November 24, 2021, 07:39:14 am

Title: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 24, 2021, 07:39:14 am
Hi guys,

I have this situations :

I have an TDBEdit, with value = 'abc', then edit its value to 'abcde'

To Update database, I clicked Save button, which codes something like this
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnSaveClick(Sender: TObject);
  2. var
  3.   Tbl : array [1..1] of TDataSet;
  4.  
  5. begin
  6.   Tbl[1] := Dt;
  7.   Dba.ApplyUpdates( Tbl);
  8.   ShowMessage('saved');
  9. end;
  10.  

That code works OK when network connection is OK, database value updated to 'abcde', but when network connection is lost, Dialog box showed with error message and option to click OK or Abort.
Error reading data from connection

That error was expected, if users click Abort, app closed, if user click OK, app not closed, value of TDBEdit in the screen still 'abcde'.

Problem is, when network connection restored again while app still open/ not restarted again, clicked again button Save still got same error like above.

Is IBX able to save data to the data  when network connection is restore again without need to restart the app?
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 24, 2021, 11:01:22 am
I am not the best person to answer this, and hopefully somebody with specialist knowledge will come along shortly.

In general, I think you have to assume that the basic database components and underlying libraries require that a connection stays live. In addition, I think you should assume that the server is likely to abort a transaction if the connection goes down.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: korba812 on November 24, 2021, 12:04:36 pm
Is IBX able to save data to the data  when network connection is restore again without need to restart the app?
I guess that's not possible. Queries linked to a dataset are prepared when the dataset is opened and are associated with the given connection.
If you cannot guarantee a permanent connection to the base, consider another solution, e.g. a web service or some RCP.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 24, 2021, 12:18:12 pm
That is too bad.

I have DevArt VCL for C++ builder that can handle database operation under unstable network.

I will ask them if their LCL for Lazarus also have the same features.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: Zvoni on November 24, 2021, 12:33:35 pm
a "manual" Workaround could be
1) Check Connected-Property before saving. If True everything OK, if False you have no Network.
2) If 1) is answered False, instead of "saving" implement a "queue" (Could be a StringList with the SQL-Statement).
You would also need a Procedure to try to reconnect back (own Thread?). TIBConnection's Ancestor TCustomConnection offers a published "AfterDisconnect"-Event. You could start the reconnect try from there. You would need a logic to exempt this code, if the User really wants to disconnect.
3) TIBConnection's Ancestor TCustomConnection offers a published "AfterConnect"-Event. If in 2) you reconnect successfully, "AfterConnect" should fire, and you could check if there are SQL-Statements waiting in the Queue
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 24, 2021, 12:52:00 pm
a "manual" Workaround could be
1) Check Connected-Property before saving. If True everything OK, if False you have no Network.
2) If 1) is answered False, instead of "saving" implement a "queue" (Could be a StringList with the SQL-Statement).
You would also need a Procedure to try to reconnect back (own Thread?). TIBConnection's Ancestor TCustomConnection offers a published "AfterDisconnect"-Event. You could start the reconnect try from there. You would need a logic to exempt this code, if the User really wants to disconnect.
3) TIBConnection's Ancestor TCustomConnection offers a published "AfterConnect"-Event. If in 2) you reconnect successfully, "AfterConnect" should fire, and you could check if there are SQL-Statements waiting in the Queue

@Zvoni: can you confirm that the DB-aware grids etc. aren't really suitable for this sort of thing, and that if a user wants to continue to show the current row etc. he should parse/copy the data into standard (i.e. non-DB) components?

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: Zvoni on November 24, 2021, 01:12:01 pm
Mark,

honestly? No Idea. Never used DB-bound controls (never in 20 years of Visual Basic, and never in 4 years of Lazarus),
since i want to do the CRUD's on a Database myself (more fine-control).

But i think you're on the right path.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 24, 2021, 01:30:26 pm
Checking Connected property isn't enough.

When there are a lots of data, such as in the Dbgrid, network connection, could be disconnected and reconnected again during ApplyUpdate process.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: Zvoni on November 24, 2021, 02:03:19 pm
Then the only "fail"-safe method i know of would be to use a local database, which replicates itself to the server if network is available
https://www.firebirdfaq.org/faq249/
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 24, 2021, 02:07:24 pm
honestly? No Idea. Never used DB-bound controls (never in 20 years of Visual Basic, and never in 4 years of Lazarus), since i want to do the CRUD's on a Database myself (more fine-control).

But i think you're on the right path.

Where I've got to in the past is that for real robustness I needed to do app the result parsing myself, in at least one case with scripted control of queries. However there's a real possibility that Delphi/Lazarus developments have moved beyond that, hence the question.

However I'd imagine that a hybrid approach would be viable: use the DB-aware controls for all of the parsing etc. with the result shadowed onto a standard control. If the program has reason to think that the connection is bad (which might take a ping etc.) it switches the shadows to the foreground.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 24, 2021, 02:29:00 pm
Then the only "fail"-safe method i know of would be to use a local database, which replicates itself to the server if network is available
https://www.firebirdfaq.org/faq249/
I think local database is to complicated.

Not all data needs to be replicated, only the data that are being input / edit by users.

I think of table in memory that holds all data before it is updated to the database server.

I don't know much of Lazarus, perhaps there is a LCL components that could be served for storing temporary data in memory.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: Zvoni on November 24, 2021, 02:36:29 pm
I think of table in memory that holds all data before it is updated to the database server.

I don't know much of Lazarus, perhaps there is a LCL components that could be served for storing temporary data in memory.
And that's different to my proposal of a Queue, how?
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 24, 2021, 02:45:56 pm
I think of table in memory that holds all data before it is updated to the database server.

I don't know much of Lazarus, perhaps there is a LCL components that could be served for storing temporary data in memory.
And that's different to my proposal of a Queue, how?

No, it's the same with your ideas.

But my question is not that, I want to know whether IBX has capabilities to handle database operations under unstable network connection.

It seems that it's not.

I will try to implement yours ideas for database operations over internet. Thanks.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: Zvoni on November 24, 2021, 03:08:55 pm
But my question is not that, I want to know whether IBX has capabilities to handle database operations under unstable network connection.
I wouldn't know of a component in any language capable of that. It's the programmers job to provide for that eventuality.
As i said: the only "fail"-safe method i know off is a replication-setup, with a proxy in between application and database-server(s) switching servers if the one you're connected to goes down
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 24, 2021, 03:16:42 pm
Please treat this as a rhetorical question.

I think local database is to complicated.

Do you have any idea whatsoever, how often an old-timer such as myself has heard an admittedly-inexperienced user say "Your solution is too complicated, I know better"?

However, I think the important thing to appreciate here is that the local database doesn't have to be the same as the backend one. For simple data entry you might do better using e.g. SQLite locally, with the upload carefully wrapped in transactions to make sure it's atomic.

And that approach /is/ different from a queue, unless your queue implementation is enhanced by a whole lot of non-volatile storage and robustness protection... in which case you've just reinvented the wheel and written a database server.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: Zvoni on November 24, 2021, 03:21:29 pm
Not all data needs to be replicated, only the data that are being input / edit by users.
That's the core-definition of Replication.
Only Data which has been
1) Added
2) Changed
3) Deleted
gets (its statement) replicated. No change=No Replication

And Mark described it perfectly
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: tonyw on November 24, 2021, 05:55:31 pm
Hi guys,

I have this situations :
  • Use IBX for database operations
  • Use TIBDataset with CacheUpdate Enabled

I have an TDBEdit, with value = 'abc', then edit its value to 'abcde'

To Update database, I clicked Save button, which codes something like this
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnSaveClick(Sender: TObject);
  2. var
  3.   Tbl : array [1..1] of TDataSet;
  4.  
  5. begin
  6.   Tbl[1] := Dt;
  7.   Dba.ApplyUpdates( Tbl);
  8.   ShowMessage('saved');
  9. end;
  10.  

That code works OK when network connection is OK, database value updated to 'abcde', but when network connection is lost, Dialog box showed with error message and option to click OK or Abort.
Error reading data from connection

That error was expected, if users click Abort, app closed, if user click OK, app not closed, value of TDBEdit in the screen still 'abcde'.

Problem is, when network connection restored again while app still open/ not restarted again, clicked again button Save still got same error like above.

Is IBX able to save data to the data  when network connection is restore again without need to restart the app?

IBX depends on the Firebird Client library as does any Firebird database client. This does not have any explicit code to recover from lost connections. When a connection is lost, the Firebird server will put the current transaction into limbo which, in theory, could allow a database adminstrator to recover any "lost" data. However, that is not always an easy procedure.

In the case of an unstable connection, your strategies are:

1. In your client systems firebird.conf file set DummyPacketInterval=0. This will avoid timeouts on inactive connections when the underlying network is unstable. However, this will not help drop outs during an update. Could be problematic.

2. Don't use ApplyUpdates or cached updates in general. All this does it make it more likely that you will lose all your updates when a connection fails. Instead, set each dataset's AutoCommit property to acCommitRetaining. This will ensure that every update is written through to the database i.e. minimising the risk of data loss.

3. Add an event handler to your TIBDatabase component to automatically re-open the database after an unexpected disconnection. You will probably want to use Application.QueueAsyncCall in the event handler to queue the re-open to ensure a tidy-up before re-opening. Your re-open procedure should re-open all previously open datasets. They should then open in the state they were in after the last autocommit.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 25, 2021, 04:01:56 am

1. In your client systems firebird.conf file set DummyPacketInterval=0. This will avoid timeouts on inactive connections when the underlying network is unstable. However, this will not help drop outs during an update. Could be problematic.

2. Don't use ApplyUpdates or cached updates in general. All this does it make it more likely that you will lose all your updates when a connection fails. Instead, set each dataset's AutoCommit property to acCommitRetaining. This will ensure that every update is written through to the database i.e. minimising the risk of data loss.

3. Add an event handler to your TIBDatabase component to automatically re-open the database after an unexpected disconnection. You will probably want to use Application.QueueAsyncCall in the event handler to queue the re-open to ensure a tidy-up before re-opening. Your re-open procedure should re-open all previously open datasets. They should then open in the state they were in after the last autocommit.

1. Usually, I don't install firebird in client, only supplied my app + fbclient.dll to client.

2. Back in C++ Builder, always use ApplyUpdates. If I use dataset's AutoCommit, in Master Detail table, wouldn't it be problems when only data in Master table saved, while data in Detail not saved due to some errors?

3. I am able re-open the database with this code
Code: Pascal  [Select][+][-]
  1. procedure TForm1.OpenDt();
  2. begin
  3.   Dba.Close(true);
  4.   Dba.Open;
  5.   Trs.Active:=true;
  6.   Dt.Open;
  7.  
  8.   if (Dba.Connected) then ShowMessage('data opened');
  9. end;
  10.  
  11.  

Dba must be closed first (which means lost all data entry/edit from users), otherwise, when tried to saved again, same error about reading connection occur again.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 25, 2021, 09:01:13 am
1. Usually, I don't install firebird in client, only supplied my app + fbclient.dll to client.

I think he means the "client software", i.e. the libraries etc. which are installed on any computer which wants to talk to the server.

Going back to the original question, any unique or sequential columns will obviously need to be handled specially since the value to be stored won't be known until on the actual server.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: tonyw on November 25, 2021, 03:15:12 pm

1. Usually, I don't install firebird in client, only supplied my app + fbclient.dll to client.

2. Back in C++ Builder, always use ApplyUpdates. If I use dataset's AutoCommit, in Master Detail table, wouldn't it be problems when only data in Master table saved, while data in Detail not saved due to some errors?

3. I am able re-open the database with this code
Code: Pascal  [Select][+][-]
  1. procedure TForm1.OpenDt();
  2. begin
  3.   Dba.Close(true);
  4.   Dba.Open;
  5.   Trs.Active:=true;
  6.   Dt.Open;
  7.  
  8.   if (Dba.Connected) then ShowMessage('data opened');
  9. end;
  10.  
  11.  

Dba must be closed first (which means lost all data entry/edit from users), otherwise, when tried to saved again, same error about reading connection occur again.

The main issue with Master/Detail is that you usually need to make sure that changes to master and detail tables are posted in the correct order to avoid foreign key constraints being violated. If you are using master/detail then yes, you do usually want to post the changes in the correct order and then commit them all at once. ApplyUpdates does not help you here any more than a standard Post. ApplyUpdates is used with cached updates and can be more problematic with Master/Detail than using non-cached updates. This is because it is very easy to get the update order wrong if you are mixing insert and delete actions on the master record and using cached updates on the detail.

I would advise using proper transaction control instead of cached updates. When you have an unstable connection, you want to commit as often as makes sense for your application as that minimises the risk of a transaction being lost or ending up in limbo. With a simple one table at a time approach, AutoCommit works. With Master/Detail then you have to be cleverer.

If you are reacting to an exception that reports the loss of a connection then yes, you should explicitly close the database "IBConnection.Active := false;" before re-opening and telling your user that the last transaction may have been lost.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 26, 2021, 05:53:15 am
Thanks to all who gave me all info/hints regarding this topic.

I think we can agree that, there are no build in features in IBX to resume database operations (insert/update/delete) on an unstable network.

Build in -> no settings in properties/ events to create to automatically do this.

Developer must implement his/her own method to handle this situation.

I will try to test methods that is simple and easy to implement. Perhaps, I will open a new topic to discuss this.

Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 26, 2021, 09:39:29 am
I think we can agree that, there are no build in features in IBX to resume database operations (insert/update/delete) on an unstable network.

Ditto in Lazarus/LCL/FPC itself :-/

The one thing I would suggest is that before you put too much effort into reinventing the wheel you investigate whether any other database backend has something in its client libraries which does this.

Apart from that a generalised discussion as to how to do it would probably be of interest to quite a lot of people.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 26, 2021, 09:56:06 am

The one thing I would suggest is that before you put too much effort into reinventing the wheel you investigate whether any other database backend has something in its client libraries which does this.

I don't know about other databases, only playing with Firebird.

I think, it is not because of Firebird.

I tried JDBC with Firebird, unstable network is not a problem. With JDBC, when connection is broken, it will throw an error, but as soon as connection restored again, it can resume inser/edit /update data normally without a need to close and reopen connection again.

Also tried LCL from DevArt, only need to set three properties on the connection component to solve this problem.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 26, 2021, 10:18:54 am
Also tried LCL from DevArt, only need to set three properties on the connection component to solve this problem.

More detail please.

However note that anything at the client end is (probably) only a partial fix: a broken connection will still almost certainly abort a transaction irrespective of server type.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 26, 2021, 12:00:37 pm
Also tried LCL from DevArt, only need to set three properties on the connection component to solve this problem.

More detail please.

However note that anything at the client end is (probably) only a partial fix: a broken connection will still almost certainly abort a transaction irrespective of server type.

MarkMLl

With DevArt's LCL
1. Set 3 properties on connection component
2. Execute ApplyUpdate, if connection not available, it will wait for sometime before throw an error
3. If connection became available again before it throws error, ApplyUpdate automatically becomes successful.
4. If connection still unavailable, it will throw an error, but you can Execute ApplyUpdate again to try again.

There's a trial version of DevArt LCL, you can try it yourself.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 26, 2021, 12:06:27 pm
There's a trial version of DevArt LCL, you can try it yourself.

Link please. Not something I'm familiar with.

If this is specifically an LCL thing but still uses the underlying FCL/RTL it might be reportable as a fixable bug or at least wishlist item.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 26, 2021, 01:02:19 pm
There's a trial version of DevArt LCL, you can try it yourself.

Link please. Not something I'm familiar with.

If this is specifically an LCL thing but still uses the underlying FCL/RTL it might be reportable as a fixable bug or at least wishlist item.

MarkMLl

https://www.devart.com/ibdac/download.html
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 26, 2021, 01:33:56 pm
OK, but that is "for Lazarus" rather than "fixes the LCL".

Nobody denies that the disconnect/reconnect problem should be fixable, although transactions might be aborted by an unexpected disconnection.

Also the limited time I can spare to look at the documentation suggests that it doesn't buffer when disconnected... or that only buffers up to the limits of the visual components and then requires a connection before work can continue.

https://www.devart.com/ibdac/IBDAC.pdf (section 4.7, "Working in an unstable network").

At the moment the best way I can see of fixing this in the general case is to use the database-specific LCL components to collect data, copy the data into non-DB components for editing/display, and buffering locally-enetered data using e.g. SQLite subject subject to requirements of sequential fields etc.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 26, 2021, 01:56:53 pm
OK, but that is "for Lazarus" rather than "fixes the LCL"

MarkMLl
What do you mean? Sorry, I am new to Lazarus, isn't LCL is short for Lazarus Components Library? Is there a different between Lazarus and LCL?


Also the limited time I can spare to look at the documentation suggests that it doesn't buffer when disconnected... or that only buffers up to the limits of the visual components and then requires a connection before work can continue.

https://www.devart.com/ibdac/IBDAC.pdf (section 4.7, "Working in an unstable network").

At the moment the best way I can see of fixing this in the general case is to use the database-specific LCL components to collect data, copy the data into non-DB components for editing/display, and buffering locally-enetered data using e.g. SQLite subject subject to requirements of sequential fields etc.

MarkMLl

I don't know if there is a buffer or not in IBDac, so far, never experienced data lost when using this components in C++ Builder.

For now, I still thinking whether to use Sqlite or  sql scripts as a buffer. Perhaps sql scripts is simpler than Sqlite.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 26, 2021, 02:26:24 pm
What do you mean? Sorry, I am new to Lazarus, isn't LCL is short for Lazarus Components Library? Is there a different between Lazarus and LCL?

(Sigh) This is not something that fixes a problem in the LCL. It is something that exists in various variants that install onto a Delphi or Lazarus development system. Go read the description.

Quote
I don't know if there is a buffer or not in IBDac, so far, never experienced data lost when using this components in C++ Builder.

There isn't. Read the docs.

Come on man, you're the one who threw that particular hat into the ring: it's your job to know what it actually does.

Quote
For now, I still thinking whether to use Sqlite or  sql scripts as a buffer. Perhaps sql scripts is simpler than Sqlite.

Both are viable, but I'm not sure how you'd get an SQL script out of a DB-aware component. Use components as a front-end for a local SQLite database and "all" you'll need will be something non-graphical that detects when the backend is contactable (using ping etc.) and transfers the local data in the context of a pair of nested transactions.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 26, 2021, 04:31:48 pm
If I use Sqlite, I don't know, perhaps for every datasets, there must be a correspondence Sqlite tables. If I have 4 datasets in a form, I have to create 4 Sqlite tables for buffer.

...which I'd expect you to be able to do under program control.

Quote
With sql scripts, only series of strings for all datasets.
I can store these strings onto Dbstring grid, then send them all to database server.

...and if the user turns the computer off before it's synced with the server zhe loses work.

Quote
Back in C++ builder, I have functions that can convert values stored from any datasets to sql scripts, plan to convert those functions to Lazarus functions.

Certainlt looks worth doing, but will that capture locally-entered data?

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: Zvoni on November 26, 2021, 04:32:51 pm
Remember that a ping is not sufficient.
Ping the machine, machine answers „ok, I’m online“, but the db-server/service is not running
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 26, 2021, 05:47:24 pm
Remember that a ping is not sufficient.
Ping the machine, machine answers „ok, I’m online“, but the db-server/service is not running

Yes, but at that point an attempt at reconnecting will fail. A ping is a good first step provided you know the IP address, some schools of thought have it that you should do a database server name lookup manually so that connections are not also at risk of failure due to a failed DNS server.

There's other useful tests, but depending on OS things like finding the current gateway and next-hop router can be surprisingly tricky.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 27, 2021, 05:29:41 am
Certainlt looks worth doing, but will that capture locally-entered data?
MarkMLl

Yes


Yes, but at that point an attempt at reconnecting will fail. A ping is a good first step provided you know the IP address, some schools of thought have it that you should do a database server name lookup manually so that connections are not also at risk of failure due to a failed DNS server.

There's other useful tests, but depending on OS things like finding the current gateway and next-hop router can be surprisingly tricky.

MarkMLl

No need for ping. I think using IBX only + cache update as a buffers, and stringlist to hold sql script are enough to get the job done.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 27, 2021, 08:37:48 am
Please keep us updated.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: tonyw on November 27, 2021, 11:43:35 am
This thread does seem to be going off at a tangent to the original post.

What you need to decide is what are you trying to achieve in terms of recovery from database connection loss. Broadly speaking, there are three levels of response to this event:

1.   Let the user work it out.
This is a minimal approach and ok for basic applications and when connection loss is rare. All you need to do is to report the exception and exit the program. The user is then responsible for restarting the program, working out what data was lost, if any, and restoring any important changes. The users task is simplified if updates are committed soon after they are made, even auto-committed as then they only have to look at the last thing they did. Caching too many updates before applying/committing them just makes the problem worse.

2.   Assisted Restart
On connection loss, your program closes down the connection (usually a forced disconnect) in order to restore itself to a known state. It then re-opens the connection, reopens the datasets  that were open when the connection was lost and tells the user that some data may have been lost. Again, it is the user’s responsibility to work out what was lost, etc.

3.   Journaling
This is where you pre-empt the problem by recording, in some journal file, every SQL Update, Insert and Delete statement, the transaction they were executed under and when each transaction starts and is either committed or rolled back. The journal file can be no more than a text file with one statement per line,

When a connection is lost, your program does a forced disconnect and re-opens the connection, It then looks in the journal for any uncommitted transactions and, for each one, starts a new transaction with the same parameters, and executes the recorded SQL statements in the same order as they were journaled and under that transaction. The datasets are then reopened  and the user should see exactly the same state as when the connection was lost. Note that the recovery transactions are not committed. That is because the view is restored to exactly the same state and it is up to the user to decide whether to commit or rollback.

The problem with the above comes if the connection is lost when committing a transaction, You then do not know if it was committed or is still in limbo. You need to save some information server side with each transactions that allows a simple way of determining whether the commit succeeded. This could be as simple as a last transaction identifier recorded by an update statement that is executed under the transaction.

IBX does not include an actual journaling capability – although this is an idea for a future release. However, it does include TISQLMonitor. This allows you to snoop on every transaction and sql statement and use this to record to a journal file.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 27, 2021, 12:38:28 pm
Please keep us updated.

MarkMLl

I will. Perhaps in the new post, caused this I think this is a new topic.

This thread does seem to be going off at a tangent to the original post.

What you need to decide is what are you trying to achieve in terms of recovery from database connection loss. Broadly speaking, there are three levels of response to this event:

1.   Let the user work it out.
This is a minimal approach and ok for basic applications and when connection loss is rare. All you need to do is to report the exception and exit the program. The user is then responsible for restarting the program, working out what data was lost, if any, and restoring any important changes. The users task is simplified if updates are committed soon after they are made, even auto-committed as then they only have to look at the last thing they did. Caching too many updates before applying/committing them just makes the problem worse.

2.   Assisted Restart
On connection loss, your program closes down the connection (usually a forced disconnect) in order to restore itself to a known state. It then re-opens the connection, reopens the datasets  that were open when the connection was lost and tells the user that some data may have been lost. Again, it is the user’s responsibility to work out what was lost, etc.

3.   Journaling
This is where you pre-empt the problem by recording, in some journal file, every SQL Update, Insert and Delete statement, the transaction they were executed under and when each transaction starts and is either committed or rolled back. The journal file can be no more than a text file with one statement per line,

When a connection is lost, your program does a forced disconnect and re-opens the connection, It then looks in the journal for any uncommitted transactions and, for each one, starts a new transaction with the same parameters, and executes the recorded SQL statements in the same order as they were journaled and under that transaction. The datasets are then reopened  and the user should see exactly the same state as when the connection was lost. Note that the recovery transactions are not committed. That is because the view is restored to exactly the same state and it is up to the user to decide whether to commit or rollback.

The problem with the above comes if the connection is lost when committing a transaction, You then do not know if it was committed or is still in limbo. You need to save some information server side with each transactions that allows a simple way of determining whether the commit succeeded. This could be as simple as a last transaction identifier recorded by an update statement that is executed under the transaction.

IBX does not include an actual journaling capability – although this is an idea for a future release. However, it does include TISQLMonitor. This allows you to snoop on every transaction and sql statement and use this to record to a journal file.


I think option no 3 is what I want.

To detect whether transaction committed OK , isn't it is enough to execute ApplyUpdate inside try and catch? If errors occurs than I will rollback transaction and try to ApplyUpdate again.

That is a good new if you plan to add journaling features to IBX, thanks.

Meanwhile, I will try to build my own journaling method, not using TSQLmonitor, caused I didn't know it has such capabilities.

So far, so good, I am able to pull sql statements from TIBdataset and build a simple journaling.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: tonyw on November 27, 2021, 05:29:38 pm
No promises, but I will look into a journaling component.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on November 28, 2021, 05:05:57 am
Guys,

I manage to build a simple solution to this matter.

This is the link :
https://forum.lazarus.freepascal.org/index.php/topic,57344.0.html
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: MarkMLl on November 28, 2021, 10:06:00 am
Well done, I'll try to take a look at some point but it won't be immediate.

MarkMLl
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: tonyw on December 07, 2021, 02:53:31 pm
I have today released an experimental version of IBX that supports Client side journaling. Client side journaling creates a per database attachment file recording each read/write transaction start and end, and each SQL query that modifies data in the database. It can optionally record each read only transaction and queries that do not modify data (e.g. select queries).

The purpose of client side journaling is:

    1. To create transaction and query log that can be used to recover from a lost database connection or server crash, and

    2. To provide a record of database activity for debugging purposes.

The experimental version currently supports the creation of the journal. However, applying the journal for recovery purposes is left to a later update - probably after Christmas.

The journal file is a text file and is intended to be human readable. Each record in the journal starts with an asterisk and is terminated by a line separator. Each record is identified by a single character immediately after the asterisk, followed by a timestamp, session id and transaction id. Each query is recorded as the original SQL with the parameters replaced by the parameter values as literals.

In order to support programmatic parsing of the journal file, the class TJournalProcessor may be found in the IBUtils unit.

For more information see ibx/fbintf/doc/README.ClientSideJournaling.pdf. Test22 in ibx/fbintf/testsuite also provides an example of direct use of the journaling capability, and ibx/testsuite Test29 provides an example of use of the new component IBJournal. Additional examples should follow in later releases.

This experimental version can be downloaded by svn using

svn co https://svn.mwasoftware.co.uk/viewvc/public/ibx/branches/journaling/ ibx-experimental

or point your browser at

https://svn.mwasoftware.co.uk/viewvc/public/ibx/branches/journaling/

and click on "Download Tarball" to get an archive of the experimental version.
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: incendio on December 08, 2021, 02:59:33 am
or point your browser at

https://svn.mwasoftware.co.uk/viewvc/public/ibx/branches/journaling/

and click on "Download Tarball" to get an archive of the experimental version.

Downloaded and tried to unpack, got an error, see attachment
Title: Re: IBX : Can it handle database operation when network connection is not stable?
Post by: tonyw on December 08, 2021, 10:46:45 am
or point your browser at

https://svn.mwasoftware.co.uk/viewvc/public/ibx/branches/journaling/

and click on "Download Tarball" to get an archive of the experimental version.

Downloaded and tried to unpack, got an error, see attachment

Looks like the software you are using to unpack the archive can't support symbolic links - or you are still using Windows XP! Although googling the subject, it looks like support in Windows for symbolic links is problematic (https://blogs.windows.com/windowsdeveloper/2016/12/02/symlinks-windows-10/).

svn (for windows) foes not have this problem.

The files affected are not critical and, apart from the first, are only used by the testsuite. You can always copy them from an older version - or from the archive as the links are to files in the archive. The first link is to a documentation file. fbintf is developed as a separate package with its own changelog and documentation - see fbintf/doc. A link to the user guide is placed in the top level doc folder for user convenience only.
TinyPortal © 2005-2018