0
我想找到一種方法來添加/獲取資源/狀態到現有的交易。這在春季可能嗎?春季:添加交易特定資源
我試圖做到的,是類似於下面的僞代碼:
@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作爲事務管理器。提前任何幫助:)
感謝您的信息!實際上,在深入瞭解源代碼之後,我發現我使用TransactionSynchronizationManager的方式並不完整。我錯過的重要一步是資源與當前活動事務的同步。這一步涉及延伸/使用以下: ** ** org.springframework.transaction.support.ResourceHolder ** ** org.springframework.transaction.support.ResourceHolderSupport ** org.springframework.transaction.support.ResourceHolderSynchronization ** – jespeno