File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Entity instance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Reply Bookmark "Entity instance" Watch "Entity instance" New topic
Author

Entity instance

Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
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?

Thanks.....
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Each client (each persistent context will have its own entity instance. As for simultaneous access - this is where versioning comes in.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
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.

Explanation:

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.


SCJP 5 (98%) - SCBCD 5 (98%)
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
thanks for detailed reply.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Entity instance
 
Similar Threads
Entitymanager.refresh doubt
Question about no of objects & references
Hibernate Collection Fetch problem with fixed length CHAR
EntityManager - persist
Objects and refrences