正在显示
1 个修改的文件
包含
32 行增加
和
33 行删除
@@ -37,6 +37,11 @@ trait DbQuery { | @@ -37,6 +37,11 @@ trait DbQuery { | ||
37 | 37 | ||
38 | public function getClient() | 38 | public function getClient() |
39 | { | 39 | { |
40 | + // 读取连接齐前 检查 | ||
41 | +// if(isset(static::$pool) && !$this->ping()){ | ||
42 | +// $this->close(); | ||
43 | +// $this->client = static::$pool->get(); | ||
44 | +// } | ||
40 | return $this->client; | 45 | return $this->client; |
41 | } | 46 | } |
42 | 47 | ||
@@ -387,51 +392,45 @@ trait DbQuery { | @@ -387,51 +392,45 @@ trait DbQuery { | ||
387 | 392 | ||
388 | 393 | ||
389 | /** | 394 | /** |
390 | - * 事务开启 | 395 | + * 事务处理 |
396 | + * @param \Closure $call 闭包函数 返回true表示提交事务 返回false/抛出异常表示回滚事务 | ||
397 | + * @param mixed ...$params 传递给闭包函数的参数 | ||
391 | * @author:dc | 398 | * @author:dc |
392 | * @time 2023/2/17 11:35 | 399 | * @time 2023/2/17 11:35 |
393 | */ | 400 | */ |
394 | - public function transaction(){ | 401 | + public function transaction(\Closure $call, ...$params){ |
395 | $this->getClient()->beginTransaction(); | 402 | $this->getClient()->beginTransaction(); |
396 | - } | 403 | + try { |
404 | + if($call($this->getClient(),...$params)){ | ||
405 | + $this->getClient()->commit(); | ||
406 | + } | ||
407 | + }catch (\Throwable $e){ | ||
408 | + | ||
409 | + } | ||
397 | 410 | ||
398 | - /** | ||
399 | - * 事务回滚 | ||
400 | - * @author:dc | ||
401 | - * @time 2023/2/17 11:35 | ||
402 | - */ | ||
403 | - public function rollBack(){ | ||
404 | $this->getClient()->rollBack(); | 411 | $this->getClient()->rollBack(); |
405 | } | 412 | } |
406 | 413 | ||
414 | + | ||
415 | + | ||
407 | /** | 416 | /** |
408 | - * 事务提交 | 417 | + * 验证是否正常连接 |
418 | + * @return bool | ||
409 | * @author:dc | 419 | * @author:dc |
410 | - * @time 2023/2/17 11:35 | 420 | + * @time 2024/4/10 10:09 |
411 | */ | 421 | */ |
412 | - public function commit(){ | ||
413 | - $this->getClient()->commit(); | ||
414 | - } | ||
415 | - | 422 | + public function ping(){ |
423 | + try { | ||
424 | + $query = $this->getClient()->query("select 200;"); | ||
425 | + if($query->fetchColumn() == 200){ | ||
426 | + return true; | ||
427 | + } | ||
428 | + }catch (\Throwable $e){ | ||
429 | + return false; | ||
430 | + } | ||
416 | 431 | ||
417 | -// /** | ||
418 | -// * 验证是否正常连接 | ||
419 | -// * @return bool | ||
420 | -// * @author:dc | ||
421 | -// * @time 2024/4/10 10:09 | ||
422 | -// */ | ||
423 | -// public function ping(){ | ||
424 | -// try { | ||
425 | -// $query = $this->getClient()->query("select 200;"); | ||
426 | -// if($query->fetchColumn() == 200){ | ||
427 | -// return true; | ||
428 | -// } | ||
429 | -// }catch (\Throwable $e){ | ||
430 | -// return false; | ||
431 | -// } | ||
432 | -// | ||
433 | -// return false; | ||
434 | -// } | 432 | + return false; |
433 | + } | ||
435 | 434 | ||
436 | 435 | ||
437 | } | 436 | } |
-
请 注册 或 登录 后发表评论