Bug #61807 Fatal error encountered during data read
Submitted: 9 Jul 2011 22:16 Modified: 13 Jul 2011 14:14
Reporter: Adil Shoukat Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:5.0.51a OS:Microsoft Windows (7)
Assigned to: Fernando Gonzalez.Sanchez CPU Architecture:Any
Tags: Data Read, FATAL ERROR, Reader.Read()

[9 Jul 2011 22:16] Adil Shoukat
Description:
Hi,
I am working on an application in which I need to read all records from a mySql table and process them one-by-one. Unfortunately I have some 850 records and it takes almost 20 minutes to process and print all the records. But after reading almost a 100 records from DB and printing them I get an exception with Error Message: "Fatal error encountered during data read."
Here are the Exception Details:

MySql.Data.MySqlClient.MySqlException was unhandled
  Message="Fatal error encountered during data read."
  Source="MySql.Data"
  ErrorCode=-2147467259
  Number=0
  StackTrace:
       at MySql.Data.MySqlClient.MySqlDataReader.Read()
       at Askari9.mainForm.performBillPrinting() in C:\Users\adil\Documents\Visual Studio 2008\Projects\Askari9\Askari9\Form1.cs:line 645
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: MySql.Data.MySqlClient.MySqlException
       Message="Reading from the stream has failed."
       Source="MySql.Data"
       ErrorCode=-2147467259
       Number=0
       StackTrace:
            at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
            at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
            at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns)
            at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns)
            at MySql.Data.MySqlClient.ResultSet.GetNextRow()
            at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior)
            at MySql.Data.MySqlClient.MySqlDataReader.Read()
       InnerException: System.IO.IOException
            Message="Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
            Source="MySql.Data"
            StackTrace:
                 at MyNetworkStream.HandleOrRethrowException(Exception e)
                 at MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
                 at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
                 at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)
                 at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
                 at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
            InnerException: System.Net.Sockets.SocketException
                 Message="An existing connection was forcibly closed by the remote host"
                 Source="System"
                 ErrorCode=10054
                 NativeErrorCode=10054
                 StackTrace:
                      at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                 InnerException: 

How to repeat:
C# code:

string MyConString = "SERVER=localhost;" +
                "DATABASE=askari9;" +
                "UID=root;" +
                "PASSWORD=;" +
                "Connect Timeout=21600;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;

            String strCommandText = "SELECT *";
            strCommandText += " FROM homeowners";

            command.CommandText = strCommandText;
            command.CommandTimeout = 21600;
            connection.Open();    // Opening DB connection
            Reader = command.ExecuteReader();
while (Reader.Read()) // Getting Exception on Reader.Read() after a few hundreds iterations
            {
                (range.Cells[12, 3] as Excel.Range).Value2 = Reader.GetValue(ownerNameAndRankFieldIndexInDB).ToString().Trim(); // Owner Name
                (range.Cells[13, 3] as Excel.Range).Value2 = Reader.GetValue(houseNoFieldIndexInDB).ToString().Trim(); // House Number
                (range.Cells[14, 3] as Excel.Range).Value2 = textBoxBillingMonth.Text; // Billing Month
                (range.Cells[15, 3] as Excel.Range).Value2 = textBoxDueDate.Text; // Due Date
                (range.Cells[18, 6] as Excel.Range).Value2 = textBoxServiceCharges.Text; // Service Charges

                ((Excel._Workbook)xlWorkBook).PrintOut(missing, missing,
                    missing, missing, missing,
                    missing, missing, missing);

                // Time taken by printer to process one document
                Thread.Sleep(2000);
            }
            connection.Close(); // closing DB connection

Suggested fix:
Could not find any ...
[11 Jul 2011 20:18] Fernando Gonzalez.Sanchez
Hi Adil,

This seems similar to issue: http://bugs.mysql.com/bug.php?id=57365

The workaround then was to customize the net_xxx_timeout settings.
For example, issue the following after openning your connection and
before executing the datareader:

MySqlCommand cmd = new MySqlCommand(
  "set net_write_timeout=99999; set net_read_timeout=99999", con );
cmd.ExecuteNonQuery();

Let us know if that solves the prb.
[12 Jul 2011 16:59] Adil Shoukat
Hi Fernando Gonzalez,
Yeas you are right
It's working like a charm now :)
Thanks a lot
[13 Jul 2011 14:14] Fernando Gonzalez.Sanchez
a workaround was provided.
[24 Sep 2014 17:38] cesar saenz zurita
I have the same error, I do what is recommended, but it marking me error, that I can do?
[10 May 2016 11:42] Chiranjeevi Battula
http://bugs.mysql.com/bug.php?id=68983 marked as duplicate of this one.