AsyncMysqlConnectionPool::connect

Begin an async connection to a MySQL instance

public function connect(
  string $host,
  int $port,
  string $dbname,
  string $user,
  string $password,
  int $timeout_micros = -1,
  string $extra_key = "",
): Awaitable<AsyncMysqlConnection>;

Once you have your pool constructed, you use this method to connect to the MySQL client. The connection pool will either create a new connection or use one of the recently available connections from the pool itself.

Parameters

  • string $host - The hostname to connect to.
  • int $port - The port to connect to.
  • string $dbname - The initial database to use when connecting.
  • string $user - The user to connect as.
  • string $password - The password to connect with.
  • int $timeout_micros = -1 - Timeout, in microseconds, for the connect; -1 for default, 0 for no timeout.
  • string $extra_key = "" - An extra parameter to help the internal connection pool infrastructure separate connections even better. Usually, the default "" is fine.

Examples

It is highly recommended that you use connection pools when using async MySQL. That way you don't have to create a new connection every time you want to make a query to the database. The following example shows you how to connect to a MySQL database using an AsyncMySqlConnectionPool.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\PoolConn\Connect;

require __DIR__ .'/../../__includes/async_mysql_connect.inc.php';

use \Hack\UserDocumentation\API\Examples\AsyncMysql\ConnectionInfo as CI;

class MyPool {
  private \AsyncMysqlConnectionPool $pool;

  public function __construct() {
    $this->pool = new \AsyncMysqlConnectionPool(array());
  }

  public function getPool(): \AsyncMysqlConnectionPool {
    return $this->pool;
  }

  public async function connect(): Awaitable<\AsyncMysqlConnection> {
    return await $this->pool->connect(
      CI::$host,
      CI::$port,
      CI::$db,
      CI::$user,
      CI::$passwd
    );
  }
}


async function get_num_rows(\AsyncMysqlConnection $conn): Awaitable<int> {
  $result = await $conn->query('SELECT * FROM test_table');
  return $result->numRows();
}

async function get_row_data(\AsyncMysqlConnection $conn):
  Awaitable<Vector<Vector>> {
  $result = await $conn->query('SELECT * FROM test_table');
  return $result->vectorRows();
}

async function run_it_1(MyPool $pool): Awaitable<void> {
  $conn = await $pool->connect();
  $rows = await get_num_rows($conn);
  \var_dump($rows);
}

async function run_it_2(MyPool $pool): Awaitable<void> {
  $conn = await $pool->connect();
  $data = await get_row_data($conn);
  \var_dump($data->count());
  // Should show only one created pool connection since we are pooling it
  \var_dump($pool->getPool()->getPoolStats());
}

function run(): void {
  $pool = new MyPool();
  \HH\Asio\join(run_it_1($pool));
  \HH\Asio\join(run_it_2($pool));
}

run();
Output
int(31)
int(31)
array(5) {
  ["created_pool_connections"]=>
  int(1)
  ["destroyed_pool_connections"]=>
  int(0)
  ["connections_requested"]=>
  int(2)
  ["pool_hits"]=>
  int(1)
  ["pool_misses"]=>
  int(1)
}