简体   繁体   中英

change entitymanager datasource in same session

in my session i need to call 2 diffrent procedures which are on db1 and db2. it works fine when i call them separetely but it fails when i call them in same session. it fetches first data correctly but it fails on second call since it looks for the second procedure on db1 although entitiymanager's datasource changed correctly.

what am i missing?

here is code snippet

@Repository
@Transactional
public class DB1Dao {

    @PersistenceContext()
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    public Model1 getData(String param1) {
    .....
    }
}

@Repository
@Transactional
public class DB2Dao {

    @PersistenceContext()
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    public Model2 getData(String param2) {
    .....
    }
}


@Autowired 
private DB1Dao dao1;

@Autowired 
private DB2Dao dao2;

@RequestMapping(value = "/inquiry", method = RequestMethod.POST)
public ResponseEntity<Object> inquiryService(@RequestBody InquiryRequest inquiryRequest){   
   ....
   Model1 model1 = dao1.getData(param2);   // success

  ....
  Model2 model 2 = dao2.getData(param2);  // fails since it looks for second procedure on db1
}   

You didn't show any persistence-related configuration info, but I'd guess you should differentiate your repositories by properly naming them:

@PersistenceContext(unitName = "db1PersitenceUnitName")
        private EntityManager entityManager;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM