AsyncMysqlConnection::releaseConnection

Releases the current connection and returns a synchronous MySQL connection

Description

public function releaseConnection(): mixed

This method will destroy the current AsyncMysqlConnection object and give you back a vanilla, synchronous MySQL resource.

Return Values

  • mixed - A resource representing a MySQL resource, or false on failure.

Examples

If you ever want to get a plain, vanilla synchronous MySQL connection from your async connection, you call AsyncMysqlConnection::releaseConnection. This examples show how to get such a connection, noting too that your async connection is destroyed.

<?hh

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

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

async function connect(\AsyncMysqlConnectionPool $pool):
  Awaitable<\AsyncMysqlConnection> {
  return await $pool->connect(
    CI::$host,
    CI::$port,
    CI::$db,
    CI::$user,
    CI::$passwd
  );
}
async function simple_query(): Awaitable<int> {
  $pool = new \AsyncMysqlConnectionPool(array());
  $conn = await connect($pool);
  $result = await $conn->query('SELECT name FROM test_table WHERE userID = 1');
  $sync_connection = $conn->releaseConnection();
  var_dump($sync_connection);
  try {
    $result2 = await $conn->query(
      'SELECT name FROM test_table WHERE userID = 1'
    );
  } catch (\Exception $ex) {  // probably InvalidArgumentException on query
    echo "Connection destroyed when released\n";
  }
  // This call will block since it is not async
  $sync_result = mysql_query(
    'SELECT name FROM test_table WHERE userID = 1',
    $sync_connection,
  );
  $sync_rows = mysql_num_rows($sync_result);
  mysql_close($sync_connection);
  return $result->numRows() + $sync_rows;
}

function run(): void {
  $r = \HH\Asio\join(simple_query());
  var_dump($r);
}

run();
Output
resource(4) of type (mysql link)
Connection destroyed when released
int(2)