Your Ad Here

Wednesday, October 28, 2009

EJB

1 Q What is EJB?
Enterprise JavaBeans (EJB) technology is the server-side component architecture for the Java 2 Platform, Enterprise Edition (J2EE) platform. EJB technology enables rapid and simplified development of distributed, transactional, secure and portable applications based on Java technology.


2 Q What are the different type of Enterprise JavaBeans ?
There are 3 types of enterprise beans, namely: Session bean, Entity beans and Message driven beans.

3 Q What is Session Bean ?
Session bean represents a single client inside the J2EE server. To access the application deployed in the server the client invokes methods on the session bean. The session bean performs the task shielding the client from the complexity of the business logic.

Session bean components implement the javax.ejb.SessionBean interface. Session beans can act as agents modeling workflow or provide access to special transient business services. Session beans do not normally represent persistent business concepts. A session bean corresponds to a client server session. The session bean is created when a client requests some query on the database and exists as long as the client server session exists.


4 Q What are different types of session bean ?
There are two types of session beans, namely: Stateful and Stateless.


5 Q What is a Stateful Session bean?
Stateful session bean maintain the state of the conversation between the client and itself. When the client invokes a method on the bean the instance variables of the bean may contain a state but only for the duration of the invocation.

A stateful session bean is an enterprise bean (EJB component) that acts as a server-side extension of the client that uses it. The stateful session bean is created by a client and will work for only that client until the client connection is dropped or the bean is explicitly removed. The stateful session bean is EJB component that implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute "stateful". Stateful session beans are called "stateful" because they maintain a conversational state with the client. In other words, they have state or instance fields that can be initialized and changed by the client with each method invocation. The bean can use the conversational state as it process business methods invoked by the client.


6 Q What is stateless session bean ?
Stateless session beans are of equal value for all instances of the bean. This means the container can assign any bean to any client, making it very scalable.

A stateless session bean is an enterprise bean that provides a stateless service to the client. Conceptually, the business methods on a stateless session bean are similar to procedural applications or static methods; there is no instance state, so all the data needed to execute the method is provided by the method arguments. The stateless session bean is an EJB component that implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute "stateless". Stateless session beans are called "stateless" because they do not maintain conversational state specific to a client session. In other words, the instance fields in a stateless session bean do not maintain data relative to a client session. This makes stateless session beans very lightweight and fast, but also limits their behavior. Typically an application requires less number of stateless beans compared to stateful beans.


7 Q What is an Entity Bean?
An entity bean represents a business object in a persistent storage mechanism. An entity bean typically represents a table in a relational database and each instance represents a row in the table. Entity bean differs from session bean by: persistence, shared access, relationship and primary key. T


8 Q What are different types of entity beans?
There are two types of entity beans available. Container Managed Persistence (CMP) , Bean managed persistence (BMP).

9 Q What is CMP (Container Managed Persistence) ?
The term container-managed persistence means that the EJB container handles all database access required by the entity bean. The bean's code contains no database access (SQL) calls. As a result, the bean's code is not tied to a specific persistent storage mechanism (database). Because of this flexibility, even if you redeploy the same entity bean on different J2EE servers that use different databases, you won't need to modify or recompile the bean's code. So, your entity beans are more portable.


10 Q What is BMP (Bean managed persistence) ?
Bean managed persistence (BMP) occurs when the bean manages its persistence. Here the bean will handle all the database access. So the bean's code contains the necessary SQLs calls. So it is not much portable compared to CMP. Because when we are changing the database we need to rewrite the SQL for supporting the new database.


11 Q What is abstract schema ?
In order to generate the data access calls, the container needs information that you provide in the entity bean's abstract schema. It is a part of Deployment Descriptor. It is used to define the bean's persistent fields and relation ships.

12 Q When we should use Entity Bean ?
When the bean represents a business entity, not a procedure. we should use an entity bean. Also when the bean's state must be persistent we should use an entity bean. If the bean instance terminates or if the J2EE server is shut down, the bean's state still exists in persistent storage (a database).


13 Q When to Use Session Beans ?
At any given time, only one client has access to the bean instance. The state of the bean is not persistent, existing only for a short period (perhaps a few hours). The bean implements a web service. Under all the above circumstances we can use session beans.


