I use multiple datasources in my spring project. I enable/disable them by manual config in project startup time. At a time all of them may be active So a transactionManager bean maybe active or not.
I implement it by @Conditional annotation in spring configuration class. When I use a disable transactional annotation on methods I have NoSuchBeanDefinitionException.
When I define transactionManager bean conditionally how to use the transactional annotioan on methods?
The archiveTransactionManager bean doesn't create by @Conditional annotation and I want spring skip checking for bean validation of conditional transaction manager.
For conditional sessionFactory beans I set 'required' parameter in Autowired annotation to false for prevent spring to throw NoSuchBeanDefinitionException but what do I do for @Transactional ?
Configuration class
@Bean("archiveTransactionManager")
@Conditional(ArchiveCondition.class)
public HibernateTransactionManager archiveTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(archiveSessionFactory());
return transactionManager;
}
Transactional method
@Transactional(value = "archiveTransactionManager", readOnly = true)
private List<DocumentItem> loadArchivedDocumentItem() {...}
Usage
if(GeneralSetting.isArchive)
documentService.loadArchivedDocumentItem();
Current result:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'archiveTransactionManager' available: No matching PlatformTransactionManager bean found for qualifier 'archiveTransactionManager' - neither qualifier match nor bean name match!
I want spring skip bean validation of conditional transactionManager beans on some situations that they don't create by conditions.
@Transactional
and write your transactional expressions explicitly in java and make thos conditional as well. Using a no-op transaction manager is probably the easiests. However why if there is no tx-manager and no -session-factory why instantiate this bean? Shouldn't simply the whole module/part of the application should be ignored. – M. Deinum