Home
Download
Document
Forum
Video
Donate
Source Code
AI 助理
Sponsors
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
Products
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
Login
Register
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
5.x版本一键协程方案,mysql数据操作错误
### 问题描述 错误信息: ``` PHP Fatal error: Uncaught think\db\exception\PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php:797 Stack trace: #0 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php(856): think\db\PDOConnection->getPDOStatement() #1 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php(819): think\db\PDOConnection->queryPDOStatement() #2 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php(1098): think\db\PDOConnection->pdoExecute() #3 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/BaseQuery.php(1098): think\db\PDOConnection->update() ``` ### Swoole版本,PHP版本,以及操作系统版本信息 swoole 5.0.3 php 8.1.0 ### 相关代码 ```php <?php declare (strict_types=1); namespace app\command; use Swoole\Runtime; use Swoole\WebSocket\Frame; use think\console\Command; use think\console\Input; use think\console\Output; use function Co\run; class Websocket extends Command { const WS_CONFIG_PARAMS = [ 'reactor_num' => 10, //设置启动的 Reactor 线程数,建议设置为 CPU 核数的 1-4 倍 'worker_num' => 10, //设置启动的 Worker 进程数 'max_request' => 10000, //设置 worker 进程的最大任务数 'max_connection' => 1000000, //服务器程序,最大允许的连接数。最大允许维持多少个 TCP 连接 'task_worker_num' => 5,//开启的task进程数 'log_file' => './runtime/socket/websocket.log', 'log_date_format' => '%Y-%m-%d %H:%M:%S', 'log_rotation' => 'SWOOLE_LOG_ROTATION_DAILY', ]; protected function configure() { // 给命令起一个名字 $this->setName('game') ->setDescription('Start game websocket server'); } protected function execute(Input $input, Output $output) { $ws = new \swoole\websocket\server("0.0.0.0", 8100); $ws->set(self::WS_CONFIG_PARAMS); echo date("Y-m-d H:i:s") . 'websocket start' . PHP_EOL; Runtime::enableCoroutine(); run(function () use($ws){ // 监听open连接打开事件 $ws->on('Open', function (\swoole\websocket\server $ws, \swoole\http\request $request) { echo "connection open: {$request->fd}" . PHP_EOL; $ws->push($request->fd, "连接成功"); }); // 监听message消息事件 $ws->on('Message', function (\swoole\websocket\server $ws, Frame $frame) { //same code app('app\controller\Index')->save($frame->data); }); //其他监听消息事件以及task }); $ws->start(); } } ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? `app\controller\Index 文件中DB::name('user')->where(['id'=>3])->select();`这种正常数据库CURD能够正常操作,根据报错提示信息,调整了thinkphp数据库连接配置 `\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true`,仍然无法解决
发布于1年前 · 52 次浏览 · 来自
提问
JKing
### 问题描述 错误信息: ``` PHP Fatal error: Uncaught think\db\exception\PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php:797 Stack trace: #0 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php(856): think\db\PDOConnection->getPDOStatement() #1 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php(819): think\db\PDOConnection->queryPDOStatement() #2 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/PDOConnection.php(1098): think\db\PDOConnection->pdoExecute() #3 /www/wwwroot/php-gapi/vendor/topthink/think-orm/src/db/BaseQuery.php(1098): think\db\PDOConnection->update() ``` ### Swoole版本,PHP版本,以及操作系统版本信息 swoole 5.0.3 php 8.1.0 ### 相关代码 ```php <?php declare (strict_types=1); namespace app\command; use Swoole\Runtime; use Swoole\WebSocket\Frame; use think\console\Command; use think\console\Input; use think\console\Output; use function Co\run; class Websocket extends Command { const WS_CONFIG_PARAMS = [ 'reactor_num' => 10, //设置启动的 Reactor 线程数,建议设置为 CPU 核数的 1-4 倍 'worker_num' => 10, //设置启动的 Worker 进程数 'max_request' => 10000, //设置 worker 进程的最大任务数 'max_connection' => 1000000, //服务器程序,最大允许的连接数。最大允许维持多少个 TCP 连接 'task_worker_num' => 5,//开启的task进程数 'log_file' => './runtime/socket/websocket.log', 'log_date_format' => '%Y-%m-%d %H:%M:%S', 'log_rotation' => 'SWOOLE_LOG_ROTATION_DAILY', ]; protected function configure() { // 给命令起一个名字 $this->setName('game') ->setDescription('Start game websocket server'); } protected function execute(Input $input, Output $output) { $ws = new \swoole\websocket\server("0.0.0.0", 8100); $ws->set(self::WS_CONFIG_PARAMS); echo date("Y-m-d H:i:s") . 'websocket start' . PHP_EOL; Runtime::enableCoroutine(); run(function () use($ws){ // 监听open连接打开事件 $ws->on('Open', function (\swoole\websocket\server $ws, \swoole\http\request $request) { echo "connection open: {$request->fd}" . PHP_EOL; $ws->push($request->fd, "连接成功"); }); // 监听message消息事件 $ws->on('Message', function (\swoole\websocket\server $ws, Frame $frame) { //same code app('app\controller\Index')->save($frame->data); }); //其他监听消息事件以及task }); $ws->start(); } } ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? `app\controller\Index 文件中DB::name('user')->where(['id'=>3])->select();`这种正常数据库CURD能够正常操作,根据报错提示信息,调整了thinkphp数据库连接配置 `\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true`,仍然无法解决
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2023-11-01
Rango
这个错误是多个协程公用一个连接导致的,你需要使用连接池。建议使用 `think-swoole`
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。