NHibernate with multiple databases and transactions

一曲冷凌霜 提交于 2019-12-06 11:43:35

问题


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.

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.

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?


回答1:


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

Example:

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();
}


来源:https://stackoverflow.com/questions/3024318/nhibernate-with-multiple-databases-and-transactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!