随机出现 Null SqlCeTransaction

Posted

技术标签:

【中文标题】随机出现 Null SqlCeTransaction【英文标题】:Null SqlCeTransaction at random times 【发布时间】: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();

        trymTransaction.Commit();
        catchthrow;
        mTransaction = null;
    
    catch (Exception e)
    
        mTransaction.Rollback();
        mTransaction = null;
        throw;
    

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

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

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

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

【问题讨论】:

【参考方案1】:

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

【讨论】:

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

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

【讨论】:

以上是关于随机出现 Null SqlCeTransaction的主要内容,如果未能解决你的问题,请参考以下文章

TomCat上托管的Play项目中的随机错误:异常ClientAbortException:null

Cassandra 数据损坏:某些列上出现 NULL 值

统计随机数出现个数与直方图显示的C实现

生成随机数 + 树

随机长度字符串近似均匀拆分

检查是不是为 Null [重复]