解决数据库并发问题

This commit is contained in:
lq
2026-09-09 17:50:15 +08:00
parent e703fb2752
commit 367281d8ce
3 changed files with 77 additions and 96 deletions
+77 -69
View File
@@ -356,21 +356,28 @@ namespace JinYuan.DAL
/// <returns></returns>
/// <exception cref="Exception"></exception>
public Task<T> QueryAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
{
return QueryAsyncImpl<T>(whereLambda);
}
private async Task<T> QueryAsyncImpl<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
{
try
{
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
if (whereLambda != null)
var config = SqlSugarDb.CurrentConnectionConfig;
using (var db = new SqlSugarClient(new ConnectionConfig()
{
up = up.Where(whereLambda);
ConnectionString = config.ConnectionString,
DbType = config.DbType,
IsAutoCloseConnection = true
}))
{
var up = db.Queryable<T>().With(SqlWith.NoLock);
if (whereLambda != null) up = up.Where(whereLambda);
return await up.FirstAsync();
}
return up.FirstAsync();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
/// <summary>
@@ -505,7 +512,7 @@ namespace JinYuan.DAL
{
up = up.Where(whereLambda);
}
if (sort != "")
if (!string.IsNullOrEmpty(sort))
{
var orderBys = ParseOrderBy(sort, byType);
up = up.OrderBy(orderBys).Take(Take);
@@ -537,7 +544,7 @@ namespace JinYuan.DAL
{
up = up.Where(whereLambda);
}
if (sort != "")
if (!string.IsNullOrEmpty(sort))
{
var orderBys = ParseOrderBy(sort, byType);
up = up.OrderBy(orderBys);
@@ -876,14 +883,18 @@ namespace JinYuan.DAL
{
try
{
var result = await SqlSugarDb.Insertable(entity).With(SqlWith.UpdLock).ExecuteCommandAsync() > 0;
return result;
var config = SqlSugarDb.CurrentConnectionConfig;
using (var db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = config.ConnectionString,
DbType = config.DbType,
IsAutoCloseConnection = true
}))
{
return await db.Insertable(entity).With(SqlWith.UpdLock).ExecuteCommandAsync() > 0;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
/// <summary>
@@ -912,15 +923,18 @@ namespace JinYuan.DAL
{
try
{
//var result = await SqlSugarDb.Insertable(entitys).With(SqlWith.UpdLock).ExecuteCommandAsync() > 0;
var result = await SqlSugarDb.Insertable(entitys).ExecuteCommandAsync() > 0;
return result;
var config = SqlSugarDb.CurrentConnectionConfig;
using (var db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = config.ConnectionString,
DbType = config.DbType,
IsAutoCloseConnection = true
}))
{
return await db.Insertable(entitys).ExecuteCommandAsync() > 0;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
#endregion
@@ -1104,23 +1118,21 @@ namespace JinYuan.DAL
{
try
{
IUpdateable<T> up = SqlSugarDb.Updateable<T>().SetColumns(update);
if (where != null)
var config = SqlSugarDb.CurrentConnectionConfig;
using (var db = new SqlSugarClient(new ConnectionConfig()
{
up = up.Where(where);
}
if (isLock)
ConnectionString = config.ConnectionString,
DbType = config.DbType,
IsAutoCloseConnection = true
}))
{
up = up.With(SqlWith.UpdLock);
var up = db.Updateable<T>().SetColumns(update);
if (where != null) up = up.Where(where);
if (isLock) up = up.With(SqlWith.UpdLock);
return await up.ExecuteCommandAsync();
}
var result = await up.ExecuteCommandAsync();
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
@@ -1178,30 +1190,22 @@ namespace JinYuan.DAL
{
try
{
// 每次获取新的 SqlSugarScope 实例,避免多线程冲突
// IsAutoCloseConnection = true 已配置,无需using语句
IUpdateable<T> up = SqlSugarDb.Updateable<T>(entity);
if (IgnoreColumns != null)
var config = SqlSugarDb.CurrentConnectionConfig;
using (var db = new SqlSugarClient(new ConnectionConfig()
{
up = up.UpdateColumns(IgnoreColumns);
}
if (where != null)
ConnectionString = config.ConnectionString,
DbType = config.DbType,
IsAutoCloseConnection = true
}))
{
up = up.Where(where);
var up = db.Updateable<T>(entity);
if (IgnoreColumns != null) up = up.UpdateColumns(IgnoreColumns);
if (where != null) up = up.Where(where);
if (isLock) up = up.With(SqlWith.UpdLock);
return await up.ExecuteCommandAsync();
}
if (isLock)
{
up = up.With(SqlWith.UpdLock);
}
var result = await up.ExecuteCommandAsync();
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
@@ -1535,20 +1539,24 @@ namespace JinYuan.DAL
/// <param name="isLock"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public Task<int> DeleteAsync<T>(Expression<Func<T, bool>> where, bool isLock = true) where T : class, new()
public async Task<int> DeleteAsync<T>(Expression<Func<T, bool>> where, bool isLock = true) where T : class, new()
{
try
{
var result = isLock ?
SqlSugarDb.Deleteable<T>().Where(where).With(SqlWith.RowLock).ExecuteCommandAsync()
: SqlSugarDb.Deleteable<T>().Where(where).ExecuteCommandAsync();
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
var config = SqlSugarDb.CurrentConnectionConfig;
using (var db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = config.ConnectionString,
DbType = config.DbType,
IsAutoCloseConnection = true
}))
{
return isLock
? await db.Deleteable<T>().Where(where).With(SqlWith.RowLock).ExecuteCommandAsync()
: await db.Deleteable<T>().Where(where).ExecuteCommandAsync();
}
}
catch (Exception ex) { throw new Exception(ex.Message); }
}