You are here

function apdqc_mysqli_new_connection in Asynchronous Prefetch Database Query Cache 7

Create a new MySQLi connection.

Parameters

array $db_info: Static var from 'apdqc_get_db_object'.

Return value

mysqli Returns a mysqli object on success or FALSE on failure.

2 calls to apdqc_mysqli_new_connection()
apdqc_get_db_object in ./apdqc.mysql.inc
Return a mysqli object that is ready to be used.
apdqc_mysqli_ping in ./apdqc.mysql.inc
Reconnect to the MySQL database if the connection has been lost.

File

./apdqc.mysql.inc, line 549
APDQC Database interface code for MySQL database servers.

Code

function apdqc_mysqli_new_connection(array $db_info, $check_max = FALSE) {

  // Create new MySQL connection.
  $mysqli = new mysqli();
  $mysqli
    ->options(MYSQLI_OPT_CONNECT_TIMEOUT, $db_info['var']['connect_timeout']);
  if (isset($db_info['connection']['pdo'])) {
    $pdo = $db_info['connection']['pdo'];
    $key = isset($pdo[PDO::MYSQL_ATTR_SSL_KEY]) ? $pdo[PDO::MYSQL_ATTR_SSL_KEY] : NULL;
    $cert = isset($pdo[PDO::MYSQL_ATTR_SSL_CERT]) ? $pdo[PDO::MYSQL_ATTR_SSL_CERT] : NULL;
    $ca = isset($pdo[PDO::MYSQL_ATTR_SSL_CA]) ? $pdo[PDO::MYSQL_ATTR_SSL_CA] : NULL;
    $capath = isset($pdo[PDO::MYSQL_ATTR_SSL_CAPATH]) ? $pdo[PDO::MYSQL_ATTR_SSL_CAPATH] : NULL;
    $cipher = isset($pdo[PDO::MYSQL_ATTR_SSL_CIPHER]) ? $pdo[PDO::MYSQL_ATTR_SSL_CIPHER] : NULL;
  }
  if (!empty($key) && !empty($cert) && !empty($ca)) {
    $mysqli
      ->ssl_set($key, $cert, $ca, $capath, $cipher);
    @$mysqli
      ->real_connect($db_info['connection']['host'], $db_info['connection']['username'], $db_info['connection']['password'], $db_info['connection']['database'], $db_info['connection']['port'], $db_info['connection']['unix_socket'], MYSQLI_CLIENT_SSL);
  }
  else {
    @$mysqli
      ->real_connect($db_info['connection']['host'], $db_info['connection']['username'], $db_info['connection']['password'], $db_info['connection']['database'], $db_info['connection']['port'], $db_info['connection']['unix_socket']);
  }
  if (empty($mysqli) || !empty($mysqli->connect_errno) || empty($mysqli->host_info)) {

    // Bail out if the DB didn't connect.
    return FALSE;
  }
  if (!empty($mysqli)) {

    // Break connection if we are within 80% of the max connections for MySQL.
    if ($check_max) {

      // @ignore sql_curly
      $max_connections_query = "SELECT IF (FLOOR(LEAST(" . $db_info['var']['max_connections'] . ", @@global.max_connections * 0.8)) > COUNT(ID), 1, 0) AS Keep_Connection FROM information_schema.processlist";
      $result = $mysqli
        ->query($max_connections_query)
        ->fetch_row();
      if ($result[0] == 0) {

        // Close the connection and return FALSE.
        $mysqli
          ->close();
        unset($mysqli);
        $mysqli = FALSE;
        return $mysqli;
      }
    }

    // Get connection ready for usage.
    $mysqli
      ->set_charset('utf8');

    // Setup MySQL.
    $init_query = "\n      SET sql_mode = 'ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER',\n      SESSION group_concat_max_len = 65535,\n      SESSION tx_isolation='READ-COMMITTED',\n      SESSION wait_timeout = LEAST(@@global.wait_timeout, {$db_info['var']['wait_timeout']}),\n      SESSION join_buffer_size = 131072\n    ";

    // Set innodb_lock_wait_timeout if MySQL 5.5 or higher.
    if ($mysqli->server_version >= 50500) {
      $init_query .= ", SESSION innodb_lock_wait_timeout = " . $db_info['var']['innodb_lock_wait_timeout'];
    }
    if (!empty($db_info['connection']['mysql_db_type']) && stripos($db_info['connection']['mysql_db_type'], 'mariadb') !== FALSE) {
      $init_query .= ", SESSION mrr_buffer_size = 262144";
    }
    else {
      $init_query .= ", SESSION read_rnd_buffer_size = 262144";
    }
    $mysqli
      ->query($init_query);
  }
  return $mysqli;
}