One entityManger finds entity , the other does not.
Posted
by
Pitelk
on Stack Overflow
See other posts from Stack Overflow
or by Pitelk
Published on 2010-12-20T17:36:13Z
Indexed on
2010/12/21
11:54 UTC
Read the original article
Hit count: 375
Hi all, I have a very strange behavior in my program. I have 2 classes (class LogIn and CreateGame) where i have injected an EntityManager in each using the annotation
@PersistenceContext(unitName="myUnitPU")
EntityManager entitymanger;
In some point i remove an object called "user" from the database using entitymanger.remove(user) from a method in LogIn class. The business logic is that a user can host and join games ( in the same time) so removing the user all the entries in database about the games the user has created are removed and all the entries showing in which games the user has joined are removed also.
After that, i call another function which checks if the user exists using a method in the LogIn class
entitymanager.find(user)
which surprisingly enough, finds the user. After that I call a method in CreateGame class which tries to find the user by using again
entitymanger.find(user)
the entitymanger in that class fails to find the user (which is the expected result as the user is removed and it's not in the database)
So the question is : Why the entitymanager in one class finds the user (which is wrong) where the other doesn't find it? Does anyone has ever the same problem?
PS : This "bug" occurs when the user has hosted a game which is joined by another user (lets call him Buser) and the Buser has made a game which is joined by the current user.
GAME | HOST | CLIENTS
game1 | user | userB
game2 | userB | user
where in this case by removing the user, the game1 is deleted and the user is removed from game2 so the result is
GAME | HOST | CLIENTS
game2 | userB |
PS2 : The Beans are EJB3.0. The methods are called from a delegate class. The beans in the delegate class are instantiated using the InitialContext.lookup() method. Note that for logging in ,creating , joining games the appropriate delegate class calls the correspondent EJB which does the transactions. In the case of logOut, the delegate calls an EJB to logout the user but becuase other stuff must be done (as said above) this EJB calls other EJB (again using lookup() ) which has methods like removegame(), removeUserFromGame() etc. After those methods are executed the user is then logged out. Maybe it has something to do with the fact the the first entity manager is called by a delegate but the second from inside an EJb and thats why the one entitymanger can see the non-existent user while the other cannot? Also all the methods have TRANSACTIONTYPE.REQUIRED
Thank you in advance
© Stack Overflow or respective owner