0 Replies Latest reply on May 16, 2019 4:32 AM by kdoctor

    Problems with using multiple EntityManagers in a service method


      Hi everyone,


      I have a service which has an EntityManager reference to talk to an Oracle database. Recently we wanted to re-direct reads to a separate Oracle instance and writes to our original Oracle database.


      For that purposes, I created a separate <persistence-unit name="readOnly"> in our persistence.xml file and autowired a second EntityManager in our service class using @PersisternceContext(unitName = "readOnly").


      Now, wherever we have a read call such as getUser(..), we use the entityManager that points to the readOnly db and if we have a db write operation, we use the entityManager that points to the original database.

      (The 2 dbs are synced near real time btw). All service methods have the @Transactional annotation.


      The problem arises when a caller method (that also has a @Transactional annotation) calls a service.saveUser(...) followed by service.getUser(..) in the same Transaction.


      The getUser(..) call returns null for some reason, when in fact it should return the newly created user instance.


      Any help/ideas would be appreciated. Thanks.