TransactionScope 在某些地方有效,但在其他地方无效
在 Windows Server 2003 上使用 ASP.NET 3.5、Linq to SQL、SQL Server 2005。在 XP SP3 上本地运行 VS 2008。
我们需要能够将插入、更新和删除包装在事务中。当我们第一次尝试使用 using(var trans = new TransactionScope()) { ...; 包装代码块时反式.Complete(); }
,我们收到一个适当的异常,告诉我们需要为远程事务启用网络访问。 我们这样做了,事情开始按照我们预期的方式进行。
快进到今天。我们的应用程序中有一个很少使用的部分也接受了 TransactionScope 处理。尽管事务在我们代码库的所有其他部分都能正常工作,但我们今天发现这个很少使用的部分抛出了与以前相同的“网络访问”异常:
这是导致异常的代码:
using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
{
using (var dc = new ChargeXferDataContext())
{
//create 'Line' object and set initial values
Line line = new Line();
line.Unit_Num = UnitId;
line.SubmittedBy = Viewer.Alias();
line.LineSubmittedOn = DateTime.Now;
//get codes to move from checked rows
//iterate rows in current gridview
foreach (GridViewRow row in gv.Rows)
{
//if checked, insert move order
HtmlInputCheckBox cb = (HtmlInputCheckBox)row.FindControl("RowLevelCheckBox");
if (cb.Checked)
{
//1st: get required values
int id = Convert.ToInt32(((TextBox)row.FindControl("fldCodeId")).Text);
int newId = Convert.ToInt32(((DropDownList)row.FindControl("ddlNewId")).SelectedValue);
char newPOA = Convert.ToChar(((DropDownList)row.FindControl("ddlPOA")).SelectedValue);
//2nd: get current diag code from old patient
//######## Exception happens here...
DiagCode code = dc.DiagCodes.SingleOrDefault(c => c.Id == id);
//########
//3rd: add code to emenline object
addCode(line, code, newId, newPOA);
}
}
dc.SubmitChanges();
trans.Complete();
}
}
如果您有任何建议,我们将不胜感激。如果我可以解释更多内容,请告诉我。提前致谢!!
Using ASP.NET 3.5, Linq to SQL, SQL Server 2005 on Windows Server 2003. Running VS 2008 on XP SP3 locally.
We need to be able to wrap inserts, updates, and deletes in a transaction. When we first tried this by wrapping code blocks with using(var trans = new TransactionScope()) { ...; trans.Complete(); }
, we got an appropriate exception telling us we needed to enable network access for remote transactions. We did so and things began to work the way we expected.
Fast-forward to today. There is a little-used part of our app that also received a TransactionScope treatment. Though transactions work properly in all other parts of our codebase, we discovered today that this seldom used piece is throwing the same “Network Access” exception as before:
Here's the code that causes the exception:
using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
{
using (var dc = new ChargeXferDataContext())
{
//create 'Line' object and set initial values
Line line = new Line();
line.Unit_Num = UnitId;
line.SubmittedBy = Viewer.Alias();
line.LineSubmittedOn = DateTime.Now;
//get codes to move from checked rows
//iterate rows in current gridview
foreach (GridViewRow row in gv.Rows)
{
//if checked, insert move order
HtmlInputCheckBox cb = (HtmlInputCheckBox)row.FindControl("RowLevelCheckBox");
if (cb.Checked)
{
//1st: get required values
int id = Convert.ToInt32(((TextBox)row.FindControl("fldCodeId")).Text);
int newId = Convert.ToInt32(((DropDownList)row.FindControl("ddlNewId")).SelectedValue);
char newPOA = Convert.ToChar(((DropDownList)row.FindControl("ddlPOA")).SelectedValue);
//2nd: get current diag code from old patient
//######## Exception happens here...
DiagCode code = dc.DiagCodes.SingleOrDefault(c => c.Id == id);
//########
//3rd: add code to emenline object
addCode(line, code, newId, newPOA);
}
}
dc.SubmitChanges();
trans.Complete();
}
}
If you've got any suggestions, they would be appreciated. Let me know if I can explain something more. Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论