itkld/distributed-transaction-db

此包最新版本(dev-master)没有可用的许可信息。

dev-master 2020-11-25 02:17 UTC

This package is auto-updated.

Last update: 2024-09-26 16:18:29 UTC


README

这是一个由数据库实现的分布式事务系统。

用法

要使用此扩展,您需要生成您的全局事务ID。

  1. 开始记录事务
    <?php
    namespace app\controllers;
    
    use app\models\Country;
    use DS_DB\DSDB;
    use Yii;
    use yii\web\Controller;
    class SiteController extends Controller
    {
        use DSDB;
        
        public function actionIndex()
        {
            // Global Transaction ID
            $gid = (string)rand();
            // Set Global Transaction ID
            self::$CURRENT_GLOBAL_ID = \Yii::$app->request->getHeaders()->get('DS-GID', $gid);
            // Start Transaction
            $this->startTransactionExec();
    //        $country = new Country();
    //        $country->setAttributes(['code' => 'DS', 'name' => 'fdfdfdfd', 'population' => 12345], false);
    //        $country->save();
    //
    //        $country->code = '00';
    //        $country->save();
    
    
            $ct = Country::find()->where(['id' => 3])->one();
            $ct->name = 'abcd4edf';
            $ct->save();
            $ct->delete();
            
            // rollback using rabbitmq
            $producer = Yii::$app->rabbitmq->getProducer('rollback');
            $msg = serialize(['GID' => $gid]);
            $producer->publish($msg, 'rollback', 'rollback');
            return $this->render('index');
        }
    }
  2. 回滚
    • restful api
      'rollback' => [
          'class' => 'DS_DB\Module',
      ]
      向"http://xxx/rollback/index/index"发送带有"DS-GID"头部的请求。
    • 手动回滚
      self::$CURRENT_GLOBAL_ID = $GID;
      $this->startTransactionRollback();