【发布时间】:2010-06-15 00:39:53
【问题描述】:
我的所有 ASP.NET 请求都包含在一个 Session 和一个仅在请求结束时提交的 Transaction 中。
在请求执行期间的某个时刻,我想插入一个对象并使其对其他潜在线程可见 - 即将插入拆分为一个新事务,提交该事务,然后继续。
原因是有问题的请求访问了一个 API,然后该 API 链接访问了我的另一个页面(接近同步),让我知道它已处理,因此重复提交了交易记录,因为原始请求没有尚未完成,因此未提交交易记录。
所以我尝试使用新的 SessionScope、TransactionScope(TransactionMode.New)、两者的组合、手动刷新所有内容等来包装插入代码。
但是,当我在对象上调用 Refresh 时,我仍然得到旧的对象状态。
这是我所看到的一些代码示例:
Post outsidePost = Post.Find(id); // status of this post is Status.Old
using (TransactionScope transaction = new TransactionScope(TransactionMode.New))
{
Post p = Post.Find(id);
p.Status = Status.New; // new status set here
p.Update();
SessionScope.Current.Flush();
transaction.Flush();
transaction.VoteCommit();
}
outsidePost.Refresh();
// refresh doesn't get the new status, status is still Status.Old
感谢任何建议、想法和 cmets!
【问题讨论】:
标签: asp.net nhibernate castle-activerecord transactions