If two or more clients request for an Entity (e.g. 3 users are viewing same item (entity) - item no 1001)
how many instance of that entity will be in JVM (server side),
how 3 different client will act on those (that) instance simaltaniosuly?
Joined: Aug 21, 2008
Each client (each persistent context will have its own entity instance. As for simultaneous access - this is where versioning comes in.
Joined: Apr 27, 2008
If "instance of that entity" means "managed instance", then each persistence context contains at most one instance with the same persistence identity. So the number depends on the number of persistence contexts. To give two examples:
1. Stateless session bean with CMT, using a container-managed entity manager, one business method with transaction attribute Requires_New, calling em.find(1001).
-> the persistence context is transaction scoped. Since each call of the business method starts a new transaction, each processing of the business method also uses its own persistence context and therefore has its own instance of the entity.
Guess the business method is called and processed simultaneously by three clients. Then there are three different managed instances altogether.
2. Same situation as in 1. but this time the method has transaction attribute Required and all three clients use the same transaction context.
This time there will be one managed instance, used by all clients.
a) Generation of trasaction-scoped persistence context is lazy. So, when the first em.find() call is performed, a persistence context is generated and associated with the transaction.
b) When the next bean instance performs em.find(), its entity manager discovers that a pesistence context is already associated with the transaction. Therefore this entity manager uses the associated persistence context (we say "the persistence context is propagated") and find() just returns the instance already managed.
c) When the third bean instance performs em.find() the same happens as in b)
Especially the statement "Each client will have its own entity instance." wouldn't be correct in this generality.