随机时间为 Null SqlCeTransaction

发布于 2024-10-10 11:46:17 字数 959 浏览 2 评论 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。)

I have the following method:

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;
    }
}

(I have taken out all my logging statements and collapsed a few methods to make this more readable.)

The weird thing is that sometimes (very very randomly) mTransaction is null when it gets to the commit step. (Since this happens so randomly, I have not been able to see if it is null when it gets the transaction from the connection.)

This method is called in two places. One is at the time the user logs into the my app and the other is when the user is inactive for a time (based on a timer that keys off of user input).

I am stumped as to what could cause SqlCeTransaction (mTransaction) to ever be null. (The help does not say anything about BeginTransaction returning null.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你穿错了嫁妆 2024-10-17 11:46:17

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

Is this code called from multiple threads? That would explain the behavior (thread 2 starts a transaction, just before thread 1 finishes and sets it to null).

帅哥哥的热头脑 2024-10-17 11:46:17

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

It could be right. Try lock(mTransaction) all block. it can slow down the program, but will surely be safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文