随机时间为 Null SqlCeTransaction
我有以下方法:(
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码是从多个线程调用的吗?这可以解释该行为(线程 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).
这可能是对的。尝试锁定(mTransaction)所有块。它会减慢程序速度,但肯定是安全的。
It could be right. Try lock(mTransaction) all block. it can slow down the program, but will surely be safe.