search
HomePHP FrameworkWorkermanWhat to do if workerman introduces db error

What to do if workerman introduces db error

Dec 23, 2019 am 10:47 AM
workerman

What to do if workerman introduces db error

In order to achieve more efficient server access speed, we will use long connections of mysql or other databases to improve access speed.

The following content uses mysql pdo as an example:

Problem:

When the cli is resident in memory, there will be many situations that cause the created connection to fail;

Solution:

When accessing the database When adding try catch capture

When the error captured is 2006 or 2013, it means that the connection has failed. Reconnect to the database at this time to ensure the normal operation of the program;

// 在和数据库交互的地方加上try catchpublic function Init($query)
    {
        try{
            // todo 这里是操作数据库的逻辑
        }catch (\Exception $e) {
            if ( $e->errorInfo[1] === 2006 || $e->errorInfo[1] === 2013 ) {
                // todo 下面填写 或者调用连接数据库的代码
                $dsn = 'mysql:dbname=' . $this->settings["dbname"] . ';host=' . $this->settings["host"] . '';
                $this->pdo = new \PDO($dsn, $this->settings["user"], $this->settings["password"], array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8;"));

                $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

                $this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
                return true;
            }
        }
    }

var_dump($e-> ;errorInfo); The value

array(3) {
  [0]=>
  string(5) "HY000"
  [1]=>
  int(2006)
  [2]=>
  string(26) "MySQL server has gone away"}

For more workerman knowledge, please pay attention to the workerman tutorial column on the PHP Chinese website.

The above is the detailed content of What to do if workerman introduces db error. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.