• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about ejbcreate()

 
Shanthi Priya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when client invoke create() method in home interface this causes the container to invoke newInstance() on bean class to create new bean instance, next container calls setSessionContext() followed by ejbcreate().
correct me if i am wrong!

My question is
Q) can container call bean class newInstance() method from the home interface?
Q) whats purpose of setSessionContext() method and what will it return??
Q) If newInstance() method creates new bean instance then why ejbcreate???
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shanthi Priya:
when client invoke create() method in home interface this causes the container to invoke newInstance() on bean class to create new bean instance, next container calls setSessionContext() followed by ejbcreate().
correct me if i am wrong!


Exactly.

Originally posted by Shanthi Priya:

My question is
Q) can container call bean class newInstance() method from the home interface?

I don�t understand exactly your question.
When the container needs to create a new instace of a bean, first it calls bean�s contructor ("new instance()").

Originally posted by Shanthi Priya:

Q) whats purpose of setSessionContext() method and what will it return??

setSessionContext is supposed to save the bean�s context. In this callback, it�s the ONLY time that a bean gets a reference for its context.
A bean�s context has a lot of important methods that you probably will need.

Originally posted by Shanthi Priya:

Q) If newInstance() method creates new bean instance then why ejbcreate???

Bear in mind that when the container calls the bean�s contructor
("new instance()") the object created is just an ordinary object. It�s NOT already a bean. Then the container actualy create a bean (creates a context and other things) and then it calls ejbCreate to finish the creating of the bean. In this method you put things that a bean can do, like accesses database, JNDI context and so on.

did you get it ?
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shanthi Priya:
when client invoke create() method in home interface this causes the container to invoke newInstance() on bean class to create new bean instance, next container calls setSessionContext() followed by ejbcreate().
correct me if i am wrong!


This is only valid for Session Beans. For entity beans, the container creates a bean instance by invoking the constructor and then setEntityContext. With entity beans, the ejbCreate method is invoked by the Container before creating a new entity in the underlying persistent storage (read: new row in the database).

When the container invokes the constructor and setEntityContext, the entity bean passes from the 'Does not exist' state to the 'pooled' state. This is different from what happens with Session Beans. With these, a 'bean' (and not simply the object) is created when the Container invokes, in order: constructor, setSessionContext(..), ejbCreate. After this method, the session bean will be in the method-ready state.

[QUOTE[
My question is
Q) can container call bean class newInstance() method from the home interface?
Q) whats purpose of setSessionContext() method and what will it return??
Q) If newInstance() method creates new bean instance then why ejbcreate???

1) I think the container uses reflection to invoke the bean's constructor. The interfaces are there mostly for clients and there is no constructor signature (which refers to how to construct a bean instance) in the home interface

The other two questions have been answered exhaustively by Vinicius.
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear in mind that when the container calls the bean�s contructor
("new instance()") the object created is just an ordinary object. It�s NOT already a bean. Then the container actualy create a bean (creates a context and other things) and then it calls ejbCreate to finish the creating of the bean


I wonder how the container implements this. All ordinary objects are the same since they are created by invoking the empty constructor. There are many create() methods, each of which takes different parameters. After container invoking ejbCreate(String s), it creates bean instance A. And after the container invoking ejbCreate(String s1, String s2), it creates bean instance B.
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Nguyen:

I wonder how the container implements this. All ordinary objects are the same since they are created by invoking the empty constructor. There are many create() methods, each of which takes different parameters. After container invoking ejbCreate(String s), it creates bean instance A. And after the container invoking ejbCreate(String s1, String s2), it creates bean instance B.


You are talking about stateful session beans here. The container creates the instance before giving the bean its 'beanness'. The order by which the container creates a SFSB is: constructor, setSessionContext, ejbCreate

The container gives actually birth to a bean from an object (meaning that the bean passes from the 'Does not exists state, to the 'method ready state') through the setSessionContext (it tells the container - 'Listen, I'm giving to you something precious, something unique: this is the only 'red phone' you will have during your lifetime to call me if you need, so don't miss it'), and through the ejbCreate (with parameters if necessary), where the container links the EJBObject, the bean's context and the bean instance together. A bean has got some priviliges that an ordinary object hasn't. And those are granted thanks to the context, through which a 'fully qualified bean' can access things like a reference to its EJBObject and EJBHome, query the container about security info related to its client, get a UserTransaction object onto which invoking methods (only for BMT), access its special JNDI environment (from which, it can lookup useful info, like environment entries, resource manager connection factories, etc.), access other bean's methods, do transaction-related things for CMT).

All of those are things that an ordinary object can't do, and this is what you're paying for (the services offered by your EJBContainer provider).
[ August 07, 2004: Message edited by: Marco Tedone ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic