分布式tx失败时如何检索错误代码和消息?(How to retrieve error code and message when distributed tx fails? (MS DTC))
我们有一个存储过程,它通过具有不同MS SQL 2008数据库的链接服务器启动分布式事务。
我们用
SET XACT_ABORT ON;
并且
BEGIN TRY / CATCH blocks
在事务周围捕获任何错误并将错误代码和消息返回给调用客户端。
但是,当分布式事务中的命令失败时,似乎MS DTC正在接管控制,并且我们的catch块无法“正常”回滚并返回错误消息等。而是引发错误:Microsoft分布式事务处理协调器(MS DTC)取消了分发的交易。 (错误1206)。
是否有任何方法可以通过catch块捕获这样的分布式tx错误?
We have got a stored procedure that starts a distributed transaction via a linked server with different MS SQL 2008 databases.
We use
SET XACT_ABORT ON;
and also
BEGIN TRY / CATCH blocks
around the transaction to catch any errors and return the error code & message back to the calling client.
However, when a command inside the distributed transaction fails, it seems that the MS DTC is taking over control and our catch block can't rollback "gracefully" and return the error message etc. Instead an error is raised: The Microsoft Distributed Transaction Coordinator (MS DTC) has cancelled the distributed transaction. (Error 1206).
Is there any way that such a distributed tx error is caught by a catch block?
原文:https://stackoverflow.com/questions/13802947
最满意答案
这更多是一个学说2的事情。
假设您使用注释进行教学映射,则必须声明您在实体中使用的存储库类:
/** * @ORM\Entity(repositoryClass="Fully\Qualified\Namespace\To\MyRepository") */ class MyEntity { }
如下所述: http : //symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes 。
然后,您可以使用标准类继承对此自定义MyRepository类进行编码。
你可以想像这样的事情:
class OrderedRepository extends EntityRepository { // some extra methods... } class MyRepository extends OrderedRespository {}
最后,如果你想覆盖你的仓库的__constructor,你必须用相同的参数初始化父构造函数:
public function __construct($em, Mapping\ClassMetadata $class) { parent::__construct($em, $class); // some extra stuff }
This is more a Doctrine2 thing.
Assuming you are using annotations for your doctrine mapping, you have to declare which repository class you are using in the Entity:
/** * @ORM\Entity(repositoryClass="Fully\Qualified\Namespace\To\MyRepository") */ class MyEntity { }
as explained here: http://symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes .
Then, you can code this custom MyRepository class, using standard class inheritance.
You could imagine something like that:
class OrderedRepository extends EntityRepository { // some extra methods... } class MyRepository extends OrderedRespository {}
Finally, if you want to override the __constructor of your repository, you have to initailize the parent constructor with the same arguments:
public function __construct($em, Mapping\ClassMetadata $class) { parent::__construct($em, $class); // some extra stuff }
相关问答
更多-
我想你只是忘了在你的实体中注册这个仓库。 您只需在实体配置文件中添加存储库类。 在src / Mypro / symBundle / Resources / config / doctrine / Register.orm.yml中: Mypro\symBundle\Entity\Register: type: entity repositoryClass: Mypro\symBundle\Entity\RegisterRepository 为了以防万一,不要忘记在更改后清除缓存。 如果你 ...
-
你不应该在EntityRepository中使用$ container。 相反,创建模型管理器服务并通过DI注入容器。 You should not use $container in the EntityRepository. Instead, create a Model Manager service and inject the container through DI.
-
扩展一个实体是一条路。 在Doctrine2世界中,他们讨论继承映射。 这里有一个代码示例。 它定义了一个BaseEntity然后将其扩展为创建一个BaseAuditableEntity ,最后还有一个扩展BaseAuditableEntity的User实体。 诀窍是使用@Orm\MappedSuperclass注解。 即使在我的关系图中有三个实体,此继承方案也会创建单个表。 这会将所有属性合并到一个表中。 创建的表格将包含通过关系映射的每个属性,即BaseAuditableEntity和User的属性。 ...
-
getRepository()将实体类作为第一个参数: $results = $this->getDoctrine() ->getRepository('griffin\UserBundle\Entity\User') ->getAdmin(); 注意:快速查看EntityManager类本身总是一个好主意。 如果你这样做,你会看到getRepository()方法签名: /** * Gets the repository for an entity class. * * @para ...
-
正如Jason Roman所说,从1.1升级到1.3解决了这个问题。 在标准扩展的情况下(例如添加已发布的,在DoctrineExtensions中发布) - 使用特征应该可以解决问题。 As Jason Roman said, upgrading from 1.1 to 1.3 solved the issue. In case of standard extensions (e.g. adding publishedAt, publishedBy like in DoctrineExtensions) ...
-
这更多是一个学说2的事情。 假设您使用注释进行教学映射,则必须声明您在实体中使用的存储库类: /** * @ORM\Entity(repositoryClass="Fully\Qualified\Namespace\To\MyRepository") */ class MyEntity { } 如下所述: http : //symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes 。 然后,您可以使用标准类继承对此自定义MyRep ...
-
EntityRepository中有一个名为findBy 。 您可以通过第二个参数传递订单。 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) 还有一个例子: $this->getDoctrine()->getRepository('User')->findBy( array(), array('email' => 'asc') ); 在我看来,重写 ...
-
通过在UserProvider类中使用注入,我能够完成此任务(Symfony 3)。 我在类中设置了一个类变量和setter方法。 另外,我必须在services.yml文件中设置服务。 我会告诉你这两个。 UserProvider类 use Doctrine\ORM\EntityRepository; //make sure to include all other relevant uses!! class WebserviceUserProvider implements UserProviderI ...
-
Florian 在这里解释了如何通过服务创建存储库: my_service: class: Doctrine\Common\Persistence\ObjectRepository factory_service: doctrine # this is an instance of Registry factory_method: getRepository arguments: [ %mytest.entity% ] 您可以添加calls以调用setSession (作为 ...
-
如果您想要一个自定义数据库函数的存储库类,那么这是正确的方法: namespace AppBundle\Entity; use Doctrine\ORM\EntityRepository; class PostRepository extends EntityRepository { public function findAll() { return $this->findBy(array(), array('id' => 'DESC', 'createdAt' ...