源码剖析Yii错误 Invalid parameter number: no parameters were bound
ActiveRecord使用的一个陷阱导致 Invalid parameter number: no parameters were bound
请看下面的例子
$criteria = new CDbCriteria();
$criteria->select = "*";
$model = Biubiu::model();
$ids = range(160,163);
$criteria->addInCondition("id", $ids);
$model->findByPk(160);//某次操作
sleep(32);//处理其他事情花费了较长时间。
$result = $model->findAll($criteria);<1>
//$result = $model->getCommandBuilder()->createFindCommand($model->getTableSchema(),$criteria)->queryAll();<2>
if($result){
echo count($result);
}
为了体现这个问题,我的本地数据库wait_timeout = 30
那么会出现下面的问题:
exception 'Exception' with message 'exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. The SQL statement executed was: SELECT * FROM `t_biubiu` `t` WHERE id IN (:ycp0, :ycp1, :ycp2, :ycp3). Bound with :ycp0=160, :ycp1=161, :ycp2=162, :ycp3=163' in E:\xxxx\framework\db\CDbCommand.php:569
Stack trace:
#0 E:\xxxx\framework\db\CDbCommand.php(578): CDbCommand->queryInternalAll('fetchAll', Array, Array)
#1 E:\xxxx\framework\db\CDbCommand.php(418): CDbCommand->queryInternal('fetchAll', Array, Array)
#2 E:\xxxx\framework\db\ar\CActiveRecord.php(1356): CDbCommand->queryAll()
#3 E:\xxxx\framework\db\ar\CActiveRecord.php(1475): CActiveRecord->query(Object(CDbCriteria), true)
#4 E:\xxxx\experiment\protected\commands\YiiCommand.php(18): CActiveRecord->findAll(Object(CDbCriteria))
我的另一片文章和这个问题的原因都是一样的:Yii 数据库重连告别General error: 2006 MySQL server has gone away
以我所见的解决方法就是 :
1—— 尽量避免上面的这种写法,直接addInCondition,把id填入SQL
$criteria->addCondition("id in (". join(",",$ids) . ")");
2——就是重连。
分析:
并不是参数没有绑定,查看findAll()的源码
public function findAll($condition='',$params=array())
{
Yii::trace(get_class($this).'.findAll()','system.db.ar.CActiveRecord');
$criteria=$this->getCommandBuilder()->createCriteria($condition,$params);
return $this->query($criteria,true);
}
query实际上执行的是:
$command=$this->getCommandBuilder()->createFindCommand($this->getTableSchema(),$criteria);
$command->queryAll();
而createFindCommand()
调用bindValue(),里面的代码如下:
$this->_statement->bindValue($name,$value,$this->_connection->getPdoType(gettype($value)));
连接已经timeout,失效了。bindValue无用。
queryAll() --> queryInternal('fetchAll',PDO::FETCH_ASSOC,[]);
-->三次调用 queryInternalAll('fetchAll',PDO::FETCH_ASSOC,[])
简化的queryInternalAll如下:
private function queryInternalAll($method,$mode,$params=array())
{
$params=array_merge($this->params,$params);
try
{
$this->prepare();
@$this->_statement->execute();
{
$mode=(array)$mode;
call_user_func_array(array($this->_statement, 'setFetchMode'), $mode);
$result=$this->_statement->$method();
$this->_statement->closeCursor();
}
return $result;
}
catch(Exception $e)
{
$errorInfo=$e instanceof PDOException ? $e->errorInfo : null;
$message=$e->getMessage();
if(YII_DEBUG)
$message.='. The SQL statement executed was: '.$this->getText().$par;
if(!empty($errorInfo) && (2006 == $errorInfo[1] || 2013 == $errorInfo[1])) {
$this->_connection->setActive(false);
$this->cancel();
}
throw new CDbException(Yii::t('yii','CDbCommand failed to execute the SQL statement: {error}',
array('{error}'=>$message)),(int)$e->getCode(),$errorInfo);
}
}
这样就看到了
exception ‘Exception’ with message ‘exception ‘CDbException’ with message ‘CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. The SQL statement executed was:
另外:设置db属性:
'attributes' => [
PDO::ATTR_TIMEOUT => 600,
],
是没有用处的。
但可以考虑使用:
Yii::$app->db->createCommand('SET SESSION wait_timeout = 28800;')->execute();
重试机制:
//Biubiu::getDb()->close(); 这么搞也行但是每次都会关闭。
do{
try{
$result = Biubiu::find()->select("id,value")->where(['id'=>$ids])->all();
foreach ($result as $one){
echo $one->id . ">" .$one->value . PHP_EOL;
}
}catch (\yii\db\Exception $e){
Biubiu::getDb()->close();
Biubiu::getDb()->open();
if(strpos($e->getMessage(), 'MySQL server has gone away')===false){
throw new Exception($e);
}
}
}while(--$retry);
执行时间较长的脚本,并且一段时间就会结束的,不能用持久化连接PDO::ATTR_PERSISTENT => true,适合用这种做法进行重连,能有效防止闪断等超时错误。
前面提到的文章给出的解决办法并不适合大量反复的数据库访问,会多很多不必要的ping操作。然而就几千条的数据,就不必在乎其带来的性能影响。
源码剖析Yii错误 Invalid parameter number: no parameters were bound的更多相关文章
- Django----djagorest-framwork源码剖析
restful(表者征状态转移,面向资源编程)------------------------------------------->约定 从资源的角度审视整个网络,将分布在网络中某个节点的资源 ...
- Django Rest Framework源码剖析(七)-----分页
一.简介 分页对于大多数网站来说是必不可少的,那你使用restful架构时候,你可以从后台获取数据,在前端利用利用框架或自定义分页,这是一种解决方案.当然django rest framework提供 ...
- Django Rest Framework源码剖析(四)-----API版本
一.简介 在我们给外部提供的API中,可会存在多个版本,不同的版本可能对应的功能不同,所以这时候版本使用就显得尤为重要,django rest framework也为我们提供了多种版本使用方法. 二. ...
- 玩转Android之Picasso使用详详详详详详解,从入门到源码剖析!!!!
Picasso是Squareup公司出的一款图片加载框架,能够解决我们在Android开发中加载图片时遇到的诸多问题,比如OOM,图片错位等,问题主要集中在加载图片列表时,因为单张图片加载谁都会写.如 ...
- 豌豆夹Redis解决方案Codis源码剖析:Dashboard
豌豆夹Redis解决方案Codis源码剖析:Dashboard 1.不只是Dashboard 虽然名字叫Dashboard,但它在Codis中的作用却不可小觑.它不仅仅是Dashboard管理页面,更 ...
- RestFramework——API基本实现及dispatch基本源码剖析
基于Django实现 在使用RestFramework之前我们先用Django自己实现以下API. API完全可以有我们基于Django自己开发,原理是给出一个接口(URL),前端向URL发送请求以获 ...
- 侯捷STL课程及源码剖析学习1
1.C++标准库和STL C++标准库以header files形式呈现: C++标准库的header files不带后缀名(.h),例如#include <vector> 新式C hea ...
- WorldWind源码剖析系列:星球球体的加载与渲染
WorldWind源码剖析系列:星球球体的加载与渲染 WorldWind中主函数Main()的分析 在文件WorldWind.cs中主函数Main()阐明了WorldWind的初始化运行机制(如图1所 ...
- PCL源码剖析之MarchingCubes算法
原文:http://blog.csdn.net/lming_08/article/details/19432877 MarchingCubes算法简介 MarchingCubes(移动立方体)算法是目 ...
随机推荐
- Python学习-列表深浅拷贝
一.先看一个简单的赋值语句 lst1 = ['France', 'Belgium', 'England'] lst2 = lst1 # lst1.append('Uruguay') lst2.appe ...
- mongodb 获取自增数
mongodb db.getCollection('user').findAndModify({update:{$inc:{'level':1}},query:{"name":&q ...
- flask+layui+echarts实现前端动态图展示数据
效果图: 该效果主要实现一个table展示数据,并在下方生成一个折线图. 实现方式: 1.首先需要对表格进行一个数据加载,这里用到了layui的table.render,具体用法可以参考 https: ...
- centos 升级
yum -y update升级所有包同时也升级软件和系统内核 yum -y upgrade只升级所有包,不升级软件和系统内核
- Hadoop点滴-Hadoop分布式文件系统
Hadoop自带HDFS,即 Hadoop Distributed FileSystem(不是HaDoop FileSystem 的简称) 适用范围 超大文件:最新的容量达到PB级 流式数据访问:H ...
- Flask基础(17)-->防止 CSRF 攻击
CSRF CSRF全拼为Cross Site Request Forgery,译为跨站请求伪造. CSRF指攻击者盗用了你的身份,以你的名义发送恶意请求. 包括:以你名义发送邮件,发消息,盗取你的账号 ...
- Locomotion和Navigation的区别
Locomotion和navigation两者都是移动.漫游的意思.但是locomotion是一个比navigation更大的概念,它指的是所有的第一人称视角的变换(first-person moti ...
- TreeMap剖析
TreeMap实现有序要么就是外界传递进来Comparator对象,要么就使用默认key的Comparable接口(实现自然排序) 最后我就来总结一下TreeMap要点吧: 由于底层是红黑树,那么时间 ...
- 03-Django基础概念和MVT架构
一.Django基础 掌握Django的 MVT 架构的使用 掌握Git管理源代码 主要内容 了解Django的 MVT 架构的使用流程 使用Django完成案例 : 书籍信息管理 MVC介绍 MVC ...
- Nullable Reference Types 可空引用类型
在写C#代码的时候,你可能经常会遇到这个错误: 但如果想避免NullReferenceException的发生,确实需要做很多麻烦的工作. 可空引用类型 Null Reference Type 所以, ...