5 Star 7 Fork 1

王杨 / mysql-timeout

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

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:1475056910
Array
(
    [0] => Array
        (
            [sleep] => 0
            [sec] => 2
        )

)
sleep 2s,now:1475056912
error message:timeout ,error code : 922922
sleep 5s but timeout is 3s,so throw a error.now:1475056915
Array
(
    [0] => Array
        (
            [sleep] => 0
            [sec] => 5
        )

)
sleep 5s success,because timeout is 6s. now:1475056920
success rows:2
primary 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

MIT License Copyright (c) 2016 王杨 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

自定义每一条sql语句的查询超时时间。再也不怕连接增多,cli模式卡死,修改配置文件啦。 expand collapse
PHP
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
PHP
1
https://gitee.com/jaredwang/mysql-timeout.git
git@gitee.com:jaredwang/mysql-timeout.git
jaredwang
mysql-timeout
mysql-timeout
master

Search