Metrics
0
Watch 101 Star 351 Fork 88

swoole / frameworkPHPLGPL-3.0

Sign up for free
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
基于Swoole扩展开发的PHP web server&应用服务器, 内置框架。提供统一注册树,数据库操作,模板,Cache,日志,队列,上传管理,用户管理等丰富的功能特性。 spread retract

Clone or download
MySQL.php 1.88 KB
Copy Edit Web IDE Raw Blame History
韩天峰 authored 2016-09-07 10:14 . MySQL连接池增加3个方法
<?php
namespace Swoole\Async;
use Swoole;
class MySQL extends Pool
{
const DEFAULT_PORT = 3306;
function __construct($config, $maxConnection = 100)
{
if (empty($config['host']))
{
throw new Swoole\Exception\InvalidParam("require mysql host option.");
}
if (empty($config['port']))
{
$config['port'] = self::DEFAULT_PORT;
}
parent::__construct($config, $maxConnection);
$this->create(array($this, 'connect'));
}
protected function connect()
{
$db = new \swoole_mysql;
$db->on('close', function ($db)
{
$this->remove($db);
});
return $db->connect($this->config, function ($db, $result)
{
if ($result)
{
$this->join($db);
}
else
{
$this->failure();
trigger_error("connect to mysql server[{$this->config['host']}:{$this->config['port']}] failed. Error: {$db->connect_error}[{$db->connect_errno}].");
}
});
}
function query($sql, callable $callabck)
{
$this->request(function (\swoole_mysql $db) use ($callabck, $sql)
{
return $db->query($sql, function (\swoole_mysql $db, $result) use ($callabck)
{
call_user_func($callabck, $db, $result);
$this->release($db);
});
});
}
function isFree()
{
return $this->taskQueue->count() == 0 and $this->idlePool->count() == count($this->resourcePool);
}
/**
* 关闭连接池
*/
function close()
{
foreach ($this->resourcePool as $conn)
{
/**
* @var $conn \swoole_mysql
*/
$conn->close();
}
}
function __destruct()
{
$this->close();
}
}

Comment ( 5 )

You need to Sign in for post a comment

Help Search