1 year ago
#382717
Maki
Entity Manager read old values even if locking with PESSIMISTIC_WRITE
I'm in the following scenario. Handling message inside an MDB, container managed transaction. MDB implements MessageListenerer interface so when a message is received it gets invoked (onMessage method). The message contains an object with an amount. I need to sum that amount to the one inside the database. So inside the onMessage method I invoke my business logic that query the database by doing :
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
MyObject myObject = em.find(MyObject.class, key, LockModeType.PESSIMISTIC_WRITE);
em.refresh(myObject, LockModeType.PESSIMISTIC_WRITE);
As far as I know the second row (em.joinTransaction()) should not be needed (since the persistence context is created inside the JTA transaction....anyway this is not working because when I run a load test I get the wrong sum. The application is deployed on weblogic 12. By logging "myObject" I can see that different thread sometimes find to have the same value while I was expecting the PESSIMISTIC_WRITE, to wait read outside of the transaction.
The action I'm performing on "myObject" is retrieving an integer, make a sum with an input value and then update the entity on the DB. What I find is that the sum has not the expected value (i.e. the sum of the values inside the inbound jms messages). Maybe the transaction is somehow shared among several thread? IMHO it makes no sense for an APP server to do that and, as far as I know, this is not the default behaviour of the weblogic JTA transaction manager.
EDIT: The database is oracle Enterprise Edition
java
jpa
weblogic
java-ee-7
jta
0 Answers
Your Answer