Calling DB Query for stored procedure fails with 'Commands out of sync' on SS4.0

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

Silverstripe Version:
SS4.0

Question:
I am writing a custom search using a stored procedure on my SS4.x site but any time I run a DB::query command if there are any commands calling the database after that I always get “Commands out of sync; you can’t run this command now”

Details of your query go here


$records =  DB::query("CALL ap_ReportByTask ('10',  '2020-01-01', '2020-12-31');");
foreach($records as $row){
$result[] = $row;
}

I found a solution

extend MySqlConnector

use SilverStripe\ORM\Connect\MySQLiConnector;

class SPMySQLiConnector extends MySQLiConnector
{
/**
 * Invoked before any query is executed
 *
 * @param string $sql
 */
protected function beforeQuery($sql)
{
    // Clear the last statement
    $this->dbConn->next_result();  //  this is the method doing the magic 
    $this->setLastStatement(null);
}
}

And add the following block to app.yml

SilverStripe\ORM\Connect\MySQLDatabase:
  dependencies:
    connector: '%$SPMySQLiConnector'      

SPMySQLiConnector is my new Class