This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know who is calling when a business method is called for a stateless bean?  RSS feed

 
Rakesh Kedia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
As we know that in stateless session bean, the beans are created and stored in a pool. During that process the newInstance(), setSessionContext() and ejbCreate() is called. These methods are called once in the bean's life cycle. We have the opportunity to cache the session context in setSessionContext() method. When the setSessionContext() is called, that time we don�t have any user information because stateless session bean is not associated with any client.

My real question is, I don�t understand or didn�t get a chance to read anywhere that what should ctx.getCallerPrincipal() will return when the user calls any business methods on stateless session bean? Is the container updates the session context each time a business method is called? If yes, how and when it updates the session context?

Thank you in advance.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it updates the caller information. So, when there is a valid caller, there is valid caller information.
 
Rakesh Kedia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nathaniel.
I have one more question to ask everyone. If I declare a class variable in a stateless bean, is a single instance of that variable is created for all the stateless bean instances? In order words, take an example that I declared a class variable like

Integer i = new Integer(10);

Is this variable is shared among all the stateless session bean instances? What is the impact if I declare the variable as a static?

Thank you in advance.
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh,


Is this variable is shared among all the stateless session bean instances?


No, ejb instance variables are not shared bw multiple bean instances. Such variable will be only associated with one particular bean instance. A bean is still a java object with some special capabilities (eg, special jndi context), and therefore usual java rules apply to it.

As you know the danger of having a class level variables (i.e. member variable) in the stateless sb is that if the bean instance changes the value of such variable during one method execution (i.e. during servicing client X) then when the same bean instance executes a method again (i.e. servicing a client Y) the instance variable will have a value that has been assigned to it during servicing client X. This is a danger

What is the impact if I declare the variable as a static?


All beans instances of this bean type will share this variable. In other words if one bean instance changes static variable all bean instances will see the change. Static variables are associated with the class not the actual class instances.

EJB 2.0 spec explicitly recommends that static fields are only to be used as read only fields (i.e. as constants).


An enterprise Bean must not use read/write static fields. Using read-only static fields is
allowed. Therefore, it is recommended that all static fields in the enterprise bean class be
declared as final.
This rule is required to ensure consistent runtime semantics because while some EJB Containers may
use a single JVM to execute all enterprise bean�s instances, others may distribute the instances across
multiple JVMs.


Therefore, the bean class should contain static member variables declared as final:



Hope it helps.
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathaniel Stoddard:
Yes it updates the caller information. So, when there is a valid caller, there is valid caller information.


What i usually do is store SessionContext as instance variable (This was what i read in Ed Roman). Now what i believe is that in stateless session bean, multiple threads may be executed on one bean instance (like servlets). So that one instance variable will be shared among many clients. So calling ctx.getCallerPrincipal() should give unexpected results, or am i missing something here?
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh,

Stateless session bean can service multiple clients but never at the same time. No concurrent access is allowed on session beans. Therefore, there is no danger that an instance variable will be accessed by more than one thread at any given time.

Session context is a session bean's access to the container environment. It is a good practice to store session context in an instance variable when setSessionContext( SessionContext ) method is called by the container. When a client executes a business method on a bean ejb container (and therefore, session context) knows client security info. Bean can call SessionContext#getCallerPrincipal() method and be 100% sure that it returns an info on the client who has invoked a method (no other client can invoke a method on the bean while it is executing the current method).

Hope it helps.
[ June 22, 2004: Message edited by: Alex Sharkoff ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!