laravel 一般错误:1615 Prepared statement 需要重新准备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25873883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:07:01  来源:igfitidea点击:

General error: 1615 Prepared statement needs to be re-prepared

mysqllaravelmariadb

提问by mschuett

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.

每次尝试将中等大小的 JSON 对象同步到我的数据库时,我都会遇到这个问题,以便我们可以对其进行一些报告。通过调查可能导致它的原因,我遇到了有关此事的这些链接。

http://blog.corrlabs.com/2013/04/mysql-prepared-statement-needs-to-be-re.htmlhttp://bugs.mysql.com/bug.php?id=42041

http://blog.corrlabs.com/2013/04/mysql-prepared-statement-needs-to-be-re.html http://bugs.mysql.com/bug.php?id=42041

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.

两者似乎都指向了 table_definition_cache 的方向。然而,这是说问题是由于同时在服务器上发生的 mysqldump。我可以向你保证,情况并非如此。此外,我已将查询精简为一次仅插入一个对象。

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->id != 33421) {
            $organization = new Organization();
            $organization->orgsync_id = $org->id;
            $organization->short_name = $org->short_name;
            $organization->long_name = $org->long_name;
            $organization->category = $org->category->name;
            $organization->save();

            $org_groups = $this->getGroupsInOrganization($org->id);
            if (!is_int($org_groups))
            {
                foreach ($org_groups as $group)
                {
                    foreach($group->account_ids as $account_id)
                    {
                        $student = Student::where('orgsync_id', '=', $account_id)->first();
                        if (is_object($student))
                        {
                            $student->organizations()->attach($organization->id, array('is_officer' => ($group->name == 'Officers')));
                        }
                    }
                }
            }
        }
    }

    $job->delete();
}

This is the code that is running when the error is thrown. Which normally comes in the form of.

这是抛出错误时正在运行的代码。这通常以 的形式出现。

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))

Which is then followed by this error repeated 3 times.

然后这个错误重复了 3 次。

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))

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.

如果有人能指出我正确的方向,我将不胜感激。我更好奇究竟是什么触发了错误,然后找到了这个特定问题的原因。使用 ORM 时,它在 laravel 应用程序中似乎也有些常见。

回答by Mark Walker

While mysqldump is the commonly reported cause for this it is not the only one.

虽然 mysqldump 是常见的原因,但它并不是唯一的原因。

In my case running artisan:migrate on any database will also trigger this error for different databases on the same server.

在我的情况下,在任何数据库上运行 artisan:migrate 也会为同一服务器上的不同数据库触发此错误。

http://bugs.mysql.com/bug.php?id=42041Mentions table locks/flush which would be called in a mysqldump so worth checking if you have any migrations, locks or flushes happening simultaneously.

http://bugs.mysql.com/bug.php?id=42041提到将在 mysqldump 中调用的表锁/刷新,因此值得检查是否有任何迁移、锁或刷新同时发生。

Failing that try switching the prepares to emulated.

如果失败,请尝试将准备切换为模拟。

'options'   => [
            \PDO::ATTR_EMULATE_PREPARES => true
        ]

回答by pencilvania

This error occurs when mysqldump is in progress. It doesn't matter which DB dump is in progress. Wait for the dump to finish and this error will vanish.

当 mysqldump 正在进行时会发生此错误。正在进行哪个数据库转储并不重要。等待转储完成,此错误将消失。

The issue is with the table definition being dumped which cause this error.

问题在于正在转储的表定义导致此错误。

Yeah I tried changing these mysql settings, but it still occurs sometime (mostly when running heavy mysql backups/dumps at night)..

是的,我尝试更改这些 mysql 设置,但有时仍然会发生(主要是在夜间运行大量 mysql 备份/转储时)。

table_open_cache 128=>16384

table_open_cache 128=>16384

table_definition_cache 1024=>16384

table_definition_cache 1024=>16384