解决数据库并发问题

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
@@ -525,8 +525,6 @@ namespace JinYuan.ControlCenters
LogHelper.Instance.WriteLog($"[{logName}] 反馈PLC结果:{string.Join(",", listRes)}", logName);
bool writeResult = CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
LogHelper.Instance.WriteLog($"[{logName}] 清0结果:{writeResult}", logName);
#endregion
+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); }
}
-25
View File
@@ -965,31 +965,6 @@ namespace LargeSquareOne
{
new FrmMsgBoxWithAck("批量复投条码个数>8, 请单个复投", "提示").ShowDialog();
}
//if (CommonMethods.isRepeat)
//{
// for (int i = 0; i < CommonMethods.repeatCodeList.Count; i++)
// {
// string code = CommonMethods.repeatCodeList[i];//取出队列数据
// if (code.Length > 11) //手持扫码枪的条码格式结尾带字符,到时候根据那个结尾字符判断
// {
// if (counter > 8) counter = 1; // 循环计数(1-8)
// // 将计数值写入PLC的D9050地址(16位整数)
// CommonMethods.plcDevices[0]?.WriteValue("D9050", counter, PLC.DataType.Int);
// // 写入条码到D9000
// JYResult result1 = CommonMethods.plcDevices[0].WriteValue(
// "D9000",
// code,
// PLC.DataType.String
// );
// Thread.Sleep(150);
// if (result1 == null)
// {
// CommonMethods.AddDataMonitorLog(2, "条码复投失败".Translated() + ":" + code);
// }
// counter++; // 计数器递增
// }
// }
//}
}
else if (checkBox4.Checked)//已包蓝膜
{