@EnableTransactionManagement annotation with 2 transaction managers
In your configuration class, use @EnableTransactionManagement
annotation.
Define a transaction manager in this class as:
@Bean(name="txName") public HibernateTransactionManager txName() throws IOException{ HibernateTransactionManager txName= new HibernateTransactionManager(); txName.setSessionFactory(...); txName.setDataSource(...); return txName; }
There on, in your class/method that executes transactional job(s), annotate as follows:
@Transactional("txName")
or
@Transactional(value = "txName")
This is how you would tie a name qualified transaction manager to wherever you need it. You can now have as many transaction managers as you want and use it accordingly wherever you need.
Just in case anyone runs into this problem, I found a solution:
@Configuration@EnableTransactionManagement@DependsOn("myTxManager")@ImportResource("classpath:applicationContext.xml")public class AppConfig implements TransactionManagementConfigurer {@Autowiredprivate PlatformTransactionManager myTxManager;...@Overridepublic PlatformTransactionManager annotationDrivenTransactionManager() { return this.myTxManager;}
In this way, you can use a specific txManager defined in an xml configuration.
In case you want to define the txManager used on service-level, you shall remove the @EnableTransactionManagement
annotation from the @Configuration
class and specify the txManager in the @Transactional
annotations, e.g.
@Service@Transactional(value="myTxManager", readOnly = true)public class MyServiceImpl implements MyService { ... }
From the java doc
For those that wish to establish a more direct relationship between
@EnableTransactionManagement
and the exact transaction manager bean to be used, theTransactionManagementConfigurer
callback interface may be implemented - notice the implements clause and the@Override
-annotated method below:
Your @Configuration
class needs to implement TransactionManagementConfigurer
interface - implement the annotationDrivenTransactionManager
which will return the reference to the transactionManager
that should be used.