其实应该算是补遗了,之前的一篇随笔曾经介绍了 如何在XPO中为非主键字段获取Int型自增量 ,但今次在实际应用中发现该段代码是有问题的。
最大的问题,它并不线程安全的,这一点在今天的一个Parallel.For测试中暴露无遗。
原代码使用一个MaxIdGenerationAttempts = 7的常量来控制重试次数,如果发现当前保存操作引发了LockingException则歇一段时间重试。这样有2个问题,一来它并没有根本上解决线程安全问题,在并发量稍高一点的情况下,某条不走运的线程重试了7次依然无法成功完成操作后还是会引发异常;二来多次的读写尝试耗费了大量的时间。加大这个常量可以延后引发异常的时间点(或者说降低一点概率),但明显依旧无法根本上解决问题,并且失败但仍旧占用了资源的读写操作也更多。
改动其实也简单,加一个线程锁就行。顺带我把model独立出来了,个人比较喜欢这样,看起来清爽。另外也改成了泛型的方法,用起来稍方便点。
public sealed class Sequencer
{
private readonly static object locker = new object();
public static uint GetNextValue<T>(IDataLayer dataLayer)
{
if(dataLayer == null)
throw new ArgumentNullException("dataLayer");
lock (locker)
{
using (Session session = new Session(dataLayer))
{
string typename = session.GetClassInfo<T>().FullName;
XpoSequence seq = session.FindObject<XpoSequence>(XpoSequence.Fields.TypeName == typename);
if (seq == null)
{
seq = new XpoSequence(session);
seq.TypeName = typename;
}
seq.CurrentID++;
seq.Save();
return seq.CurrentID;
}
}
}
public static uint GetNextValue<T>()
{
return GetNextValue<T>(XpoDefault.DataLayer);
}
}
{
private readonly static object locker = new object();
public static uint GetNextValue<T>(IDataLayer dataLayer)
{
if(dataLayer == null)
throw new ArgumentNullException("dataLayer");
lock (locker)
{
using (Session session = new Session(dataLayer))
{
string typename = session.GetClassInfo<T>().FullName;
XpoSequence seq = session.FindObject<XpoSequence>(XpoSequence.Fields.TypeName == typename);
if (seq == null)
{
seq = new XpoSequence(session);
seq.TypeName = typename;
}
seq.CurrentID++;
seq.Save();
return seq.CurrentID;
}
}
}
public static uint GetNextValue<T>()
{
return GetNextValue<T>(XpoDefault.DataLayer);
}
}
[Persistent("Sequence")]
public class XpoSequence : XPBaseObject
{
public XpoSequence()
: base()
{
// This constructor is used when an object is loaded from a persistent storage.
// Do not place any code here.
}
public XpoSequence(Session session)
: base(session)
{
// This constructor is used when an object is loaded from a persistent storage.
// Do not place any code here.
}
public override void AfterConstruction()
{
base.AfterConstruction();
// Place here your initialization code.
}
// Fields...
private uint _CurrentID;
private string _TypeName;
[Key]
public string TypeName
{
get
{
return _TypeName;
}
set
{
SetPropertyValue("TypeName", ref _TypeName, value);
}
}
public uint CurrentID
{
get
{
return _CurrentID;
}
set
{
SetPropertyValue("CurrentID", ref _CurrentID, value);
}
}
}
public class XpoSequence : XPBaseObject
{
public XpoSequence()
: base()
{
// This constructor is used when an object is loaded from a persistent storage.
// Do not place any code here.
}
public XpoSequence(Session session)
: base(session)
{
// This constructor is used when an object is loaded from a persistent storage.
// Do not place any code here.
}
public override void AfterConstruction()
{
base.AfterConstruction();
// Place here your initialization code.
}
// Fields...
private uint _CurrentID;
private string _TypeName;
[Key]
public string TypeName
{
get
{
return _TypeName;
}
set
{
SetPropertyValue("TypeName", ref _TypeName, value);
}
}
public uint CurrentID
{
get
{
return _CurrentID;
}
set
{
SetPropertyValue("CurrentID", ref _CurrentID, value);
}
}
}
简单试了一下,原来代码使用的那种反复重试的方式实际上比线程锁消耗的时间更多得多,这也好理解,一个线程锁消耗的时间比反复去数据库端尝试读写便宜多了。
但这代码实际上仍然不完美, 例如当我们要用这段代码为User,Product,Order等多个类生成ID时,都会受到同一个线程锁的影响,实际上这是不必要的,理想的情况下User管User锁,Product管Product锁就行了,以此类推。目前这段代码在我的项目中用得不多,这样还OK了就先不动了。或者您有好办法的话,不吝赐教啊。
标签:
DevExpress
, XPO
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述