• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

mysql-timeout: 自定义每一条sql语句的查询超时时间。再也不怕连接增多,cli模式卡死 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

mysql-timeout

开源软件地址:

https://gitee.com/jaredwang/mysql-timeout

开源软件介绍:

mysql-timeout

Customizing the timeout for a database query

Install

composer.json

{"require": {    "wwaayyaa/mysql-timeout":  "dev-master"  }}

$ composer install

Example

php code

    $conf = Conf::$db1;    print_r($conf);    $d = new MysqlTimeout($conf);    echo "now:".time()."\n";    $r = $d->query('select sleep(2) as `sleep`,2 as `sec`;');    print_r($r);    echo "sleep 2s,now:".time()."\n";    try{    $r = $d->query('select sleep(5) as `sleep`,5 as `sec`;',3);    }catch(Exception $e){        echo sprintf("error message:%s ,error code : %d \n",$e->getMessage(),$e->getCode());    }    echo "sleep 5s but timeout is 3s,so throw a error.now:".time()."\n";    $d = new MysqlTimeout($conf);//must new a MysqlTimeout. because     $r = $d->query('select sleep(5) as `sleep`,5 as `sec`;',6);    print_r($r);    echo "sleep 5s success,because timeout is 6s. now:".time()."\n";        //Other methods    $r = $d->update("update dtk_zhibo_chat_log set addtime=now() where id = 6 or id = 17;");    echo sprintf("success rows:%d \n",$r);    $r = $d->insert("insert into dtk_zhibo_chat_log (zhiboId,content) values (1292,'test');");    echo sprintf("primary id:%d \n",$r);

result

Array(    [host] => 127.0.0.1    [port] => 3306    [user] => root    [password] => root    [dbname] => test    [charset] => utf8)now:1475056910Array(    [0] => Array        (            [sleep] => 0            [sec] => 2        ))sleep 2s,now:1475056912error message:timeout ,error code : 922922sleep 5s but timeout is 3s,so throw a error.now:1475056915Array(    [0] => Array        (            [sleep] => 0            [sec] => 5        ))sleep 5s success,because timeout is 6s. now:1475056920success rows:2primary id:1036

Method

	/**     * @return mixed  query data     */	public function query($sql, $timeout = 3);	/**     * @return int   rows count     */	public function update($sql, $timeout = 3);	/**     * @return int   data primary id     */	public function insert($sql, $timeout = 3);	/**     * @return int   rows count     */	public function delete($sql, $timeout = 3);

Args

config

  • host | must '127.0.0.1'
  • port | non-must '3306'
  • user | must 'root'
  • password | must 'root'
  • dbname | must 'text'
  • charset | must 'utf8'
  • timeout | non-must 3

timeout (defalut : 3)

  • The MsyqlTimeout inspection cycle is 0.05 seconds,
  • so the minimum value of the $timeout parameter is 0.05,
  • the maximum value depends on the PHP.ini max_execution_time and MySQL timeout settings.

Exception

errorcode

If the query timeout, MysqlTimeout will throw an exception, the exception number 922922


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
soybean: 一个轻量级的java orm库发布时间:2022-03-24
下一篇:
pysequoiadb: SequoiaDB 巨衫数据库的 Python 开发包发布时间:2022-03-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap