Transaction.Commit不会在NHibernate中将数据刷新到DB

时间:2011-12-09 02:30:39

标签: nhibernate commit flush

我有一个方法/单元测试用例,里面有2个事务。

第一个事务中持久存在的id对象正在第二个事务中使用。但是,数据库/ nhibernate会抛出一个错误,即持久化对象不存在且ID无效。但是,查看数据库并非如此,并且实体存在。

为什么第二个事务中的查询无法识别第一个事务所持有的实体?第二个是否有可能在第一个有机会完成之前运行?

这个问题在我们的烟雾测试版本中非常不规律但一致地发生(大概每周一次或两次)。但是,我无法通过手动运行测试在本地计算机或服务器上复制此错误。

如何调查/复制/解决此问题?

谢谢, 和Sandeep

[编辑] 以下是测试用例代码:

public void UpdateContactMethodConfirmationStatus()
    {
        string workflowId = Guid.NewGuid().ToString();
        try
        {
            Customer customer = null;
            Preference preference = null;

//the first object is persisted here (the AddPreference call has the transaction.commit call
            var preferenceRequest = new AddPreferenceRequest();
            preferenceRequest.Preference = _adminMessageBuilder.CreateDoubleOptInPreference(3);
            using (new TestTimer(TestContext, "CimAdminLoadTest", "AddPreference", workflowId))
                preference = _service.AddPreference(preferenceRequest).Preference;

//the object persisted in above transaction is being used here(preference.id), AddCustomer call also has a transaction.commit
            var request = _messageBuilder.GetAddCustomerRequest("I");
            request.Customer.Preferences.Add(_messageBuilder.AddPreferenceContactMethod(preference.Id));
            using (new TestTimer(TestContext, "CustomerLoadTest", "AddCustomer", workflowId))
                customer = _service.AddCustomer(request).Customer;

            var tenantContext = new TenantContext("CCC", "CCCTEST", "CCM");

            var commPreference = customer.Preferences.Find(x => x.PreferenceId == preference.Id) as CustomerCommunicationPreference;

            var contactMethod = commPreference.ContactMethods[0];

            var key = CustomerPreferenceConfirmationKeyHelper.GenerateCustomerPreferenceConfirmationKey(contactMethod.Id, tenantContext);
            var updateContactMethodConfirmationStatusRequest = new UpdateContactMethodConfirmationStatusRequest()
            {
                Channel = "PEG",
                IsConfirmed = true,
                ShortCode = "123",
                CustomerPreferenceContactMethodKey = key
            };
            using (new TestTimer(TestContext, "CustomerLoadTest", "ResendNotifications", workflowId))
               _service.UpdateContactMethodConfirmationStatus(updateContactMethodConfirmationStatusRequest);
        }
        catch (Exception ex)
        {
            Logger.WriteFailureMessage(ex.Message);
            throw;
        }
    }

0 个答案:

没有答案