网站设计建设趋势,seo网站seo,个人网站网站建设,广东上海专业网站建设公司哪家好教程地址#xff1a;Hyperf
一、安装
1.1 hyperf框架
composer require hyperf/db-connection
1.2 其他框架
composer require hyperf/database
二、配置
配置项类型默认值备注driverstring无数据库引擎hoststring无数据库地址databasestring无数据库默认 DBusernamest…教程地址Hyperf
一、安装
1.1 hyperf框架
composer require hyperf/db-connection
1.2 其他框架
composer require hyperf/database
二、配置
配置项类型默认值备注driverstring无数据库引擎hoststring无数据库地址databasestring无数据库默认 DBusernamestring无数据库用户名passwordstringnull数据库密码charsetstringutf8数据库编码collationstringutf8_unicode_ci数据库编码prefixstring数据库模型前缀timezonestringnull数据库时区pool.min_connectionsint1连接池内最少连接数pool.max_connectionsint10连接池内最大连接数pool.connect_timeoutfloat10.0连接等待超时时间pool.wait_timeoutfloat3.0超时时间pool.heartbeatint-1心跳pool.max_idle_timefloat60.0最大闲置时间optionsarrayPDO 配置 options相关pdo文档PHP: PDO - Manual
三、多库配置、读写分离、原生查询、sql输出 由于hyperf/database 衍生于 illuminate/database 。
illuminate/database多库配置和读写分离参考illuminate/database 使用 四-CSDN博客 原生查询和sql输出参考illuminate/database 使用 五-CSDN博客 。
3.1 原生查询-事务
3.1.1 自动管理数据库事务 根据文档描述如果事务的闭包 Closure 中出现一个异常事务将会回滚。如果事务闭包 Closure 执行成功事务将自动提交。 确实比手动方便但是还得查下报错怎么处理。 根据之前文章应该明白Illuminate\Database中调用顺序即从Illuminate\Database\Capsule\Manager最后会调用到Illuminate\Database\Connectionl。Connectionl中调用Concerns\ManagesTransactions类。transaction()方法在ManagesTransactions类中被调用。 源码如下
trait ManagesTransactions
{/*** Execute a Closure within a transaction.** param \Closure $callback* param int $attempts* return mixed** throws \Throwable*/public function transaction(Closure $callback, $attempts 1){for ($currentAttempt 1; $currentAttempt $attempts; $currentAttempt) {$this-beginTransaction();// Well simply execute the given callback within a try / catch block and if we// catch any exception we can rollback this transaction so that none of this// gets actually persisted to a database or stored in a permanent fashion.try {$callbackResult $callback($this);}// If we catch an exception well rollback this transaction and try again if we// are not out of attempts. If we are out of attempts we will just throw the// exception back out and let the developer handle an uncaught exceptions.catch (Throwable $e) {$this-handleTransactionException($e, $currentAttempt, $attempts);continue;}try {if ($this-transactions 1) {$this-getPdo()-commit();}$this-transactions max(0, $this-transactions - 1);if ($this-transactions 0) {optional($this-transactionsManager)-commit($this-getName());}} catch (Throwable $e) {$this-handleCommitTransactionException($e, $currentAttempt, $attempts);continue;}$this-fireConnectionEvent(committed);return $callbackResult;}}/*** Handle an exception encountered when running a transacted statement.** param \Throwable $e* param int $currentAttempt* param int $maxAttempts* return void** throws \Throwable*/protected function handleTransactionException(Throwable $e, $currentAttempt, $maxAttempts){// On a deadlock, MySQL rolls back the entire transaction so we cant just// retry the query. We have to throw this exception all the way out and// let the developer handle it in another way. We will decrement too.if ($this-causedByConcurrencyError($e) $this-transactions 1) {$this-transactions--;optional($this-transactionsManager)-rollback($this-getName(), $this-transactions);throw $e;}// If there was an exception we will rollback this transaction and then we// can check if we have exceeded the maximum attempt count for this and// if we havent we will return and try this query again in our loop.$this-rollBack();if ($this-causedByConcurrencyError($e) $currentAttempt $maxAttempts) {return;}throw $e;}
/*** Handle an exception encountered when committing a transaction.** param \Throwable $e* param int $currentAttempt* param int $maxAttempts* return void** throws \Throwable*/protected function handleCommitTransactionException(Throwable $e, $currentAttempt, $maxAttempts){$this-transactions max(0, $this-transactions - 1);if ($this-causedByConcurrencyError($e) $currentAttempt $maxAttempts) {return;}if ($this-causedByLostConnection($e)) {$this-transactions 0;}throw $e;}……
}
如源码所示闭包执行错误执行handleTransactionException()方法处理回滚并抛出异常。
$this-transactions--大概是处理事务嵌套相关代码。
提交异常执行handleCommitTransactionException()方法嵌套事务大概会继续执行最后抛出异常。
测试代码
function test4()
{Capsule::transaction(function () {Capsule::table(userinfo)-where([id 1])-update([votes 1]);});
}
test4(); 运行结果
Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column votes in field list in D:\workspace\php\wj_test\illuminate_database\vendor\illuminate\database\Connection.php on line 712Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column votes in field list (SQL: update userinfo set votes 1 where (id 1)) in D:\workspace\php\wj_test\illuminate_database\vendor\illuminate\database\Connection.php on line 712Call Stack:0.0009 411368 1. {main}() D:\workspace\php\wj_test\illuminate_database\test.php:00.0208 1925160 2. test4() D:\workspace\php\wj_test\illuminate_database\test.php:1190.0208 1925480 3. Illuminate\Database\Capsule\Manager::transaction(class Closure) D:\workspace\php\wj_test\illuminate_database\test.php:1170.0208 1925856 4. Illuminate\Database\Capsule\Manager::__callStatic(string(11), array(1)) D:\workspace\php\wj_test\illuminate_database\test.php:1170.0269 2634512 5. Illuminate\Database\MySqlConnection-transaction(class Closure, ???) D:\workspace\php\wj_test\illuminate_database\vendor\illuminate\database\Capsule\Manager.php:2000.0705 3895296 6. Illuminate\Database\MySqlConnection-handleTransactionException(class Illuminate\Database\QueryException, long, long) D:\workspace\php\wj_test\illuminate_database\vendor\illuminate\database\Concerns\ManagesTransactions.php:37
此时update执行Illuminate\Database\Connection::update(),update调用Connection::run()执行,run()通过Connection::runQueryCallback()执行,产生PDO异常再将PDO异常传递给\Illuminate\Database\QueryException异常。
使用throw 会直接输出异常。
PDOExceptionPHP: PDOException - Manual
3.1.2 手动管理数据库事务
function test5()
{Capsule::beginTransaction();try {Capsule::table(userinfo)-where([id 1])-update([votes 1]);Capsule::commit();} catch (\Throwable $th) {var_dump($th-getMessage());Capsule::rollBack();}
}
test5(); 和之前是同样的道理代码最后执行都是Connectionl类Connectionl::beginTransaction()、Connectionl::commit()、Connectionl::rollback()都是Connectionl中调用的ManagesTransactions类中的方法。
例子中Capsule类相当于官网的DB类。
四、构造器
参考
illuminate/database 使用 一-CSDN博客
Hyperf
自己写的博客有涉及一些运行原理官网的例子比较多。