title: General error: 1615 Prepared statement needs to be re-prepared
body: <p>I have been running into this issue every time I try and sync a medium size JSON object to my database so we can perform some reporting on it. From looking into what can cause it I have come across these links on the matter.</p>
  
  <p><a href="http://blog.corrlabs.com/2013/04/mysql-prepared-statement-needs-to-be-re.html">http://blog.corrlabs.com/2013/04/mysql-prepared-statement-needs-to-be-re.html</a>
  <a href="http://bugs.mysql.com/bug.php?id=42041">http://bugs.mysql.com/bug.php?id=42041</a></p>
  
  <p>Both seem to point me in the direction of table_definition_cache. However this is saying the issue is due to a mysqldump happening on the server at the same time. I can assure you that this is not the case. Further I have slimmed down the query to only insert one object at a time.</p>
  
  <pre><code>public function fire($job, $data) 
  {
      foreach (unserialize($data['message']) as $org) 
      {
          // Ignore ID 33421 this will time out.
          // It contains all users in the system.
          if($org-&gt;id != 33421) {
              $organization = new Organization();
              $organization-&gt;orgsync_id = $org-&gt;id;
              $organization-&gt;short_name = $org-&gt;short_name;
              $organization-&gt;long_name = $org-&gt;long_name;
              $organization-&gt;category = $org-&gt;category-&gt;name;
              $organization-&gt;save();
  
              $org_groups = $this-&gt;getGroupsInOrganization($org-&gt;id);
              if (!is_int($org_groups))
              {
                  foreach ($org_groups as $group)
                  {
                      foreach($group-&gt;account_ids as $account_id)
                      {
                          $student = Student::where('orgsync_id', '=', $account_id)-&gt;first();
                          if (is_object($student))
                          {
                              $student-&gt;organizations()-&gt;attach($organization-&gt;id, array('is_officer' =&gt; ($group-&gt;name == 'Officers')));
                          }
                      }
                  }
              }
          }
      }
  
      $job-&gt;delete();
  }
  </code></pre>
  
  <p>This is the code that is running when the error is thrown. Which normally comes in the form of.</p>
  
  <pre><code>SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: insert into `organization_student` (`is_officer`, `organization_id`, `student_id`) values (0, 284, 26))
  </code></pre>
  
  <p>Which is then followed by this error repeated 3 times.</p>
  
  <pre><code>SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: insert into `organizations` (`orgsync_id`, `short_name`, `long_name`, `category`, `updated_at`, `created_at`) values (24291, SA, Society of American, Professional, 2014-09-15 16:26:01, 2014-09-15 16:26:01))
  </code></pre>
  
  <p>If anyone can point me in the right direction I would be very grateful. I am more curious about what is actually triggering the error then finding the cause of this specific issue. It also seems to be somewhat common in laravel application when using the ORM.</p>