AsyncMysqlConnection::isReusable

Returns whether or not the current connection is reusable

public function isReusable(): bool;

By default, the current connection is reusable by the pool. But if you call setResuable(false), then the current connection will not be reusable by the connection pool.

Returns

  • bool - true if the connection is reusable; false otherwise.

Examples

The following example shows how to make a connection not reusable in a connection pool with AsyncMysqlConnection::setReusable and test it with AsyncMysqlConnection::isReusable. By default, connections in pools are reusable. So, here we create a pool connection that is assigned to $conn. When we close $conn, that destroys that connection permanently. So when we get $conn2, a whole new connection will need to be created since we can't use the connection that was associated to $conn.

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

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

async function connect(
  \AsyncMysqlConnectionPool $pool,
): Awaitable<\AsyncMysqlConnection> {
  $conn = await $pool->connect(
    CI::$host,
    CI::$port,
    CI::$db,
    CI::$user,
    CI::$passwd,
  );
  // By default pool connections are automatically set to be reusable
  $conn->setReusable(false);
  return $conn;
}

async function simple_query(\AsyncMysqlConnection $conn): Awaitable<int> {
  $result = await $conn->query('SELECT name FROM test_table WHERE userID = 1');
  return $result->numRows();
}

async function simple_query_2(\AsyncMysqlConnection $conn): Awaitable<int> {
  $result = await $conn->query('SELECT name FROM test_table WHERE userID = 2');
  return $result->numRows();
}

async function get_connection(
  \AsyncMysqlConnectionPool $pool,
): Awaitable<\AsyncMysqlConnection> {
  return await connect($pool);
}

function get_pool(): \AsyncMysqlConnectionPool {
  $options = array(
    'pool_connection_limit' => 1,
  );
  return new \AsyncMysqlConnectionPool($options);
}

function run(): void {
  $pool = get_pool();

  $conn = \HH\Asio\join(get_connection($pool));
  // This will be false now
  \var_dump($conn->isReusable());
  $r2 = \HH\Asio\join(simple_query($conn));
  $conn->close();

  $conn2 = \HH\Asio\join(get_connection($pool));
  $r2 = \HH\Asio\join(simple_query_2($conn2));
  // You will see one destroyed pool connection since we close $conn above
  // and we didn't set it to be reusable
  \var_dump($pool->getPoolStats());
  $conn2->close();
}

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