Page 1 of 1

Avoiding Connection Lost (during query) exceptions

Posted: Mon 01 Dec 2014 10:58
by John Smith22

We have a scientific program that calculates data in a very large matrix. 50 instances of the program run on 50 processors on 10 PCs, so 50 matrices are in use at any moment by all program instances. The average size of a matrix is about 500 MB but can go up to 2 GB. The server runs on Win 7 64. The client PCs run on Win XP 32.

The calculation of a single matrix by a program instance can take days or even weeks, then when it’s finished, the program starts to write all matrix data (about 1 GB) to the database.

Then I get this error:
"Lost connection to MySQL server during queryError on data writing to the connection:An established connection was aborted by the software in your host machine.Socket Error Code: 10053($2745)"

This happens on some of the write operations. Other do not have problems. I presume the largest matrices show this problems.

There seem to be two issues:

(1). The connection will be aborted by the server after a certain timeout, certainly the time to calculate a matrix is much larger than the server timeout;
(2). When writing tens of millions of records to a database, the connection might be reset or lost during query to store the data;

What’s the correct procedure to write very large amounts after a long time of inactivity to a server? In other words: How do I avoid any errors?

The structure (in pseudocode) I use now is:

(… do some very time consuming data)
MyConnection.connect ;
MyQuery.Connection := MyConnection ;
for i := 1 to 10000 do
Query.SQL.Text := ALongSQLInsertStatement ;
Query.Execute ;
end ;

I tried the START TRANSACTION / COMMIT method before, but that caused even more connection lost errors as it probably tries to write the whole set of data to the database in one chunk. In the above example the data is split in 10,000 pieces which solved many errors, but not all. I still get the above error in about 10% of the (largest?) matrix write to database operations.

Re: Avoiding Connection Lost (during query) exceptions

Posted: Mon 01 Dec 2014 12:27
by ViktorV
The MySQL server connection loss may be due to a little value of the wait_timeout MySQL system variable. Try to set the value of the wait_timeout MySQL system variable to 31536000, and check if the problem still persists.
Also, MySQL server connection loss when inserting large data to MySQL server can be due to the server settings. To load big data, you should increase the value of the max_allowed_packet server variable in the my.ini file and restart your server.
For example:
max_allowed_packet = 16M
Try to increase the value of the max_allowed_packet MySQL system variable, and check if the problem still persists.
You can find more information here: ... ables.html

Re: Avoiding Connection Lost (during query) exceptions

Posted: Mon 01 Dec 2014 15:33
by John Smith22
OK. I will try this solution.
By the way, for a windows environment, the wait_timeout value is 2147483, not 31536000.

Since I might have to wait for a week to see whether it solves the problem, I will cannot give feedback on short term.

I'll also try whether I can mimic (part of this) this error by manually killing the connection from inside MySQL workbench before the client starts writing. This will save time.

Re: Avoiding Connection Lost (during query) exceptions

Posted: Tue 02 Dec 2014 15:57
by John Smith22
By the way, I found an interesting article about this problem. See link below: ... d-harmful/

This matches other solutions I've seen on this forum that reconnect on an 'not connected' exception.

I temporary solved the problem in a quick and dirty way by disconnecting and reconnect to the server before writing the matrix data. Since I'm the only user of the server and hours lie between the write sessions, this does no harm.

MyConnection.Close ;
MyConnection.Open ;
//Start writing data

I will implement a proper solution later based on the 'not connected' exception.

Re: Avoiding Connection Lost (during query) exceptions

Posted: Wed 03 Dec 2014 11:26
by ViktorV
To resume lost connection, you should use the TMyConnection.OnConnectionLost event handler. The OnConnectionLost event handler is used to process fatal errors and perform failover. To make the OnConnectionLost event handler work, you should set the TMyConnection.Options.LocalFailover property to True. Note that to use the OnConnectionLost event handler, you should add the MemData unit to the USES clause of your unit. Here is an example of using the OnConnectionLost event handler:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
  MyConnection1.Options.LocalFailover := True;

procedure TForm1.MyConnection1ConnectionLost(Sender: TObject;
  Component: TComponent; ConnLostCause: TConnLostCause;
  var RetryMode: TRetryMode);
  RetryMode := rmReconnectExecute;
In this case, if connection was lost, MyDAC will try to reconnect and reexecute the abortive operation. For more information, please read the MyDAC documentation.

Re: Avoiding Connection Lost (during query) exceptions

Posted: Fri 02 Feb 2018 04:02
by dataflow
Hi, i have version 8.7.27 and I keep getting the timeout even after trying all this suggestions, can someone post a different solution?.


Re: Avoiding Connection Lost (during query) exceptions

Posted: Fri 02 Feb 2018 11:55
by ViktorV
The OnConnectionLost event occurs only when the following conditions are fulfilled:
- a fatal error occurs;
- there are no opened transactions in a connection that are not ReadOnlyReadCommitted (if connection has at least one opened transaction, which is not ReadCommitedReadOnly, FailOver does not execute. All ReadCommitedReadOnly transaction are restored with FailOver operation);
- there are no opened and non-fetched datasets;
- there are no explicitly prepared datasets or SQLs.
Please make sure that none of the conditions above is violated.

Note that MyDAC does not automatically initiate checking connection to the server. Therefore, after successfully connecting to the server if the connection to the server disconnects the TUniConnection.Connected property will be set to True and the OnConnectionLost event will not be initiated until there is an attempt to connect to the server.
Also to solve your problem you can use the TMyConnection.Ping method. This method is used to test the connection to MySQL server if the connection was disconnected, there will be an error and an attempt to reconnect to the server.
As well this method can be used in order for the server not to close a connection due to timeout connection activity. In this case, you can use TMyConnection.Ping as follows - call it from the timer and set the interval timer to a value less than wait_timeout of the server (so that the intervals between commands receiving by the server do not exceed wait_timeout).