AmfPHP 2.0 running on PHP 7 & PDO

549 views Asked by At

AmfPHP 2.0 running on Nginx and PHP 7.0 (FPM), PDO is used as database.

As many users log in to the game, the CPU increases immediately to 100% on all 4 cores due to MySQL. If there are fewer players, the process calms down again and the CPU drops.

Code snippet of PDO connection:

class Database extends PDO {

    public static function getConnection() {
        $connectionString = sprintf("mysql:dbname=%s;host=%s;charset=utf8", MYSQL_DB, MYSQL_HOST);

        try {
            $pdo = new PDO($connectionString, MYSQL_USER, MYSQL_PASS);
            $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            return $pdo;
        } catch(PDOException $e) {
            $error = date("Y-m-d g:i:s a T") . "\Database::getConnection\tError: (" . $e->getCode . ") " . $e->getMessage;
            throw new Exception($error);
        }
    }

    // Example
    public static function Test($arg) {
        try {
            $pdo = Database::getConnection();

            $stmt = $pdo->prepare("select * from xx where xx = :xx");
            $stmt->bindParam(":xx", $arg, PDO::PARAM_STR);
            $stmt->execute();
            $result = $stmt->fetch(PDO::FETCH_ASSOC);

            if ($stmt->rowCount() > 0) {
                // execute my code
            }
        } catch(PDOException $e) {
            $error = date("Y-m-d g:i:s a T") . "\Database::Test\tError: (" . $e->getCode . ") " . $e->getMessage;
            throw new Exception($error);
        }
    }

}

For further questions I am at any time available.

0

There are 0 answers