Codeigniter 实例在线程闭包内失去与数据库的连接
Codeigniter instance losing connection to database inside of threaded closure
在使用 php index.php [controllername] [methodname]
.
从 php-cli 调用的控制器中调用以下代码
此代码还嵌套在 foreach
循环中。该代码最终调用一个模型,该模型使用内置 mysql 驱动程序查询数据库。这里的问题是第一次迭代,Codeigniter 能够从模型调用成功连接到数据库。然而...
在第二次迭代中加载模型并在驱动程序 returns 数据库查询的结果为空时调用该方法。 $ci_instance
变量是控制器上下文中 $this 的副本。
我在尝试在 Codeigniter PHP 单元测试中模拟模型时也遇到了类似的问题。
$thread = ThreadManager::async(function () use (
$ci_instance,
$model,
$function_name,
$vars,
$log_id,
$cron
) {
$exception = NULL;
try
{
$ci_instance->load->model($model);
$success = $ci_instance->$model->$function_name($vars);
}
catch(Exception $e)
{
$exception = $e->getMessage();
}
if($success)
{
$ci_instance->cron_queue_model->close_crontab($cron['id']);
$ci_instance->cron_log_model->update_note($log_id, 'info', 'Completed #'. $cron['id']. '-'. $model.'/'.$function_name . ' ' . $success);
}
else
{
$ci_instance->cron_log_model->insert_error($cron['id'], 'error', 'Failed -'. $model.'/'.$function_name . ' ' . $exception);
log_message('error', 'The model:' . $model . ' function:' . $function_name . ' with vars:' . $vars . ' did not run as expected ' . $exception);
}
});
这个问题我好像有点解决了。当我这样做时,我得到了以下回复:
mysql_stat($ci_instance->db->conn_id);
"MySQL server has gone away"
所以我在 try catch 块中添加了以下行并且它起作用了:
try
{
if(mysql_stat($ci_instance->db->conn_id) === "MySQL server has gone away")
{
$ci_instance->db->reconnect();
}
$ci_instance->load->model($model);
$success = $ci_instance->$model->$function_name($vars);
}
但是,我仍然无法在不等待进程控制分支的结果的情况下让它在自己的进程中工作。
我根本不知道 codeignigter API,但我 运行 在我自己的工作中遇到过类似的问题。我的具体问题是数据库连接在我的 class' __destruct 方法中被关闭。我怀疑正在发生的事情是当你的闭包 returns (我假设意味着线程死亡)PHP 正在调用你的数据库实例上的析构函数并可能关闭连接。由于数据库连接是共享的,因此这将关闭 class.
所有实例的连接
在使用 php index.php [controllername] [methodname]
.
此代码还嵌套在 foreach
循环中。该代码最终调用一个模型,该模型使用内置 mysql 驱动程序查询数据库。这里的问题是第一次迭代,Codeigniter 能够从模型调用成功连接到数据库。然而...
在第二次迭代中加载模型并在驱动程序 returns 数据库查询的结果为空时调用该方法。 $ci_instance
变量是控制器上下文中 $this 的副本。
我在尝试在 Codeigniter PHP 单元测试中模拟模型时也遇到了类似的问题。
$thread = ThreadManager::async(function () use (
$ci_instance,
$model,
$function_name,
$vars,
$log_id,
$cron
) {
$exception = NULL;
try
{
$ci_instance->load->model($model);
$success = $ci_instance->$model->$function_name($vars);
}
catch(Exception $e)
{
$exception = $e->getMessage();
}
if($success)
{
$ci_instance->cron_queue_model->close_crontab($cron['id']);
$ci_instance->cron_log_model->update_note($log_id, 'info', 'Completed #'. $cron['id']. '-'. $model.'/'.$function_name . ' ' . $success);
}
else
{
$ci_instance->cron_log_model->insert_error($cron['id'], 'error', 'Failed -'. $model.'/'.$function_name . ' ' . $exception);
log_message('error', 'The model:' . $model . ' function:' . $function_name . ' with vars:' . $vars . ' did not run as expected ' . $exception);
}
});
这个问题我好像有点解决了。当我这样做时,我得到了以下回复:
mysql_stat($ci_instance->db->conn_id);
"MySQL server has gone away"
所以我在 try catch 块中添加了以下行并且它起作用了:
try
{
if(mysql_stat($ci_instance->db->conn_id) === "MySQL server has gone away")
{
$ci_instance->db->reconnect();
}
$ci_instance->load->model($model);
$success = $ci_instance->$model->$function_name($vars);
}
但是,我仍然无法在不等待进程控制分支的结果的情况下让它在自己的进程中工作。
我根本不知道 codeignigter API,但我 运行 在我自己的工作中遇到过类似的问题。我的具体问题是数据库连接在我的 class' __destruct 方法中被关闭。我怀疑正在发生的事情是当你的闭包 returns (我假设意味着线程死亡)PHP 正在调用你的数据库实例上的析构函数并可能关闭连接。由于数据库连接是共享的,因此这将关闭 class.
所有实例的连接