5

项目设置:Spring 3.0.5 / JPA 2 / Hibernate / @Transactional

我们使用几个不同的数据源(因此也使用不同的事务管理器),但有共同的服务基类,因为很多功能都被重用了。

所以我们认为我们可以通过使用自定义注释来解决这个问题。我们在一个抽象基类中定义所有方法,并为每个事务管理器创建一个空的实现类。

现在的问题是:

AbstractFallbackTransactionAttributeSource中,这是正在执行的查找:

TransactionAttribute txAtt = findTransactionAttribute(specificMethod);
// ...
// Second try is the transaction attribute on the target class.
txAtt = findTransactionAttribute(specificMethod.getDeclaringClass());

首先检查方法,然后检查声明该方法的类(及其祖先)。但是在我们的上下文中,自定义注解位于一个子类中,不能通过向上找到。

那么我们该如何解决这个问题呢?

【我在Spring社区论坛也问过这个问题】

4

2 回答 2

4

很棒的收获。我不知道一个好的解决方案,但作为一种解决方法,您可以在实现中覆盖有问题的方法,如下所示:

@Override
void method(...)
    // Just to work around a bug in AbstractFallbackTransactionAttributeSource
    super.method(...);
}

这将使@Transactional可见但很难看:-/

于 2011-05-17T14:30:24.717 回答
1

For the time being, we solved this issue by providing

  1. our own subclass of AnnotationTransactionAttributeSource which does a lookup on the target class first and then delegates to the super class
  2. A BeanFactoryPostProcessor that substitues our implementation for the original AnnotationTransactionAttributeSource bean definition that is created internally by <tx:annotation-driven>

.

于 2011-05-17T15:54:29.317 回答