是否有 ISession.Merge() 的替代方案,在使用乐观锁定时不会抛出异常?
我一直在尝试使用 ISession.Merge() 来保持两个会话之间的一致性,但是当合并实例的 Version 属性高于会话中加载的版本属性时(带有 StaleObjectStateException),它会失败。
当版本字段不匹配时,是否有其他方法可以使用?
I've been trying to use ISession.Merge() to keep coherence between two sessions, but it fails when the merged instance has a higher Version property than the one loaded in the session (with a StaleObjectStateException).
Is there an alternative method that would work when the Version fields do not match ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试调用:
使用 LockMode.Force。该方法的注释指出:
对于 LockMode.Force:
Try calling:
with LockMode.Force. The remarks for that method state:
And for LockMode.Force:
似乎没有任何方法可以安全地合并会话之间的实体,至少没有乐观锁定。
我将采用另一种模式:每个会话都有自己的每个实体副本,并且我根据需要刷新每个会话上的实例。这增加了内存使用和数据库往返的开销,但它似乎有效。
There doesn't seem to be any way to safely merge entities between sessions, at least with optimistic-locking.
I'm going with another pattern: each session has its own copies of each entity, and I
refresh()
the instances on each session as needed. This has added overhead in memory usage and round-trips to the DB, but it seems to work.