14 Q When to use Stateful session bean?
The bean's state represents the interaction between the bean and a specific client. The bean needs to hold information about the client across method invocations. The bean mediates between the client and the other components of the application, presenting a simplified view to the client. Under all the above circumstances we can use a stateful session bean.


15 Q When to use a stateless session bean?
The bean's state has no data for a specific client. In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an email that confirms an online order. The bean fetches from a database a set of read-only data that is often used by clients. Such a bean, for example, could retrieve the table rows that represent the products that are on sale this month. Under all the above circumstance we can use a stateless session bean.


16 Q Why Use EJB?
EJB helps in building enterprise applications easily. Developers of EJB needs to focus on business logic only. All other features like transaction, persistence etc will be managed by the container. EJB provides developers architectural independence

17 Q What are the different methods of Entity Bean?
An entity bean consists of 4 type of methods: create methods, finder methods, remove methods and home methods

18 Q What are create methods of Entity Bean?
Create methods are used to create a new instance of a CMP entity bean. The create() method on bean's home interface returns an object of remote. ejbCreate(parameters) methods are used for creating Entity Bean instances according to the parameters specified and to some programmer-defined conditions. We can declare more than one create() methods in home interface, and each of which must have a corresponding ejbCreate() and ejbPostCreate() methods in the bean class. These creation methods are linked at run time, so that when a create() method is invoked on the home interface, the container delegates the invocation to the corresponding ejbCreate() and ejbPostCreate() methods on the bean class.

19 Q What are finder methods of Entity Bean?
Finder methods are used to query for specific entity beans. These are methods declared in home interface and begin with find. There are two kinds of finder methods, single-entity and multi-entity. Single-entity finder methods return a remote object that matches given find request. If no records found, this method throws an ObjectNotFoundException . The multi-entity finder methods return a collection ( Enumeration or Collection type) of entities that match the find request. If no entities are found, finder returns an empty collection.

20 Q What are remove methods of Entity Bean ?

Remove methods allow the client to remove an Entity bean by specifying either Handle or a Primary Key of that Entity Bean.

21 Q What are home methods in Entity Bean?
Home methods are methods that are designed and implemented by a developer according to his/her needs. EJB specification doesn't have any requirements for home methods except they need to throw a RemoteException.

22 Q What are different callback methods in Entity beans?
The bean class implements a set of callback methods that allow the container to notify the events in its life cycle. The call back methods available in Entity Bean are
public void setEntityContext();
public void unsetEntityContext();
public void ejbLoad();
public void ejbStore();
public void ejbActivate();
public void ejbPassivate();
public void ejbRemove();

23 Q What is the use of setEntityContext in Entity bean?
The setEntityContext() method is used to set the EntityContext interface for that bean. The EntityContext contains information about the context under which bean is operating. EntityContext interface gives security information about caller. The EntityContext is set only once in the life time of an entity bean instance

24 Q What is the use of unsetEntityContext in Entity Bean?
The unsetEntityContext() method is called at the end of a bean's life cycle before the instance is unloaded from memory. It is used to dereference EntityContext and to perform any clean up operations if required.

25 Q What are ejbLoad and ejbStore methods of Entity Bean?
ejbLoad method is primarily used for data retrievals. ejbStore is used for updating data. Typically the container invokes ejbLoad before the first business method in a transaction and the ejbStore is invoked at the end of the transaction. ejbStore method will be invoked when we change some values in memory.

26 Q What are ejbActivate and ejbPassivate methods of Entity Bean?
The ejbPassivate() is invoked by the container before the beans is passivated and ejbActivate() is invoked by the container after the bean is activated. Passivation and activation is used to save resources. passivation means, dissociating a bean instance from its EJB object. Activation is the process of associating a bean with EJB object. Stateless session beans are never passivated.

27 Q What is the architecture of EJB?
Every EJB is having three classes. A home interface which acts as a factory of remote objects. A remote object which is used for client interaction and a bean object which contains all the business logic.

28 Q Can an Entity bean have zero create methods?
An entity bean ca have zero or more create methods. If there is no create methods we will not be able insert data to the database. So that

29 Q What is the default transaction attribute in EJB?
The default transaction attribute is 'supports'

30 Q What are the different transaction attributes ?
There are six different transaction attributes available: Not Supported, Required, Supports, RequiresNew, Mandatory, Never.

No comments:

Post a Comment

Your Ad Here
Your Ad Here