src/SMProxyServer.php
public function onClose(\swoole_server $server, int $fd)
    {
        if (isset($this->source[$fd])) {
            unset($this->source[$fd]);
        }
        if (isset($this->halfPack[$fd])) {
            unset($this->halfPack[$fd]);
        }
        $connectHasTransaction = false;
        $connectHasAutoCommit  = false;
        if (isset($this->connectHasTransaction[$fd]) && true === $this->connectHasTransaction[$fd]) {
            //回滚未关闭事务
            $connectHasTransaction = true;
            unset($this->connectHasTransaction[$fd]);
        }
        if (isset($this->connectHasAutoCommit[$fd]) && true === $this->connectHasAutoCommit[$fd]) {
            //开启autocommit=0未关闭
            $connectHasAutoCommit = true;
            unset($this->connectHasAutoCommit[$fd]);
        }
        if (isset($this->mysqlClient[$fd])) {
            foreach ($this->mysqlClient[$fd] as $key => $mysqlClient) {
                $model = explode(DB_DELIMITER, $key)[0];
                if ($model == 'write') {
                    if (isset($mysqlClient ->client) && $mysqlClient ->client) {
                        if ($connectHasTransaction) {
                            $mysqlClient->send(getString([9, 0, 0, 0, 3, 82, 79, 76, 76, 66, 65, 67, 75]));
                        }
                        if ($connectHasAutoCommit) {
                            $mysqlClient->send(getString([
                                17, 0, 0, 0, 3, 115, 101, 116, 32, 97, 117, 116, 111, 99, 111, 109, 109, 105, 116, 61, 49,
                            ]));
                        }
                    }
                }
                //处理预处理语句连接断开未关闭
                $clientId = spl_object_hash($mysqlClient);
                if (isset($this->stmtPrepare[$clientId])) {
                    $stmtIdes = $this->stmtPrepare[$clientId] ?? [];
                    if (!empty($stmtIdes)) {
                        foreach ($stmtIdes as $stmtId) {
                            $mysqlClient->send(getString(array_merge([5, 0, 0, 0, 25], getMysqlPackSize($stmtId, 4))));
                        }
                    }
                    unset($this->stmtPrepare[$clientId]);
                }
                unset($clientId);
                MySQLPool::recycle($mysqlClient);
            }
            unset($this->mysqlClient[$fd]);
        }
        if (isset($this->connectReadState[$fd])) {
            unset($this->connectReadState[$fd]);
        }
        parent::onClose($server, $fd);
    }
public function onClose(\swoole_server $server, int $fd)
    {
        if (isset($this->source[$fd])) {
            unset($this->source[$fd]);
        }
        if (isset($this->halfPack[$fd])) {
            unset($this->halfPack[$fd]);
        }
        $connectHasTransaction = false;
        $connectHasAutoCommit  = false;
        if (isset($this->connectHasTransaction[$fd]) && true === $this->connectHasTransaction[$fd]) {
            //回滚未关闭事务
            $connectHasTransaction = true;
            unset($this->connectHasTransaction[$fd]);
        }
        if (isset($this->connectHasAutoCommit[$fd]) && true === $this->connectHasAutoCommit[$fd]) {
            //开启autocommit=0未关闭
            $connectHasAutoCommit = true;
            unset($this->connectHasAutoCommit[$fd]);
        }
        if (isset($this->mysqlClient[$fd])) {
            foreach ($this->mysqlClient[$fd] as $key => $mysqlClient) {
                $model = explode(DB_DELIMITER, $key)[0];
                if ($model == 'write') {
                    if (isset($mysqlClient ->client) && $mysqlClient ->client) {
                        if ($connectHasTransaction) {
                            $mysqlClient->send(getString([9, 0, 0, 0, 3, 82, 79, 76, 76, 66, 65, 67, 75]));
                        }
                        if ($connectHasAutoCommit) {
                            $mysqlClient->send(getString([
                                17, 0, 0, 0, 3, 115, 101, 116, 32, 97, 117, 116, 111, 99, 111, 109, 109, 105, 116, 61, 49,
                            ]));
                        }
                    }
                }
                //处理预处理语句连接断开未关闭
                $clientId = spl_object_hash($mysqlClient);
                if (isset($this->stmtPrepare[$clientId])) {
                    $stmtIdes = $this->stmtPrepare[$clientId] ?? [];
                    if (!empty($stmtIdes)) {
                        foreach ($stmtIdes as $stmtId) {
                            $mysqlClient->send(getString(array_merge([5, 0, 0, 0, 25], getMysqlPackSize($stmtId, 4))));
                        }
                    }
                    unset($this->stmtPrepare[$clientId]);
                }
                unset($clientId);
                MySQLPool::recycle($mysqlClient);
            }
            unset($this->mysqlClient[$fd]);
        }
        if (isset($this->connectReadState[$fd])) {
            unset($this->connectReadState[$fd]);
        }
        parent::onClose($server, $fd);
    }
客户端断开连接.

@param \swoole_server $server
@param int            $fd