24. What is the difference between Context, InitialContext and Session Context? How they are used?
javax.naming.Context is an interface that provides methods for binding a name to an object. It's much like the RMI Naming. bind () method.
25. Why an onMessage call in Message Driven Bean is always a separate transaction?
26. Why are ejbActivate () and ejbPassivate () included for stateless session bean even though they are never required as it is a no conversational bean?
To have a consistent interface, so that there is no different interface that you need to implement for Stateful Session Bean and Stateless Session Bean.
Both Stateless and Stateful Session Bean implement javax.ejb.SessionBean and this would not be possible if stateless session bean is to remove ejbActivate and ejbPassivate from the interface.
27. Static variables in EJB should not be relied upon as they may break in clusters. Why?
Static variables are only ok if they are final. If they are not final, they will break the cluster. What that means is that if you cluster your application server (spread it across several machines) each part of the cluster will run in its own JVM.
Say a method on the EJB is invoked on cluster 1 (we will have two clusters - 1 and 2) that causes value of the static variable to be increased to 101. On the subsequent call to the same EJB from the same client, a cluster 2 may be invoked to handle the request. A value of the static variable in cluster 2 is still 100 because it was not increased yet and therefore your application ceases to be consistent. Therefore, static non-final variables are strongly discouraged in EJBs.
28. If I throw a custom ApplicationException from a business method in Entity bean which is participating in a transaction, would the transaction be rolled back by container?
EJB Transaction is automatically rolled back only when a SystemException (or a subtype of it) is thrown. Your ApplicationExceptions can extend from javax.ejb.EJBException, which is a sub class of RuntimeException.
When an EJBException is encountered the container rolls back the transaction. EJB Specification does not mention anything about Application exceptions being sub-classes of EJBException.
You can tell container to rollback the transaction, by using setRollBackOnly on SessionContext/EJBContext object as per type of bean you are using.
29. Can I map more than one table in a CMP?
No, you cannot map more than one table to a single CMP Entity Bean. CMP has been, in fact designed to map a single table.
30. Can a Session Bean be defined without ejbCreate () method?
The ejbCreate () methods is part of the bean's lifecycle, so, the compiler will not return an error because there is no ejbCreate() method.
However, the J2EE spec is explicit:
1). the home interface of a Stateless Session Bean must have a single create () method with no arguments, while the session bean class must contain exactly one ejbCreate () method, also without arguments.
2) Stateful Session Beans can have arguments (more than one create ( ) method)
31. What is clustering?
Clustering is grouping machines together to transparently provide enterprise services. Clustering is an essential piece to solving the needs for today's large websites.
The client does not know the difference between approaching one server or approaching a cluster of servers.
32. Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?
You can pass the HttpSession as parameter to an EJB method, only if all objects in session are serializable. This has to be consider as "passed-by-value", that means that it's read-only in the EJB. If anything is altered from inside the EJB, it won't be reflected back to the HttpSession of the Servlet Container.
- Javax.naming.InitialContext is a Context and provides implementation for methods available in the Context interface.
- Whereas SessionContext is an EJBContext object that is provided by the EJB container to a SessionBean in order for the SessionBean to access the information and/or services or the container.
- There is EntityContext too which is also and EJBContext object that'll be provided to an EntityBean for the purpose of the EntityBean accessing the container details. In general, the EJBContext (SessionContext and EntityContext), AppletContext and ServletContext help the corresponding Java objects in knowing about its 'context' [environment in which they run], and to access particular information and/or service.Whereas, the javax.naming.Context is for the purpose of 'NAMING' [by the way of referring to] an object.
javax.naming.Context is an interface that provides methods for binding a name to an object. It's much like the RMI Naming. bind () method.
25. Why an onMessage call in Message Driven Bean is always a separate transaction?
- EJB 2.0 specification: "An onMessage call is always a separate transaction, because there is never a transaction in progress when the method is called."
- When a message arrives, it is passed to the Message Driven Bean through the onMessage() method, that is where the business logic goes.
- Since there is no guarantee when the method is called and when the message will be processed, is the container that is responsible of managing the environment, including transactions.
26. Why are ejbActivate () and ejbPassivate () included for stateless session bean even though they are never required as it is a no conversational bean?
To have a consistent interface, so that there is no different interface that you need to implement for Stateful Session Bean and Stateless Session Bean.
Both Stateless and Stateful Session Bean implement javax.ejb.SessionBean and this would not be possible if stateless session bean is to remove ejbActivate and ejbPassivate from the interface.
27. Static variables in EJB should not be relied upon as they may break in clusters. Why?
Static variables are only ok if they are final. If they are not final, they will break the cluster. What that means is that if you cluster your application server (spread it across several machines) each part of the cluster will run in its own JVM.
Say a method on the EJB is invoked on cluster 1 (we will have two clusters - 1 and 2) that causes value of the static variable to be increased to 101. On the subsequent call to the same EJB from the same client, a cluster 2 may be invoked to handle the request. A value of the static variable in cluster 2 is still 100 because it was not increased yet and therefore your application ceases to be consistent. Therefore, static non-final variables are strongly discouraged in EJBs.
28. If I throw a custom ApplicationException from a business method in Entity bean which is participating in a transaction, would the transaction be rolled back by container?
EJB Transaction is automatically rolled back only when a SystemException (or a subtype of it) is thrown. Your ApplicationExceptions can extend from javax.ejb.EJBException, which is a sub class of RuntimeException.
When an EJBException is encountered the container rolls back the transaction. EJB Specification does not mention anything about Application exceptions being sub-classes of EJBException.
You can tell container to rollback the transaction, by using setRollBackOnly on SessionContext/EJBContext object as per type of bean you are using.
29. Can I map more than one table in a CMP?
No, you cannot map more than one table to a single CMP Entity Bean. CMP has been, in fact designed to map a single table.
30. Can a Session Bean be defined without ejbCreate () method?
The ejbCreate () methods is part of the bean's lifecycle, so, the compiler will not return an error because there is no ejbCreate() method.
However, the J2EE spec is explicit:
1). the home interface of a Stateless Session Bean must have a single create () method with no arguments, while the session bean class must contain exactly one ejbCreate () method, also without arguments.
2) Stateful Session Beans can have arguments (more than one create ( ) method)
31. What is clustering?
Clustering is grouping machines together to transparently provide enterprise services. Clustering is an essential piece to solving the needs for today's large websites.
The client does not know the difference between approaching one server or approaching a cluster of servers.
32. Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?
You can pass the HttpSession as parameter to an EJB method, only if all objects in session are serializable. This has to be consider as "passed-by-value", that means that it's read-only in the EJB. If anything is altered from inside the EJB, it won't be reflected back to the HttpSession of the Servlet Container.