NHibernate具有多个数据库和事务 [英] NHibernate with multiple databases and transactions

查看:94
本文介绍了NHibernate具有多个数据库和事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在理解如何最好地使用NHibernate时遇到了一些问题.通常,我们有相对大量的(相对于表而言)很小的SQL Server数据库,而不是一个包含很多对象的数据库.

We are having a few problems understanding how best to use NHibernate. We typically have a relatively large number of quite small (in terms of number of tables) SQL Server databases rather than one database with a lot of objects.

我们正在研究用于处理多个会话工厂的各种选项,并且很可能对此加以控制.但是,我们不确定如何将所有调用包装在一个事务中.使用手动滚动数据访问功能,您只需将其全部包装在TransactionScope中,但是我们不太愿意使用NHibernate进行此操作,因为它似乎希望处理其自己的所有事务.

We are looking at various options for handling multiple session factories and probably have this under control. However we are not sure how we would wrap all calls in a single transaction. Using hand-rolled data access you would just wrap it all in a TransactionScope but we are a little reluctant to do this with NHibernate as it seems to like to handle all its own Transactions.

使用具有共享事务的多个数据库似乎是很多人想要使用NHibernate进行的事情.

Using multiple databases with shared transactions seems like the sort of thing lots of people would want to do with NHibernate.

我们只是完全把错误的树种了吗?

Are we just barking up the wrong tree entirely?

推荐答案

您可以安全地使用TransactionScope.但是您也必须打开NHibernate事务.

You can safely use TransactionScope. But you have to open NHibernate transactions too.

示例:

using (var ts = new TransactionScope())
using (var session1 = sessionFactory1.OpenSession())
using (var tx1 = session1.BeginTransaction())
using (var session2 = sessionFactory2.OpenSession())
using (var tx2 = session2.BeginTransaction())
{
    //Do work...
    tx1.Commit();
    tx2.Commit();
    ts.Complete();
}

这篇关于NHibernate具有多个数据库和事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