I have two datasources that are equal in their structure but not in their Data. My application has to deal with both of them at the same time. I have controller, servie, dao structure that looks like this.
Controller Modell:
@Controller
public abstract class MyFancyControllerModell{
private MyService service;
public MyFancyControllerModell (MyService service){
this.service = service;
}
@RequestMapping(....)
public void editSomeData(String data){....}
}
Controller implementation:
@Controller
@RequestMapping(...)
public class MyControllerImpl1 extends MyFancyControllerModell{
@Autowired
public MyControllerImpl1(@Qualifier("myServiceInstance1") MyService service){
super(service);
}
}
@Controller
@RequestMapping(...)
public class MyControllerImpl2 extends MyFancyControllerModell{
@Autowired
public MyControllerImpl2(@Qualifier("myServiceInstance2") MyService service){
super(service);
}
}
And the Service:
public class MyService{
private MyDao myDao;
public MyService(MyDao myDao){
this.myDao = myDao;
}
@Transactional
public void editSomeData(String data){...}
}
I create the Beans in my configuration class like this:
private DataSource lookupDataSource(final String jndiName) {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
return dsLookup.getDataSource(jndiName);
}
@Bean
public DataSource dataSource1(){
return lookUpDataSource("dataSource1");
}
@Bean
public DataSource dataSource2(){
return lookUpDataSource("dataSource2");
}
@Bean
public MyService myServiceInstance1(@Qualifier("dataSource1") DataSource dataSource){
return new(MyService(new MyDao(dataSource))));
}
@Bean
public MyService myServiceInstance1(@Qualifier("dataSource1") DataSource dataSource){
return new(MyService(new MyDao(dataSource)));
}
My question is, is it possible to create transactionmanagers for both datasources without the need to declare in the service layer which transactionmanager is used?
I tried creating them as bean just like the services but that did not work.
Check out here the answers for previous quesion related to transaction manager...