0

我有以下方法:

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。)

4

2 回答 2

1

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

于 2011-01-05T08:04:20.057 回答
0

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

于 2011-01-07T00:21:06.207 回答