PHP 简单的事务用法实例+(Exception 错误自定义;抛出:laraval实现)
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use Illuminate\Http\Request;
6
7 use Illuminate\Support\Facades\DB;
8
9
10 class ManayController extends Controller
11 {
12 public function index(){
13
14 try {
15
16 DB::beginTransaction();
17
18 $res1 = DB::table('manay')->where('id', '1')->increment('manay', 200);
19
20 $res2 = DB::table('manay')->where('id', '2')->decrement('manay', 200);
21
22 if($res1==true && $res2==true){
23
24 DB::commit();
25
26 }else{
27
28 $this->fun();
29
30 }
31
32 }catch (Exception $e1){
33
34 echo "发生错误(代号:". $e1->getCode() . ")";
35
36 echo $e1->getMessage();
37
38 DB::rollBack();
39
40
41
42 }
43
44
45 }
46
47 public function fun(){
48
49 throw new \Exception('有错误','300');
50
51
52
53 }
54
55
56
57 }