C# NHibernate事务在Postgres上无法正常工作
我有桌面应用程序和web应用程序引用的公共dll。其中一个方法创建3个对象并将它们插入事务中的DB中C# NHibernate事务在Postgres上无法正常工作,c#,postgresql,nhibernate,transactions,C#,Postgresql,Nhibernate,Transactions,我有桌面应用程序和web应用程序引用的公共dll。其中一个方法创建3个对象并将它们插入事务中的DB中 Notifications not = new Notifications(); not.Notes = applicationName; not.GeneratedTime = DateTime.Now; using (var session = SessionManager
Notifications not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
using (var session = SessionManager.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
// do what you need to do with the session
session.Save(not);
transaction.Commit();
}
但是,当我同时从web应用程序和桌面应用程序调用此方法时,对象不是3by3插入的…它们的顺序是混合的(1来自桌面应用程序,1来自web应用程序,等等)
Notifications not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
using (var session = SessionManager.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
// do what you need to do with the session
session.Save(not);
transaction.Commit();
}
我的代码可以吗?是否与映射或nhibernate cfg有关???
事先非常感谢
Notifications not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
using (var session = SessionManager.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
// do what you need to do with the session
session.Save(not);
transaction.Commit();
}
您错误地认为,在一个事务中执行插入将防止在其他并发事务中执行插入
Notifications not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
using (var session = SessionManager.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
// do what you need to do with the session
session.Save(not);
transaction.Commit();
}
这是不正确的(除非您使用特定的事务隔离级别,以独占方式锁定整个表等等……但我们只能说这不是正确的)关系数据库中的行没有“排序”。它们的存储顺序完全不相关。只需确保在检索数据时应用适当的
ORDER BY
。这可以通过使用时间戳列来实现,该列存储插入行的时间。非常感谢,这很有帮助。
Notifications not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
using (var session = SessionManager.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
session.Save(not);
not = new Notifications();
not.Notes = applicationName;
not.GeneratedTime = DateTime.Now;
// do what you need to do with the session
session.Save(not);
transaction.Commit();
}