easyswoole开启事务其实和原生sql一样,首先我们安装easyswoole的mysqli-pool组件:composer require easyswoole/mysqli-pool
在dev.php添加配置:'MYSQL' => [
'host' => '',
'port' => 3306,
'user' => '',
'password' => '',
'database' => '',
'timeout' => 30,
'charset' => 'utf8mb4',
'connect_timeout' => '5',//连接超时时间
],
主进程注册
一般的,我们在EasySwoole的全局initialize 事件中,进行注册use EasySwoole\EasySwoole\Config as GConfig;
use EasySwoole\MysqliPool\Mysql;
use EasySwoole\Mysqli\Config;
$configData = GConfig::getInstance()->getConf('MYSQL');
$config = new Config($configData);
/**
这里注册的名字叫mysql,你可以注册多个,比如mysql2,mysql3
*/
$poolConf = Mysql::getInstance()->register('mysql',$config);
//$poolConf->setMaxObjectNum($configData['maxObjectNum']);
//$poolConf->setMinObjectNum($configData['minObjectNum']);
调用mysqli操作事务$db = \EasySwoole\MysqliPool\Mysql::defer('mysql');
$db->startTransaction();
$db->commit();
$db->rollback();