mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2026-03-22 14:17:21 +08:00
【优化】并发下单场景用户金额变动增加事务锁的悲观锁等待模式,防止出现金额异常情况。
This commit is contained in:
@@ -229,6 +229,18 @@ namespace CoreCms.Net.IRepository
|
||||
Task<T> QueryByClauseAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByPredicate,
|
||||
OrderByType orderByType, bool blUseNoLock = false);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件查询数据(悲观锁等待模式)
|
||||
/// </summary>
|
||||
/// <param name="predicate">条件表达式树</param>
|
||||
/// <param name="orderByPredicate">排序字段</param>
|
||||
/// <param name="orderByType">排序顺序</param>
|
||||
/// <param name="blUseTranLock">是否使用TranLock</param>
|
||||
/// <returns></returns>
|
||||
Task<T> QueryByClauseWithTranLockAsync(Expression<Func<T, bool>> predicate,
|
||||
Expression<Func<T, object>> orderByPredicate, OrderByType orderByType, bool blUseTranLock = false);
|
||||
|
||||
/// <summary>
|
||||
/// 写入实体数据
|
||||
/// </summary>
|
||||
|
||||
@@ -230,6 +230,18 @@ namespace CoreCms.Net.IServices
|
||||
Task<T> QueryByClauseAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByPredicate,
|
||||
OrderByType orderByType, bool blUseNoLock = false);
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件查询数据(悲观锁等待模式)
|
||||
/// </summary>
|
||||
/// <param name="predicate">条件表达式树</param>
|
||||
/// <param name="orderByPredicate">排序字段</param>
|
||||
/// <param name="orderByType">排序顺序</param>
|
||||
/// <param name="blUseTranLock">是否使用TranLock</param>
|
||||
/// <returns></returns>
|
||||
Task<T> QueryByClauseWithTranLockAsync(Expression<Func<T, bool>> predicate,
|
||||
Expression<Func<T, object>> orderByPredicate, OrderByType orderByType, bool blUseTranLock = false);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 写入实体数据
|
||||
/// </summary>
|
||||
|
||||
@@ -373,6 +373,25 @@ namespace CoreCms.Net.Repository
|
||||
: await DbBaseClient.Queryable<T>().OrderBy(orderByPredicate, orderByType).FirstAsync(predicate);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件查询数据(悲观锁等待模式)
|
||||
/// </summary>
|
||||
/// <param name="predicate">条件表达式树</param>
|
||||
/// <param name="orderByPredicate">排序字段</param>
|
||||
/// <param name="orderByType">排序顺序</param>
|
||||
/// <param name="blUseTranLock">是否使用TranLock</param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> QueryByClauseWithTranLockAsync(Expression<Func<T, bool>> predicate,
|
||||
Expression<Func<T, object>> orderByPredicate, OrderByType orderByType, bool blUseTranLock = false)
|
||||
{
|
||||
return blUseTranLock
|
||||
? await DbBaseClient.Queryable<T>().TranLock(DbLockType.Wait).OrderBy(orderByPredicate, orderByType)
|
||||
.FirstAsync(predicate)
|
||||
: await DbBaseClient.Queryable<T>().OrderBy(orderByPredicate, orderByType).FirstAsync(predicate);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 写入实体数据
|
||||
/// </summary>
|
||||
|
||||
@@ -300,6 +300,21 @@ namespace CoreCms.Net.Services
|
||||
return await BaseDal.QueryByClauseAsync(predicate, orderByPredicate, orderByType, blUseNoLock);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件查询数据(悲观锁等待模式)
|
||||
/// </summary>
|
||||
/// <param name="predicate">条件表达式树</param>
|
||||
/// <param name="orderByPredicate">排序字段</param>
|
||||
/// <param name="orderByType">排序顺序</param>
|
||||
/// <param name="blUseTranLock">是否使用TranLock</param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> QueryByClauseWithTranLockAsync(Expression<Func<T, bool>> predicate,
|
||||
Expression<Func<T, object>> orderByPredicate, OrderByType orderByType, bool blUseTranLock = false)
|
||||
{
|
||||
return await BaseDal.QueryByClauseWithTranLockAsync(predicate, orderByPredicate, orderByType, blUseTranLock);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入实体数据
|
||||
/// </summary>
|
||||
|
||||
@@ -64,14 +64,21 @@ namespace CoreCms.Net.Services
|
||||
|
||||
if (money != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
_unitOfWork.BeginTran();
|
||||
|
||||
//取用户实际余额
|
||||
var userInfo = await userServices.QueryByIdAsync(userId);
|
||||
//(会员陌小北提供)
|
||||
var userInfo = await userServices.QueryByClauseWithTranLockAsync(p => p.id == userId, p => p.id, OrderByType.Desc, true);
|
||||
|
||||
if (userInfo == null)
|
||||
{
|
||||
jm.data = jm.code = 11004;
|
||||
jm.msg = GlobalErrorCodeVars.Code11004;
|
||||
return jm;
|
||||
}
|
||||
|
||||
//取描述,并简单校验
|
||||
var res = UserHelper.GetMemo(type, money, cateMoney);
|
||||
if (string.IsNullOrEmpty(res))
|
||||
@@ -85,17 +92,15 @@ namespace CoreCms.Net.Services
|
||||
|
||||
}
|
||||
//如果是减余额的操作,还是加余额操作
|
||||
if (type == (int)GlobalEnumVars.UserBalanceSourceTypes.Pay || type == (int)GlobalEnumVars.UserBalanceSourceTypes.Tocash)
|
||||
if (type is (int)GlobalEnumVars.UserBalanceSourceTypes.Pay or (int)GlobalEnumVars.UserBalanceSourceTypes.Tocash)
|
||||
{
|
||||
money = -money - cateMoney;
|
||||
}
|
||||
if (type != (int)GlobalEnumVars.UserBalanceSourceTypes.Service)
|
||||
{
|
||||
//后台充值或调不改绝对值
|
||||
|
||||
}
|
||||
|
||||
|
||||
var balance = userInfo.balance + money;
|
||||
if (balance < 0)
|
||||
{
|
||||
@@ -119,6 +124,12 @@ namespace CoreCms.Net.Services
|
||||
|
||||
jm.data = balanceModel;
|
||||
|
||||
_unitOfWork.CommitTran();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_unitOfWork.RollbackTran();
|
||||
}
|
||||
}
|
||||
jm.status = true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user