java - Different txManager with same DAO, want inject Connection of ConnectionHolder into persistence -


i have application copy data 1 db other db. on both db's same db schema installed. when copy data lot of business logic needed. have typical, traditional implementation service, dao , custom persistence layer (no entity manager).

first start define 2 datasources used 2 different transaction managers.

@configuration @profile("test") public class testenvironment {       /**      * configuration database datasource 1      *       * @return      */     @bean(name = { "datasource1"})     public datasource basicdatasource1() {             ...     }      @bean     public datasourcetransactionmanager txmanagerdb1() {         datasourcetransactionmanager datasourcetransactionmanager = new datasourcetransactionmanager();         datasourcetransactionmanager.setdatasource(basicdatasource1());          return datasourcetransactionmanager;     }      /**      * configuration database datasource 2      *       * @return      */     @bean(name = { "datasource2"})     public datasource basicdatasource2() {             ...     }      @bean     public datasourcetransactionmanager txmanagerdb2() {         datasourcetransactionmanager datasourcetransactionmanager = new datasourcetransactionmanager();         datasourcetransactionmanager.setdatasource(basicdatasource2());          return datasourcetransactionmanager;     }  } 

then implement service , inject via @autowired test dao. implement 2 methods using same dao (reentrant). 2 methods in service annotate @transactional , set respectively txmanager want use.

@service @primary public class mytestserviceimpl extends abstracthandler implements mytestservice {      @autowired     private txmanagertestdao txmanagertestdao;      @transactional("txmanagerdb1")     public string calltodb1() {         txmanagertestdao.settransactionname("txmanagerdb1");         return txmanagertestdao.loadtestbean().getteststring();     }      /*      * return db connection type      */     @transactional("txmanagerdb2")     public string calltodb2() {         txmanagertestdao.settransactionname("txmanagerdb2");         return txmanagertestdao.loadtestbean().getteststring();     }  } 

the line txmanagertestdao.settransactionname("txmanagerdb1") or txmanagertestdao.settransactionname("txmanagerdb2") bad design want remove. because, need know in dao layer transaction manager want use set active connection persistence layer.

my dao looks following:

@repository public class txmanagertestdaoimpl implements txmanagertestdao {      private string transactionname;      @autowired     private autowirecapablebeanfactory autowirebeanfactory;      public testbean loadtestbean() {          datasourcetransactionmanager mytransaction = (datasourcetransactionmanager) autowirebeanfactory.getbean(transactionname);         connectionholder conholder = (connectionholder) transactionsynchronizationmanager.getresource(mytransaction.getdatasource());          //conholder.getconnection() know correct connection have use due @transactional in servide layer.         //how can generic way active transaction manager?          testbean testbean = new testbean();         testbean.setteststring("mytransaction: '" + mytransaction + "' connection: '" + conholder.getconnection() + "'");          return testbean;     }      @override     public void settransactionname(string transactionname) {         this.transactionname = transactionname;     } }    

i want set active txmanager via @transactional("...") did in service layer. not use in future txmanagertestdao.settransactionname("txmanagerdb2") in redundant way. solution works perfect, not perfect implement our special use case.

is there generic way found out in method txmanagertestdaoimpl.loadtestbean() if txmanagerdb1 or txmanagerdb2 active? result need active connectionholder object of active transaction. inject connection persistence, classes runs in same transaction. dao , persistence layer should neutral. our persistence datamapper from/to sql. want/can not use jpa or similar framework.

thanks


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -