在laravel中实现事务回滚的方法

2025-05-27 0 100

之前做项目用到了事务回滚这个机制。我把代码贴出来多多交流给点意见,我用的是laravel 5.1bane版本的,

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41
public static function createDeal($to_status, $params, $new_balance, $update = true)

{

\\DB::beginTransaction();

try {

$update_order_status = \\DB::table('wallet_order')

->where('order_id', $params['order_id'])

->update(['to_status' => $to_status, 'update_time' => $params['pay_time']]);

if (!$update_order_status) {

throw new \\Exception("update order error");

}

$create_deal_status = \\DB::table('wallet_deal')

->insert($params);

if (!$create_deal_status) {

throw new \\Exception("create deal error");

}

if ($update) {

$update_manage_status = self::updateManage(['balance' => $new_balance, 'update_time' => $params['pay_time']], $params['from_user']);

if (!$update_manage_status) {

throw new \\Exception("update manage error");

}

} else {

$manage_params = [

'user_id' => intval($params['from_user']),

'balance' => $new_balance,

'add_time' => $params['pay_time'],

'update_time' => $params['pay_time'],

];

$create_manage_status = self::createManage($manage_params);

if (!$create_manage_status) {

throw new \\Exception("create manage error");

}

}

\\DB::commit();

} catch (\\Exception $e) {

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
//异常处理进行回滚,自己想对应的业务

\\DB::rollback();

$trouble_params = [

'order_id' => $params['order_id'],

'deal_id' => $params['deal_id'],

'from_user' => $params['from_user'],

'to_user' => $params['to_user'],

'total_amount' => $params['total_amount'],

'add_time' => $params['pay_time'],

'type' => $params['type'],

'to_status' => $to_status

];

\\DB::connection('mongodb')

->table('wallet_trouble')

->insert($trouble_params);

} finally {

self::createLog($params, $to_status);

}

}

以上这篇在laravel中实现事务回滚的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/chen529834149/article/details/77161485

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 在laravel中实现事务回滚的方法 https://www.kuaiidc.com/71013.html

相关文章

发表评论
暂无评论