收藏
回答

云数据库 持续 ResourceUnavailable.TransactionBusy

框架类型 问题类型 终端类型 AppID 环境ID 基础库版本
小程序 Bug 工具 wxaef1b428d778f802 cloud1-5g3heh176335048c 2.14.1

user 和 reservation table 的 where 都是unique key 只返回一个entry


Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID local_debug_d527cb6d-0088-4602-9171-a3a25cf8aef8, cloud function service error code -1, error message collection.get:fail -501001 resource system error. [ResourceUnavailable.TransactionBusy] Transaction is busy. Please check your request, but if the problem persists, contact us.; at cloud.callFunction api; 

exports.enrollCourse = async (event) => { try { await db.runTransaction(async transaction => { let futures = [] let userQuery = {} userQuery[users.schema.openId] = event.userId futures.push(transaction.collection('users').where(userQuery).get()) futures.push(transaction.collection('slots').doc(event.slotId).get()) futures.push(transaction.collection('courses').doc(event.courseId).get()) let resQuery = {} resQuery[reservations.schema.userId] = event.userId resQuery[reservations.schema.slotId] = event.slotId futures.push(transaction.collection('reservations').where(resQuery).get()) let result = await Promise.all(futures) console.log(result) let user = result[0] let slot = result[1] let course = result[2] let res = result[3] if (res.data.length != 0) { await transaction.rollback(-1) return } if (!course.data) { await transactions.rollback(-2) return } if (!slot.data) { await transactions.rollback(-3) return } if (!user.data) { await transactions.rollback(-4) return } if (slot.data[slots.schema.enrolledNumber] >= course.data[courses.schema.capacity]) { await transactions.rollback(-5) return } if (slot.data[slots.schema.limitCourseId] != undefined && slot.data[slots.schema.limitCourseId] != course.data[courses.schema.id]) { await transactions.rollback(-6) return } if (user.data[users.schema.balance] < course.data[courses.schema.price]) { await transactions.rollback(-7) return } let writes = [] userUpdate = {} userUpdate[users.schema.balance] = _.inc(-course.data[courses.schema.price]) writes.push(transaction.collection('users').where(userQuery).update(userUpdate)) slotUpdate = {} slotUpdate[slots.schema.enrolledNumber] = _.inc(1) writes.push(transaction.collection('slots').doc(event.slotId).update(slotUpdate)) data = {} data[reservations.schema.userId] = event.userId data[reservations.schema.slotId] = event.slotId data[reservations.schema.startTs] = slot.data[slots.schema.startTs] data[reservations.schema.courseId] = event.courseId data[reservations.schema.courseName] = course.data[courses.schema.courseName] data[reservations.schema.price] = course.data[courses.schema.price] data[reservations.schema.userId] = !event.isPublic writes.push(transaction.collection('slots').add.update({ data: data })) try { await Promise.all(data) } catch (e) { console.error(e) await transactions.rollback(-500) } }) } catch (e) { console.error(e) throw e } }
回答关注问题邀请回答
收藏
登录 后发表内容
问题标签