【问题标题】:Null SqlCeTransaction at random times随机出现 Null SqlCeTransaction
【发布时间】:2011-01-04 19:57:32
【问题描述】:

我有以下方法:

public void SaveReferenceUpdates(ReferenceUpdatesDataContract updates)
{
    mTransaction = Connection.BeginTransaction();
    try
    {
        // Do all the updates to get the database inline with the contract.
        updates.UpdateFromContract();

        // Do all the deletes to get the database inline with the contract.
        updates.DeletesFromContract();

        try{mTransaction.Commit();}
        catch{throw;}
        mTransaction = null;
    }
    catch (Exception e)
    {
        mTransaction.Rollback();
        mTransaction = null;
        throw;
    }
}

(我已取出所有日志记录语句并折叠了一些方法以使其更具可读性。)

奇怪的是,有时(非常随机地)mTransaction 在进入提交步骤时为空。 (由于这种情况发生得如此随机,所以当它从连接中获取事务时,我无法查看它是否为空。)

这个方法在两个地方被调用。一种是用户登录我的应用程序时,另一种是用户一段时间不活动时(基于关闭用户输入的计时器)。

我不知道什么可能导致 SqlCeTransaction (mTransaction) 永远为空。 (帮助没有说明 BeginTransaction 返回null。)

【问题讨论】:

    标签: windows-mobile compact-framework sql-server-ce windows-mobile-6.5


    【解决方案1】:

    这段代码是从多个线程调用的吗?这可以解释这种行为(线程 2 开始一个事务,就在线程 1 完成并将其设置为 null 之前)。

    【讨论】:

    • 我认为你是对的。我需要找到一种方法来确保一次只有一个线程可以写入。
    • 您或许可以将事务传递给UpdateFromContract() 和DeleteFromContract() 方法,然后mTransaction 可以是本地变量。
    【解决方案2】:

    可能是对的。尝试锁定(mTransaction)所有块。它可以减慢程序的速度,但肯定是安全的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 2018-04-16
      • 1970-01-01
      相关资源
      最近更新 更多