我可以在交易中打开多个会话吗?

时间:2013-11-07 00:50:53

标签: session nhibernate transactions

我随机收到错误,但有以下异常:

  

NHibernate.TransactionException:事务未成功启动   在NHibernate.Transaction.AdoTransaction.Rollback()

代码如下:

try
        {
            using (var session = SessionProvider.Instance.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        foreach (var donationLineEntity in donationEntity.DonationLines)
                        {

                            if (donationLineEntity.EventContributions != null)
                            {
                                // If we need to update the total donations for an associated fundraising page
                                if (updateTotals)
                                {
                                    switch ((DonationChannel)donationEntity.DonationSourceId)
                                    {
                                        case DonationChannel.FundraiserPage:
                                            UpdateFundraiserPageSummary(session,
                                                                        DeriveDonationTotalAmount(donationLineEntity),
                                                                        donationLineEntity.EventContributions[0]
                                                                            .EventGivingGroupId, IsRefund(donationLineEntity));
                                            break;
                                        case DonationChannel.Team:
                                            UpdateTeamSummary(session, DeriveDonationTotalAmount(donationLineEntity),
                                                              donationLineEntity.EventContributions[0].TeamId, IsRefund(donationLineEntity));
                                            break;
                                    }
                                }

                                donationLineEntity.EventContributions[0].ProcessStatusId =
                                    donationEntity.ProcessStatusId;
                            }
                        }

                        session.Update(donationEntity);

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new PersistenceException("There was a problem during the donation save transaction", ex);
                    }
                }

                session.Flush();
            }


        }
        catch (Exception ex)
        {
            throw new PersistenceException("There was a problem during the donation save session", ex);
        }

现在,在switch语句中你会看到“UpdateFundraiserPageSummary”和“UpdateTeamSummary”。这两个使用以下方式打开新会话:

SessionProvider.Instance.OpenStatelessSession()

两个“更新”方法的实际代码如下所示:

var result = _session.CreateQuery(@"
            update Bb02TeamTotal t
            set t.TotalRaisedOnline = t.TotalRaisedOnline + :amount,
                t.TotalContributionsOnline = t.TotalContributionsOnline + 1,
                t.LastContributionDate = :lastModified
            where t.TeamId = :id"
            )
            .SetDecimal("amount", _amount)
            .SetDateTime("lastModified", DateTime.Now)
            .SetInt32("id", _teamId)
            .ExecuteUpdate();

问题是...... 我应该使用我在“using(var session = SessionProvider.Instance.OpenSession())”中为这两个更新打开的相同会话吗?它们是否仍会包含在交易范围内?我想知道在尝试调用Rollback()时,这两个嵌套会话是否会导致异常。

0 个答案:

没有答案