PHP进阶教程-实现一个简单的MySQL连接池( 二 )

server.phpinclude './MysqlPool.php';//创建http server$http = new SwooleHttpServer("0.0.0.0", 9501);$http->set(["worker_num" => 2]);$http->on('WorkerStart', function ($serv, $worker_id) {$config = ['min' => 3,'max' => 5,'time_out' => 1,'db_host' => '127.0.0.1','db_user' => 'root','db_passwd' => 'sunny123','database' => 'lv'];MysqlPool::getInstance($config)->init();});$http->on('request', function ($request, $response) {try {MysqlPool::getInstance()->printLenth(SwooleCoroutine::getCid() . '获取前:');$mysql = MysqlPool::getInstance()->getConnection();MysqlPool::getInstance()->printLenth(SwooleCoroutine::getCid() . '归还前:');$result = $mysql->query("select * from sunny_member");$row = $result->fetch(MYSQLI_ASSOC);MysqlPool::getInstance()->printLenth(SwooleCoroutine::getCid() . '归还后:');$response->end($row['content']);} catch (Exception $e) {$response->end($e->getMessage());}});$http->start();本案例实现:

  • 最小连接数
  • 最大连接数
  • 当前连接数
  • 连接池对象
  • 获取连接池超时时间
思考:怎么实现健康度检查?

【PHP进阶教程-实现一个简单的MySQL连接池】


推荐阅读