# 7、使用事务

# 两个账户之间进行转账的简易示例

let { data = {}, userInfo, util, originalParam } = event;
let { customUtil, config, pubFun, vk, db, _ } = util;
let { uid } = data;
let res = { code : 0, msg : '' };
// 业务逻辑开始-----------------------------------------------------------
// 可写与数据库的交互逻辑等等
// 开启事务
const transaction = await vk.baseDao.startTransaction();
try {
  let dbName = "uni-id-users";
  let money = 100;
  // 给用户001减100余额
  let update1Res = await vk.baseDao.updateById({
    db:transaction,
    dbName,
    id:"001",
    dataJson:{
      account_balance:_.inc(money * -1)
    },
  });
  // 给用户2加100余额
  let update2Res = await vk.baseDao.updateById({
    db:transaction,
    dbName,
    id:"002",
    dataJson:{
      account_balance:_.inc(money)
    },
  });
  const endRes = await vk.baseDao.findById({
    db:transaction,
    dbName,
    id:"001"
  });
  if (endRes.account_balance < 0) {
    transaction.rollback(-100);
    return {
      code:-1,
      msg:"用户001账户余额不足",
      aaaAccount: endRes.account_balance
    }
  }else{
    // 提交事务
    await transaction.commit();
    console.log(`transaction succeeded`);
    return {
      code:0,
      msg:"转账成功",
      aaaAccount: endRes.account_balance
    }
  }
}catch (err) {
  // 事务回滚
  await transaction.rollback();
  console.error(`transaction error`, err)
  return {
    code: -1,
    msg:"数据库写入异常,事务已回滚",
    err: {
      message:err.message,
      stack:err.stack
    }
  }
}
// 业务逻辑结束-----------------------------------------------------------
return res;
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

# 注意:

# 暂只有以下API支持事务

  • 1、vk.baseDao.add
  • 2、vk.baseDao.findById
  • 3、vk.baseDao.updateById
  • 4、vk.baseDao.deleteById
  • 5、vk.baseDao.updateAndReturn
最后修改时间: 11/30/2021, 17:31:14