Since the EntityManagerFactory is shared across EntityManagers, if you are calling setDataSource() on the EMF, then you could have a potential threading issue. The scenario below explains it.
Thread 1 --> Begin transaction 1
Thread 2 --> Begin transaction 2
Thread 1 --> emf.setDataSource(Client1)
Thread 2 --> emf.setDataSource(Client2)
Thread 2 --> em2.persist(entity2)
Thread 2 --> Commit transaction 2
Thread 1 --> em1.persist(entity1) // Remember, the last set datasource was client 2 datasource set on EMF by thread 2.
Thread 1 --> Commit transaction 1 // Will this not try to save the data through datasource of client 2.
Be sure entity1 is persisted to Client1 (and not Client2) datasource !!
Is the above understanding clear and sane? Do we not see a potential issue as I anticipate.
Any help will be appreciated.


Reply With Quote