PHP 数据库连接池实现
-
- 摘要
- xml
- 读取配置文件
- 简易方式
- 常规方式
- PHP解析XML
- 配置文件
- 解析
- 读取配置文件
- 数据库连接池
- 测试
- 申请过多时拒绝请求
- 已满后拒绝放入
- 总结
摘要
xml
可以容忍冗余,但是不能带来一点歧义,或者维护困难方面的问题。
读取配置文件
简易方式
$content = file_get_contents("filename.xml");
echo $content;- 1
- 2
- 1
- 2
常规方式
// 读取配置文件内容$handle = fopen("filepath", "r");$content = fread($handle, filesize("filepath"));- 1
- 2
- 3
- 1
- 2
- 3
PHP解析XML
配置文件
<mysql><host>localhosthost><user>rootuser><password>123456password><db>testdb><port>3306port>
mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
解析
/*** 作为解析XML配置文件必备工具*/
class XMLUtil {public static $dbconfigpath = "./db.config.xml";public static function getDBConfiguration() {$dbconfig = array ();try {// 读取配置文件内容$handle = fopen(self::$dbconfigpath, "r");$content = fread($handle, filesize(self::$dbconfigpath));// 获取xml文档根节点,进而获取相关的数据库信息$mysql = simplexml_load_string($content);// 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用$dbconfig['host'] = $mysql->host;$dbconfig['user'] = $mysql->user;$dbconfig['password'] = $mysql->password;$dbconfig['db'] = $mysql->db;$dbconfig['port'] = $mysql->port;// 将配置信息以关联数组的形式返回return $dbconfig;} catch ( Exception $e ) {throw new RuntimeException ( "读取数据库配置文件信息出错!
" );}return $dbconfig;}
}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
数据库连接池
从池子中取,用毕,归还给池子。
/**x* PHP中的数据库 工具类设计* 郭璞* 2016年12月23日* **/
class DbHelper {private $dbconfig;private $dbpool;public $poolsize;public function __construct($poolsize = 20) {if (! file_exists ( "./utils.php" )) {throw new RuntimeException ( "utils.php文件丢失,无法进行配置文件的初始化操作!
" );}else {require './utils.php';}// 初始化 配置文件信息$this->dbconfig = XMLUtil::getDBConfiguration ();// 准备好数据库连接池“伪队列”$this->poolsize = $poolsize;$this->dbpool = array ();for($index = 1; $index <= $this->poolsize; $index ++) {$conn = mysqli_connect ( $this->dbconfig ['host'], $this->dbconfig ['user'], $this->dbconfig ['password'], $this->dbconfig ['db'] ) or die ( "连接数据库失败!
" );array_push ( $this->dbpool, $conn );}}/*** 从数据库连接池中获取一个数据库链接资源** @throws ErrorException* @return mixed*/public function getConn() {if (count ( $this->dbpool ) <= 0) {throw new ErrorException ( "数据库连接池中已无链接资源,请稍后重试!" );} else {return array_pop ( $this->dbpool );}}/*** 将用完的数据库链接资源放回到数据库连接池** @param unknown $conn * @throws ErrorException*/public function release($conn) {if (count ( $this->dbpool ) >= $this->poolsize) {throw new ErrorException ( "数据库连接池已满
" );} else {array_push ( $this->dbpool, $conn );}}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
测试
申请过多时,拒绝请求
已满后拒绝放入
总结
来源:http://blog.csdn.net/Marksinoberg/article/details/53857511
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
