【问题标题】:Handle multiple database connection in one transaction scope在一个事务范围内处理多个数据库连接
【发布时间】:2016-03-14 10:36:40
【问题描述】:

我在一个transaction scope 中编写了以下函数having two database context。我正在使用MySqlEF 5.0

 private static void SyncPremiumStores(JoyRydeWebPortalData.joyryde_WebPortalEntities contextWebPortal, JoyRydeMallStoreData.joyryde_MallStoreEntities contextMallStore)
    {
        using (TransactionScope scope = new TransactionScope())
        {
            foreach (var objWebPortalPremiumStore in contextWebPortal.tbl_premium_store.Where(x => x.INT_DATA_TRANS_STATUS == 0).ToList())
            {
                try
                {
                    var objMallStore = contextMallStore.tbl_store.Where(x => x.LNG_STORE_ID == objWebPortalPremiumStore.LNG_STORE_ID).FirstOrDefault();
                    if (objMallStore != null)
                    {
                        JoyRydeMallStoreData.tbl_premium_store objMallPremiumStore = new JoyRydeMallStoreData.tbl_premium_store()
                        {
                            DAT_CREATED = objWebPortalPremiumStore.DAT_CREATED,
                            DAT_PREMIUM_FROM = objWebPortalPremiumStore.DAT_PREMIUM_FROM,
                            DAT_PREMIUM_TO = objWebPortalPremiumStore.DAT_PREMIUM_TO,
                            LNG_PRIMARY_STORE_ID = objMallStore.LNG_PRIMARY_STORE_ID,
                            LNG_STORE_ID = objMallStore.LNG_STORE_ID,
                            TXT_PACK_NAME = ""
                        };
                        contextMallStore.tbl_premium_store.Add(objMallPremiumStore);
                        objWebPortalPremiumStore.INT_DATA_TRANS_STATUS = 1;
                    }

                    contextMallStore.SaveChanges();
                    contextWebPortal.SaveChanges();
                    scope.Complete();
                }
                catch (Exception ex)
                {
                    LogUtility.WriteErrorLog(ex);
                }
            }
        }
    }

在执行时,它会抛出错误

var objMallStore = contextMallStore.tbl_store.Where(x => x.LNG_STORE_ID == objWebPortalPremiumStore.LNG_STORE_ID).FirstOrDefault(); 带有内部异常消息的行

Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.

我需要在单个事务中更新两个不同的数据库。 怎么办??

【问题讨论】:

标签: c# mysql entity-framework transactionscope


【解决方案1】:

我认为这是 MySQL 的限制,因为只有 XA 事务支持分布式事务(参与全局事务的多个独立事务资源)。

据我所知,MySql .net 连接器确实支持分布式事务。尝试在连接字符串中设置AutoEnlist=false

【讨论】:

  • 通过添加AutoEnlist = false,它现在对我有效。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多