在laravel中实现事务回滚的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-12-30 10:53:54 浏览: 评论:0
今天小编就为大家分享一篇在laravel中实现事务回滚的方法,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。
之前做项目用到了事务回滚这个机制,我把代码贴出来多多交流给点意见,我用的是laravel 5.1bane版本的。
- 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) {
- //异常处理进行回滚,自己想对应的业务
- \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);
- }
- }
Tags: laravel事务回滚
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)