使用嵌套事务范围更新数据库
Update database with nested transactions scope
在我的应用程序中,我有两种方法:GetPaymentToDate
和 RemovePayment
:
public Payment RemovePayment(int paymentId)
{
Payment payment;
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
{
//some staff
m_staffContext.SaveChanges();
transaction.Complete();
}
return payment;
}
public Payment GetPaymentToDate(DateTime paymentDate)
{
var payment = new Payment
{
//initialize properties
};
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
{
m_staffContext.Payments.Add(payment);
m_staffContext.SaveChanges();
transaction.Complete();
}
return payment;
}
不,我需要实施 Update 方法。此方法的逻辑是删除旧付款,然后创建新付款。所以我想在一个父事务范围内执行此操作,如果另一个事务失败则角色返回嵌套事务。我将从现有方法中删除 TransactionScopeOption.RequiresNew
选项,并在更新方法中编写如下内容:
public Payment UpdatePayment(int paymentId)
{
Payment newPayment;
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
{
var removedPayment = RemovePayment(paymentId);
var newPayment = GetPaymentToDate(removedPayment.Date);
m_staffContext.SaveChanges();
transaction.Complete();
}
return newPayment;
}
我的代码正确吗?
您不应该调用这些方法,而应该在 Update Payment 中使用像这样的块实现实际逻辑:
using( var transaction = .....)
{
var payment = m_staffContext.Payments.Get(paymentId);
m_staffContext.Payments.Remove(payment);
m_staffContext.Payments.Add(payment);
m_staffContext.SaveChanges();
}
但是你为什么不只更新现有的付款。
在 AddNewPayment 方法中,Payment 对象的初始化可以在单独的方法中完成以供重用。
如果这不能解决您的问题,请发表评论。
在我的应用程序中,我有两种方法:GetPaymentToDate
和 RemovePayment
:
public Payment RemovePayment(int paymentId)
{
Payment payment;
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
{
//some staff
m_staffContext.SaveChanges();
transaction.Complete();
}
return payment;
}
public Payment GetPaymentToDate(DateTime paymentDate)
{
var payment = new Payment
{
//initialize properties
};
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
{
m_staffContext.Payments.Add(payment);
m_staffContext.SaveChanges();
transaction.Complete();
}
return payment;
}
不,我需要实施 Update 方法。此方法的逻辑是删除旧付款,然后创建新付款。所以我想在一个父事务范围内执行此操作,如果另一个事务失败则角色返回嵌套事务。我将从现有方法中删除 TransactionScopeOption.RequiresNew
选项,并在更新方法中编写如下内容:
public Payment UpdatePayment(int paymentId)
{
Payment newPayment;
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
{
var removedPayment = RemovePayment(paymentId);
var newPayment = GetPaymentToDate(removedPayment.Date);
m_staffContext.SaveChanges();
transaction.Complete();
}
return newPayment;
}
我的代码正确吗?
您不应该调用这些方法,而应该在 Update Payment 中使用像这样的块实现实际逻辑:
using( var transaction = .....)
{
var payment = m_staffContext.Payments.Get(paymentId);
m_staffContext.Payments.Remove(payment);
m_staffContext.Payments.Add(payment);
m_staffContext.SaveChanges();
}
但是你为什么不只更新现有的付款。 在 AddNewPayment 方法中,Payment 对象的初始化可以在单独的方法中完成以供重用。
如果这不能解决您的问题,请发表评论。