AsyncMysqlConnection::setReusable

Sets if the current connection can be recycled without any clean up.

Description

public function setReusable(
  bool $reusable,
): void

Sets if the current connection can be recycled without any clean up.

By default, the current connection is reusable.

If a connection in a AsyncMysqlConnectionPool is used, but you call setReusable(false), then you will have to create a whole new connection instead of reusing this particular connection.

Parameters

  • bool $reusable - Pass true to make the connection reusable; false otherwise.

Examples

The following example shows how to make a connection not reusable in a connection pool with AsyncMysqlConnection::setReusable. 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.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\Conn\SetReusable;

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));
  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)
}