title: C# MySql error: An established connection was aborted by the software in your host machine
body: <p>I have a strange problem when working with MySql in C#. When executing the command sometimes(not always) I get "An established connection was aborted by the software in your host machine" error. I cannot solve this problem and can't find any solution in web.</p>
  
  <p>As a workaround I developed simple ReConnection class. When this exception occurs I simply call ReOpen() method. It works perfectly with ADO.NET and I am using this trick more than 2 year.</p>
  
  <pre><code>public class ReConnection : DbConnection
  {
      private readonly int _reconnectAttempts;
      private readonly int _reconnectInterval;
  
      private bool _isOpened;
      private int _reConnectSessions;
      private readonly object _syncRoot = new object();
  
      private readonly DbCommand _pingCommand;
      private readonly DbConnection _innerConnection;
  
      public ReConnection(DbConnection innerConnection,
          int reconnectAttempts = 4, int reconnectInterval = 1000)
      {
          _innerConnection = innerConnection;
  
          _pingCommand = _innerConnection.CreateCommand();
          _pingCommand.CommandText = "SELECT 1 FROM DUAL";
  
          _reconnectAttempts = reconnectAttempts;
          _reconnectInterval = reconnectInterval;
      }
  
      public override void Open()
      {
          _innerConnection.Open();
          _isOpened = true;
      }
  
      public override void Close()
      {
          _isOpened = false;
          while (_reConnectSessions &gt; 0)
              Thread.Sleep(100);
          _innerConnection.Close();
      }
  
      internal bool ReOpen()
      {
          var restored = false;
          lock (_syncRoot)
          {
              _reConnectSessions++;
              var retries = _reconnectAttempts;
              try
              {
                  _pingCommand.ExecuteNonQuery();
                  restored = true;
              }
              catch (Exception)
              {
                  while (_isOpened &amp;&amp;
                         ((retries &gt; 0) || (_reconnectAttempts == -1)) &amp;&amp;
                         !restored)
                  {
                      if ((retries &lt; _reconnectAttempts) || (_reconnectAttempts == -1))
                          Thread.Sleep(_reconnectInterval);
                      try
                      {
                          _innerConnection.Close();
                          try
                          {
                              _innerConnection.Open();
                          }
                          catch{}
                          //here ecception occurs. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. 
                          _innerConnection.Close(); // closing the corrupted connection
                          _innerConnection.Open(); // opening the new connection
                              //tut vnezapno poyavlaetsya transport connection, kuda mojno pisat' dannie
                          _pingCommand.ExecuteNonQuery();
  
                          restored = true;
                      }
                      catch (Exception){}
                      retries--;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>But recently I have developed an app with Entity Framework and similar exception occurs here also. I can't develop previous workaround for Entity Framework.</p>
  
  <ol>
  <li>Does anyone encounter such problem with MySql ? </li>
  <li>What is the actual reason behind this exception?</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/2N5ZT.jpg" rel="nofollow noreferrer">Where the error occurs</a></p>
  
  <p><a href="https://i.stack.imgur.com/TRGxL.jpg" rel="nofollow noreferrer">Error Details</a></p>