## 連接池
只需在協程中使用連接池組件獲取連接對象即可。
```
/** @var ConnectionPool $dbPool */
$db = app()->dbPool->getConnection();
$result = $db->createCommand('select sleep(5)')->queryAll();
$db->release(); // 不手動釋放的連接不會歸還連接池,會在析構時丟棄
```
[>> 到 GitHub 查看 DEMO <<](https://github.com/mix-php/mix-skeleton/tree/v2.1/app/Console/Commands/CoroutineCommand.php#L40)
## `Mix\Pool\AbstractConnectionPool`
所有的連接池對象都繼承該抽象類,框架提供了 db, redis 兩個封裝好的連接池類:
- [DB](https://github.com/mix-php/mix-database/tree/master/src/Pool):`Mix\Database\Pool\ConnectionPool::class`
- [Redis](https://github.com/mix-php/mix-redis/tree/master/src/Pool):`Mix\Redis\Pool\ConnectionPool::class`
## 依賴注入配置
- [beans/database](https://github.com/mix-php/mix-skeleton/tree/v2.1/manifest/beans/database.php)
- [beans/redis](https://github.com/mix-php/mix-skeleton/tree/v2.1/manifest/beans/redis.php)
## `Dialer` 撥號器
當連接池內的連接為空或者連接不足時,連接池組件會調用 `dialer` 對象獲取新的連接。
>[info] 撥號對象都必須實現 Mix\Pool\DialerInterface 接口。
框架默認提供了兩個撥號類,如果需要擴展,用戶自行實現 `Mix\Pool\DialerInterface `接口即可,也可繼承重寫。
- [Database Dialer](https://github.com/mix-php/database/blob/master/src/Pool/Dialer.php)
- [Redis Dialer](https://github.com/mix-php/redis/blob/master/src/Pool/Dialer.php)
- 歡迎使用 MixPHP
- 安裝說明
- 全棧安裝
- Phar 開發安裝
- 新手教程
- 命令行常識
- 進程管理
- 熱更新
- 全局變量
- 入門須知
- 命名空間
- 自動加載
- 入口文件
- 增改應用
- 核心功能
- 配置 (manifest.php)
- 協程
- 什么是協程
- 開啟協程
- PHP Stream Hook
- xgo + Channel
- WaitGroup + xdefer
- 連接池
- 協程池
- 定時器
- 依賴注入
- 事件調度
- 命令行
- 簡介
- Application
- 創建命令
- 命令參數
- 打印與顏色
- 守護進程
- 后臺運行
- Web/API 應用
- 簡介
- 服務器
- 路由
- 中間件
- 請求
- 文件上傳
- 響應
- 控制器
- 視圖
- Auth
- Session
- 客戶端
- GuzzleHttp
- 調試與錯誤
- 安全建議
- WebSocket 應用
- 簡介
- 服務器
- 客戶端
- JavaScript
- Swoole
- nginx代理
- 60s無消息斷線
- TCP 應用
- 簡介
- 服務器
- 客戶端
- Telnet
- PHP
- Swoole
- UDP 應用
- 簡介
- 服務器
- 客戶端
- NC
- Swoole
- Sync Invoke 同步調用
- 簡介
- 服務器
- 客戶端
- 公共組件
- 驗證器
- 驗證器定義
- 驗證規則
- 靜態調用
- 日志
- 緩存
- 數據庫
- Database
- ConnectionPool
- Connection
- QueryBuilder
- ExecutedEvent
- Redis
- ConnectionPool
- Connection
- CalledEvent
- 常見問題
- 如何利用CPU多核
- 連接多個數據庫
- 使用主從數據庫
- 如何設置跨域
- form-data 上傳文件失敗
- 輸出大于2M的文件失敗 (xlsx)
- 如何接入EasyWeChat
- 升級指導
- 不兼容修改-001
- 不兼容修改-002
- 不兼容修改-003