From aeffcb73e36c8a9eb2daf1536b6d6f9d520317e9 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Sat, 27 Sep 2014 18:13:53 -0500
Subject: [PATCH] orm: Fix MySQL occasional "Commands OOS" error
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Under certain intermittent circumstances (usually a significant number of
ORM queries), the ORM will trigger a MySQL error:

Commands out of sync; you can't run this command now

Usually this MySQL error is related to buffered versus unbuffered queries.
However, the ORM already uses buffered queries (MySQL calls it
"store_result"). In this case, it appears there is some sort of race between
fetching the result metadata before configuring the statement for buffering.
(By "race" I mean that the error is not reliably triggered).

This patch seems to fix the issue by configuring buffering before fetching
result metadata — necessary to configure the fetching (output) phase of the
statement.
---
 include/class.orm.php | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index 1d8810919..29d474c4c 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -986,8 +986,6 @@ class MysqlExecutor {
     function _prepare() {
         $this->execute();
         $this->_setup_output();
-        if (!$this->stmt->store_result())
-            throw new OrmException('Unable to process query: '.$this->stmt->error);
     }
 
     function execute() {
@@ -996,7 +994,7 @@ class MysqlExecutor {
                 .' '.$this->sql);
         if (count($this->params))
             $this->_bind($this->params);
-        if (!$this->stmt->execute()) {
+        if (!$this->stmt->execute() || ! $this->stmt->store_result()) {
             throw new OrmException('Unable to execute query: ' . $this->stmt->error);
         }
         return true;
-- 
GitLab