我正在尝试找到一种向现有事务添加/获取资源/状态的方法。这在春天可能吗?
我想要实现的类似于下面的伪代码:
@Service
@Transactional("txManager")
public class ServiceImpl implements Service {
@Override
@AddResourceHere
public TestObj doSomething(){
...
}
@Override
@AddResourceHere
public TestObj doSomethingAgain(){
...
}
}
@Aspect
@Component
public class Interceptor {
private static final Logger logger = LogManager.get(Interceptor.class);
@Before("@annotation(my.package.AddResourceHere)")
public void switchDatabase(JoinPoint joinPoint){
MyResource resource = TransactionResouceAdder.getResource("transactionSpecificResource");
if(resource == null){
TransactionResouceAdder.addResource(new MyResource("A new resource"));
...
}
else
log.info("resource has already been added for this transaction");
}
}
public class Test {
...
@Test
@Transactional("txManager")
public void doSomethingTest(){
serviceImpl.doSomething();
serviceImpl.doSomethingAgain();
}
}
我发现了类似的东西
org.springframework.transaction.support.TransactionSynchronizationManager#getResouce(Object)
org.springframework.transaction.support.TransactionSynchronizationManager#bindResource(Object, Object)
但是,这会将资源添加到事务的当前线程。有没有办法使资源事务仅受限制?
在我的实际代码中,我使用 spring jdbc 的 DataSourceTransactionManager 作为事务管理器。
在此先感谢您的帮助:)