我正在研究实现IRepository模式使用NHibernate和我有问题,我一直无法回答搜索网络。C#共享事务和NHibernate使用IRepository
假设我有3个存储库,PersonRepository,PersonAddressRepository和PersonAccountRepository。现在假设业务逻辑规定存在调用PersonRepository.Deactivate(),PersonAddressRepository.Deactivate()和PersonAccountRepository.Deactivate()的“Deactivate Person”进程。
我希望能够做的线沿线的东西..
using (ITransaction transaction = session.BeginTransaction()) {
session.Update(Person);
session.Update(PersonAddress);
session.Update(PersonAccount);
}
因此,如果其中任何更新失败,整个过程滚动数据库内回来。现在,此刻我的NHibernate的理解是,你只能创建每个对象,让会话..
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Person).Assembly);
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
using (ISession session = sessionFactory.OpenSession()) {
using (ITransaction transaction = session.BeginTransaction()) {
session.Save(Person);
}
这是正确的还是我错了?与NHibernate有关的多表更新和事务的事务最佳实践是什么?
在此先感谢。
嗨,谢谢你的回答,我的后续问题是,这个TransactionService是否有效地管理着一个事务池,这样如果一个事务失败了,那么池中的所有事务都会回滚?或者是否有跨多个存储库共享1个事务的方式? – David 2011-02-17 09:35:34
每个线程都有一个事务。当然,其他并行事务在其他线程上进行。 – 2011-02-17 11:28:35