一 .什么是mysql连接池
场景:每秒同时有1000个并发,但是这个mysql同时只能处理400个连接,mysql会宕机。
 
解决方案:连接池,这个连接池建立了200个和mysql的连接,这1000个并发就有顺序的共享这连接池中的200个连接。
这个连接池能够带来额外的性能提升,因为这个和mysql建立连接的这个过程消耗较大,使用连接池只需连接一次mysql。
 
连接池定义:永不断开,要求我们的这个程序是一个常驻内存的程序。数据库连接池(Connection pooling)是程序启
动时建立足够的数据库连接,并将这些连接组成一个连接池,由程序动态地对池中的连接进行申请,使用,释放。
 
二.小案例
查找用户表数据库最新注册的3个会员?
(1)小提示
show processlist #mysql查看连接数
(2)创建10个mysql连接示例代码
<?php
/**
 * Created by PhpStorm.
 * User: Luke
 * Date: 2019/10/30
 * Time: 14:12
 */
//编写mysql连接池,这个类只能被实例化一次(单例)
class MysqlConnectionPool
{
    private static $instance;//单例对象
    private $connection_num = 10;//连接数量
    private $connection_obj = [];
 
    //构造方法连接mysql,创建20mysql连接
    private function __construct()
    {
        for($i=0;$i<$this->connection_num;$i++){
            $dsn = "mysql:host=127.0.0.1;dbnane=swoole";
            $this->connection_obj[] =  new Pdo($dsn,'root','rootmysql123');
        }
    }
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
    public static function getInstance()
    {
        if(is_null(self::$instance)){
            self::$instance = new self();
        }
    }
}
MysqlConnectionPool::getInstance();
//创建swool的http服务器对象
$serv = new swoole_http_server('0.0.0.0',8000);
//当浏览器链接点这个http服务器的时候,向浏览器发送helloworld
$serv->on('request', function($request,$response){
    //$request包含这个请求的所有信息,比如参数
    //$response包含返回给浏览器的所有信息,比如helloworld
 
    //(2.3)向浏览器发送helloworld
    $response->end("hello world");
});
//启动http服务器
$serv->start();
(3)效果
(4)完善mysql连接池
<?php
/**
 * Created by PhpStorm.
 * User: Luke
 * Date: 2019/10/30
 * Time: 14:12
 */
//编写mysql连接池,这个类只能被实例化一次(单例)
class MysqlConnectionPool
{
    private static $instance;//单例对象
    private $connection_num = 20;//连接数量
    private $connection_obj = [];
    private $avil_connection_num = 20;//可用连接
 
    //构造方法连接mysql,创建20mysql连接
    private function __construct()
    {
        for($i=0;$i<$this->connection_num;$i++){
            $dsn = "mysql:host=127.0.0.1;dbname=swoole";
            $this->connection_obj[] =  new Pdo($dsn,'root','rootmysql123');
        }
    }
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
    public static function getInstance()
    {
        if(is_null(self::$instance)){
            self::$instance = new self();
        }
        return self::$instance;
    }
 
    //执行sql操作
    public function query($sql)
    {
        if($this->avil_connection_num==0){
            throw new Exception("暂时没有可用的连接诶,请稍后");
        }
        //执行sql语句
        $pdo = array_pop($this->connection_obj);
        //可用连接数减1
        $this->avil_connection_num --;
        //使用从连接池中取出的mysql连接执行查询,并且把数据取成关联数组
        $rows = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        //把mysql连接放回连接池,可用连接数+1
        array_push($this->connection_obj,$pdo);
        $this->avil_connection_num ++;
        return $rows;
    }
}
//创建swool的http服务器对象
$serv = new swoole_http_server('0.0.0.0',8000);
//当浏览器链接点这个http服务器的时候,向浏览器发送helloworld
$serv->on('request', function($request,$response){
    //$request包含这个请求的所有信息,比如参数
    //$response包含返回给浏览器的所有信息,比如helloworld
    //向浏览器发送helloworld
    $stop = false;
    while (!$stop){
        try{
            $sql = "SELECT * FROM user ORDER BY id  DESC LIMIT 5";
            $rows = MysqlConnectionPool::getInstance()->query($sql);
            $response->end(json_encode($rows));
            $stop = true;
        }catch (Exception $e){
            usleep(100000);
        }
    }
 
});
//启动http服务器
$serv->start();

最新文章

  1. Android 利用RecyclerView.Adapter刷新列表中的单个view问题
  2. ListView中动态显示和隐藏Header&amp;Footer
  3. mybatis公用代码抽取到单独的mapper.xml文件
  4. 系统广播 android.intent.action.KILL_BACKGROUND_SERVICE
  5. php常用正则
  6. Hibernate输出SQL语句以便调试
  7. zuul入门(2)zuul的过滤器分类和加载
  8. Activity之间传递大数据问题
  9. 【Unity游戏开发】UGUI不规则区域点击的实现
  10. [Swift]LeetCode731. 我的日程安排表 II | My Calendar II
  11. springboot之mybatis注解形式
  12. python字典类型
  13. 关于java做题时需要注意的事项
  14. [daily][mariadb][mysql] mariadb快速设置
  15. go test命令參数问题
  16. 如何将int整型转换成String字符串类型
  17. STM32 usb_mem.c和usb_sil.c文件的分析
  18. NASA: Seeing Jupiter(注视木星)
  19. 多个Mapper和Reducer的Job
  20. cocos2dx对所有子节点设置透明度

热门文章

  1. iPhone 启动页尺寸
  2. 【Android】Android Studio NDK 开发
  3. MySQL Error Log 文件丢失导致The server quit without updating PID file启动失败的场景
  4. 算法之冒泡排序手写之js写法andjava写法
  5. enable user-defined extended attributes for ext3 file systems; 增加ext3 文件系统的扩展属性;
  6. 运行java可执行jar包
  7. 【oracle】ORA-00947: 没有足够的值
  8. SpringBoot系列之日志框架使用教程
  9. Springboot对SpringMVC如何扩展配置
  10. 多进程操作-进程锁multiprocess.Lock的使用