在事务中启动新进程

时间:2021-11-18 00:11:12

I have to create a user in a third party application that stores its information in SQL Server, and I also store information about this user in our application database, also in SQL Server.

我必须在第三方应用程序中创建用户,将其信息存储在SQL Server中,我还将有关此用户的信息存储在我们的应用程序数据库中,也存储在SQL Server中。

I was hoping to wrap both updates in a TransactionScope, so they can succeed or rollback together. Unfortunately the only method I have for creating users is via a command-line utility, that I am currently running through a Process.Start call. I was hoping the new process might still enlist in my active transaction, but this does not seem to be the case.

我希望在TransactionScope中包装这两个更新,以便它们可以成功或一起回滚。不幸的是,我创建用户的唯一方法是通过命令行实用程序,我目前正在通过Process.Start调用运行。我希望新进程可能仍然存在于我的活动事务中,但似乎并非如此。

As it happens, both datbases are on the same server machine, but are under different instances. My application is written in C# for .NET 3.5

实际上,两个数据库都在同一台服务器上,但是在不同的实例下。我的应用程序是用C#for .NET 3.5编写的

Am I doing something wrong, or is there some explicit way to get this process to enlist in the transaction, or will I just have to manually rollback the changes?

我做错了什么,或者是否有一些明确的方法让这个过程在事务中登记,或者我只需要手动回滚更改?

2 个解决方案

#1


Enlist command line app in distributed transaction - don't think this can be done.

在分布式事务中登记命令行应用程序 - 不要认为可以这样做。

#2


To answer your question: They can't automatically go into the same transaction scope. Try to create the user first, that way, if it fails you don't even create the records in your db.

回答你的问题:他们不能自动进入同一个交易范围。尝试先创建用户,这样,如果失败,您甚至不会在数据库中创建记录。

If your Command Line process that you use to create the user returns an error code, (the int in 'public static int Main()' check that and make that part of the result of your transaction. 0 means success, everything else usually means failure.

如果用于创建用户的命令行进程返回错误代码,('public static int Main()'中的int检查并将该部分作为事务的结果.0表示成功,其他一切通常意味着失败。

If it doesn't create an error code. but some text on STDERR, override the STDERR stream, read it's output and check that way.

如果它没有创建错误代码。但是STDERR上的一些文本会覆盖STDERR流,读取它的输出并检查这种方式。

#1


Enlist command line app in distributed transaction - don't think this can be done.

在分布式事务中登记命令行应用程序 - 不要认为可以这样做。

#2


To answer your question: They can't automatically go into the same transaction scope. Try to create the user first, that way, if it fails you don't even create the records in your db.

回答你的问题:他们不能自动进入同一个交易范围。尝试先创建用户,这样,如果失败,您甚至不会在数据库中创建记录。

If your Command Line process that you use to create the user returns an error code, (the int in 'public static int Main()' check that and make that part of the result of your transaction. 0 means success, everything else usually means failure.

如果用于创建用户的命令行进程返回错误代码,('public static int Main()'中的int检查并将该部分作为事务的结果.0表示成功,其他一切通常意味着失败。

If it doesn't create an error code. but some text on STDERR, override the STDERR stream, read it's output and check that way.

如果它没有创建错误代码。但是STDERR上的一些文本会覆盖STDERR流,读取它的输出并检查这种方式。