title: Delphi - DataSet creating too many connections in MySQL
body: <p>I am having a problem using the TFDDataSet component in my application.<br><br>
  I have a function that fetch many times if a customer has new orders. If it returns empty the function ends.</p>
  
  <pre><code>...
   fdm_XMLREsumo.Close;
         fdm_XMLREsumo.Active := false;
         _DataSetJSON := SM.GetXMLResumoChave( pEnt_ID, pChave); //See Edit 1
         _DataSet.Close;
         _DataSet := TFDJSONDataSetsReader.GetListValueByName( _DataSetJSON, sXMLResumo );
         _DataSet.Open; &lt;-- here's the problem 
         if not _DataSet.IsEmpty then begin
          exit;
         end;
         fdm_XMLREsumo.AppendData( _DataSet );
        ...
  </code></pre>
  
  <p>The problem is every time it executes <code>_DataSet.Open;</code> it creates a new connection in my DB.<br>Because of that I'm having a <code>too many connections</code> exception.<br><br>I've checked in my <strong>Server Properties</strong> and it is like this:</p>
  
  <p><a href="https://i.stack.imgur.com/DmgWs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DmgWs.png" alt="Many threads in Sleep state"></a></p>
  
  <p>I have tried <code>Connection.Close</code>,<code>_DataSet.Close</code>, <code>_DataSet.Free</code> and <code>_DataSet.Destroy</code> but nothing worked.<br>I read <a href="https://stackoverflow.com/questions/25194398/what-is-the-exact-effect-of-tdataset-close-in-delphi">this</a>, and it explains that even if you do <code>_DataSet.Close</code> the connection still exists, because <code>DataSets</code> work in memory.<br>There is also <a href="https://stackoverflow.com/questions/840353/delphi-tsqlquery-leaving-a-proccess-on-mysql-even-after-been-freed">this</a> guy having a similar issue, but using <code>Query</code>.<br> Does anyone know how can I manage to solve this?<br>I am using <strong>MySQL</strong></p>
  
  <p><strong>EDIT 1</strong><br>
  As @CraigYoung helped me saying my example needs MCVE<br><br>
  <code>SM.GetXMLResumoChave</code> method:</p>
  
  <p><br>Here it uses a connection to the database that is closed at the end of the function. Already debugged, and here it does not leave an open connection in MySQL Proccess List<br></p>
  
  <pre><code>function TDAOXMLResumo.GetXMLResumoChave(xEnt_id: Integer; xChave: String): TFDJSONDataSets;
  begin
    if not oSM.FDConn.Connected then
      oSM.FDConn.Connected := true;
    QueryPesquisa.SQL.Clear;
    QueryPesquisa.SQL.Text :=
       ' select * from table' +
       ' where ent_id = :ent_id ' +
       '       and xre_chNFe = :xre_chNFe ';
    QueryPesquisa.ParamByName('ent_id').Asinteger   := xEnt_id;
    QueryPesquisa.ParamByName('xre_chNFe').Asstring := xChave;
    Result := TFDJSONDataSets.Create;
    //TFDJSONDataSetsWriter.ListAdd Opens the Query that is passed as parameter and store the data as JSON in the TFDJSONDataSets (Result) object
    TFDJSONDataSetsWriter.ListAdd(Result, sXMLResumo, QueryPesquisa);  
    //Closing the Query
    QueryPesquisa.Close;
    //Closing the Connection
    oSM.FDConn.Close;
  end;`
  </code></pre>
  
  <p>Basically, the <code>_DataSet</code> is only receiving a JSON List here: <code>_DataSet := TFDJSONDataSetsReader.GetListValueByName( _DataSetJSON, sXMLResumo );</code>, and then open it to access the data in it.</p>