简单交易

发布于 2024-09-15 01:25:04 字数 1080 浏览 1 评论 0原文

我有 2 个 linq 2 SQL 语句,我希望处于一个事务中(SQL 服务器是远程的,在防火墙之外等),所有其他通信都可以工作,但是当我将这 2 个语句包装在 TransactionScope() 中时,我开始必须配置 MSDTC我们做到了,但是存在防火墙问题(我认为)有更简单的方法吗?

我想做的事情的基础可以归结为:(两者都是底层的存储过程)

using (var transactionScope = new TransactionScope())
{
    Repository.DataContext.SubmitChanges();
    Repository.DataContext.spDoFinalStuff(tempID, ref finalId);
    transactionScope.Complete();
}

实现此目的最简单的方法是什么?

编辑:
首先我得到这个:事务管理器已禁用其对远程/网络事务的支持。 (HRESULT 异常:0x8004D024) 在我们的服务器上,我按照此处的说明进行操作 来纠正这个问题。但是,这些说明似乎不适用于 Windows 7(我的开发盒),请参阅我对上述答案的评论。

更正问题后(在非 win7 机器上),我得到以下信息: 事务已被隐式或显式提交或中止(HRESULT 的异常:0x8004D00E),其中一些谷歌搜索 建议可能是防火墙问题。

编辑
我刚刚发现远程数据库是 SQL 2000

I have 2 linq 2 SQL statements I'd like to be in a transaction (the SQL server is remote, outside firewalls etc) all other communication works but when I wrap these 2 statements in a TransactionScope() I begin having to configure MSDTC which we did, but then there are firewall issues (I think) is there a simpler way?

the basics of what I want to do boil down to this: (both are stored procs under the hood)

using (var transactionScope = new TransactionScope())
{
    Repository.DataContext.SubmitChanges();
    Repository.DataContext.spDoFinalStuff(tempID, ref finalId);
    transactionScope.Complete();
}

What is the simplest way to achieve this?

EDIT:
first I got this: The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)
On our servers I followed the instructions here to correct this. However the instructions don't seem to apply to windows 7 (my dev box) see my comment on above answer.

after correcting the issue (on the non win7 boxes) I get this: The transaction has already been implicitly or explicitly committed or aborted (Exception from HRESULT: 0x8004D00E) which some googling suggested may be firewall issue.

EDIT
I just discovered the remote DB is SQL 2000

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

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

发布评论

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

评论(4

紙鸢 2024-09-22 01:25:06

当您在事务期间只需要处理一个数据库时,您可以简单地创建并打开一个新的SqlConnection。这可以防止您需要使用 TransactionScope。这是一个示例:

using (var con = new SqlConnection("constr"))
{
    con.Open();
    using (var tran = con.BeginTransaction())
    {
        using (var context = new YourDataContext(con))
        {
            // Do stuff
            context.SubmitChanges();
            int generatedId = /* get this id */
            // Do stuff with id

            context.SubmitChanges();
        }
    }
}

因为您使用Repository,所以您必须在后台创建上下文,但想法是相同的。并且不要忘记处理数据库事务和连接。

When you only need to handle one single database during the transaction, you can simply create and open a new SqlConnection. This prevents you from needing to use a TransactionScope. Here is an example:

using (var con = new SqlConnection("constr"))
{
    con.Open();
    using (var tran = con.BeginTransaction())
    {
        using (var context = new YourDataContext(con))
        {
            // Do stuff
            context.SubmitChanges();
            int generatedId = /* get this id */
            // Do stuff with id

            context.SubmitChanges();
        }
    }
}

Because you use a Repository, you will have to create the context on the background, but the idea is the same. And don't forget to dispose the database transaction and connection.

写下不归期 2024-09-22 01:25:06

如果您不想搞乱 SQL,那么 TransactionScope 就是您的最佳选择。我不太熟悉通过防火墙进行交易并遇到任何问题。您能发布您遇到的异常/错误吗?

如果它确实给您带来了问题,您可以创建一个存储过程来包装这些问题,并在包装​​的存储过程中执行事务。

TransactionScope is the way to go if you don't want to mess around with SQL. I am not to familiar with doing transactions over a firewall and having any issues though. Can you post what exceptions / errors you are encountering?

If it is really causing you problems you could make a stored procedure to wrap those and do the transaction inside the wrapped stored procedure.

诗酒趁年少 2024-09-22 01:25:05

如果 2 个更新发送到 2 个不同的数据库,.net transactoinScope 类需要 MSDTC 的帮助来协调事务。 SQL Server 2005 或更高版本,如果两个更新位于同一数据库内,则不涉及 MSDTC。

MSDTC在com+组件服务中配置,选择您的计算机名称,然后选择属性,基本上您应该选择“无身份验证”。

以下链接可能会有所帮助

http://support.microsoft.com/kb/306843

http: //blogs.msdn.com/b/distributedservices/archive/2008/11/12/troubleshooting-msdtc-issues-with-the-dtcping-tool.aspx

if 2 updates are sent to 2 different database, .net transactoinScope class need MSDTC's help to coordinate the transactions. SQL server 2005 or later, if two update are within the same database, no MSDTC are involved.

MSDTC are configured in com+ component service, choose your computer name, and select properties, basically you should select No authentication.

the following link might help

http://support.microsoft.com/kb/306843

http://blogs.msdn.com/b/distributedservices/archive/2008/11/12/troubleshooting-msdtc-issues-with-the-dtcping-tool.aspx

新人笑 2024-09-22 01:25:05

您总是可以创建一个新的存储过程来在其自己的事务中执行这两个 SP?又快又脏……

You could always create a new stored procedure that executes these two SPs within its own transaction? Quick n' dirty...

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