如何在NHibernate中同时提交两个数据库? [英] How to commit to two databases at the same time in NHibernate?

查看:68
本文介绍了如何在NHibernate中同时提交两个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保持两个数据库的镜像,但是我无法使用SQL Server镜像来做到这一点.该项目使用NHibernate 2.2作为ORM.

I need to keep two databases mirrored, but I can not do it with SQL Server mirroring. The project uses NHibernate 2.2 as ORM.

有没有可能解决此问题的方法?例如:用于NHibernate的任何插件/扩展都可以同时保存到多个数据库.

Is there any possible way to solve this problem? e.g: Any plugin/extension for NHibernate to save to multiple databases at the same time.

推荐答案

StackOverflow上有多个类似的问题.请参阅.

There are multiple similar questions on StackOverflow. Refer this, this and this.

您必须创建两个SessionFactoryISession实例,每个数据库一个.此外,您应该使用TransactionScope并将数据库操作包装在其中的多个数据库中.

You must be creating two SessionFactory and ISession instances, one for each database. Further, you should use TransactionScope and wrap your database actions against multiple databases in it.

针对以下问题之一,以下答案是通过"里卡多·佩雷斯"从答案中复制的:

Following code is copied from answer by "Ricardo Peres" for one of the questions above:

using (TransactionScope tx = new TransactionScope())
{
  using (ISession session1 = ...)
  using (ITransaction tx1 = session.BeginTransaction())
  {
    ...do work with session
    tx1.Commit();
  }

  using (ISession session2 = ...)
  using (ITransaction tx2 = session.BeginTransaction())
  {
    ...do work with session
    tx2.Commit();
  }

  tx.Complete();
}

这篇关于如何在NHibernate中同时提交两个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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