EJB Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. What are the different types of EJB?
Mainly three types of EJB:
- Entity Bean
- Session Bean
- Message Driven Bean(MDB)
(1) Entity Bean: It represents an entity which is mapped with database or we can say it makes OR object Relational mapping with Database. Entity bean typically represent table in RDBMS and each instance represent row in the table.
Two types of entity bean:
- CMP Entity bean: Container managed entity bean its responsibility of container to manage the bean persistence behavior.
- BMP Entity bean: Programmer manage the bean persistence behavior.
Types of session bean:
- Stateless session bean: When there is not need to maintain state of a particular client stateless session bean is used .They alive for short period of time. For example if we are validating the credit card we can use stateless session bean.
- Stateful session bean: Stateful session bean maintain the conversational state of client over the series of method call before the bean instance goes to passive state conversational state is saved to persistence area like Hard disk and again when same client send a request and bean instance come into the active state it will come out from hard disk to main memory. For Example when we do online banking transaction ,online reservation we use stateful session bean.
Ques 2. Explain the life cycle method of EJB?
Life Cycle of Entity Bean:
- First stage is Does Not Exist Stage then Container creates the instance of EJB and call SetEntityContext() method which will set all entity context to bean and now it will become available on pool, to get a particular identity of an EJB object it has to move from Pooled stage to ready stage which is done by calling the create() method which in turns call ejbCreate() and ejbPostCreate() method.
- There is another way by which directly entity bean can move to pooled stage to ready stage that's is call ejbActivate() method. Now we are ready to invoke the business method of entity bean .After completion of business method if we want to move again in pooled stage from ready stage we can call remove() method which in turns call ejbRemove() or directly call ejbPassivate() method.
- At the end container remove the instance of EJBfrom pool and call unSetEntityContext().
- Stateful session beans life cycle starts when client call create() method.The container create the instance of session bean and call setSessionContext() and ejbCreate() method.
- Now the stateful session bean is ready to serve the client request after serving the request if it is not used after a long time container can move this bean to passive stage by calling the ejbPassivate() method.similarly when bean is in passive stage and client invoke the business method the container call ejbActivate() method to move bean from passive stage to active or ready stage.
- At the end of life cycle client call remove() method and container will call ejbRemove() method and bean is ready for garbage collection.
- Stateless session bean has short life cycle it can have two stage does not exist and ready stage. ejb container create the instance of stateless session bean and call setSessionContext() and ejbCreate() method.Now the bean is ready to invoke business method on this.it will not maintain the state so remove () method is been called after completion of business method which in turns call ejbRemove() and now its ready for garbage collection.
- MDBs have same life cycle like stateless session bean. setMessageDrivenContext() method and ejbCreate() method is called by container to create the instance of MDB.now its ready to receive message and at the end of lifecycle client will call remove() method which in turns call ejbRemove() and now its ready for garbage collection.
Ques 3. What are the two important TCP Socket classes?
Socket and ServerSocket.
ServerSocket is used for normal two-way socket communication. Socket class allows us to read and write through the sockets. getInputStream() and getOutputStream() are the two methods available in Socket class.
Ques 4. What is Ripple Effect?
The process of propagating the changes in the properties of a server group during runtime to all the associated clones is called Ripple Effect.
Ques 5. What is a Clone?
The copies of a server group are called Clones. But unlike a Server Group Clones are associated with a node and are real server process running in that node.
Ques 6. What’s new in the EJB 2.0 specification?
Following are some of the main features supported in EJB 2.0:
1. Integration of EJB with JMS,
2. Message Driven Beans,
3. Implement additional Business methods in Home interface which are not specific for bean instance, EJB QL.
Ques 7. How can I access EJB from ASP?
We can use the Java 2 Platform, Enterprise Edition Client Access Services (J2EETM CAS) COM Bridge 1.0, currently downloadable from Sun.
Ques 8. What is the relationship between local interfaces and container-managed relationships?
Entity beans that have container-managed relationships with other entity beans, must be accessed in the same local scope as those related beans, and therefore typically provide a local client view. In order to be the target of a container-managed relationship, an entity bean with container-managed persistence must provide a local interface.
Ques 9. What is the difference between session and entity beans?
An entity bean represents persistent global data from the database; a session bean represents transient user-specific data that will die when the user disconnects (ends his session). Generally, the session beans implement business methods (e.g. Bank.transferFunds) that call entity beans (e.g. Account.deposit, Account.withdraw)
Ques 10. What are Local Interfaces? Explain.
EJB was originally designed around remote invocation using the Java Remote Method Invocation (RMI) mechanism, and later extended to support to standard CORBA transport for these calls using RMI/IIOP. This design allowed for maximum flexibility in developing applications without consideration for the deployment scenario, and was a strong feature in support of a goal of component reuse in J2EE. Many developers are using EJBs locally, that is, some or all of their EJB calls are between beans in a single container. With this feedback in mind, the EJB 2.0 expert group has created a local interface mechanism. The local interface may be defined for a bean during development, to allow streamlined calls to the bean if a caller is in the same container. This does not involve the overhead involved with RMI like marshalling etc. This facility will thus improve the performance of applications in which co-location is planned. Local interfaces also provide the foundation for container-managed relationships among entity beans with container-managed persistence.
Ques 11. Can Entity Beans have no create() methods?
Yes. In some cases the data is inserted NOT using Java application, so you may only need to retrieve the information, perform its processing, but not create your own information of this kind.
Ques 12. What are the methods of Entity Bean?
An entity bean consists of 4 groups of methods:
1. create methods: To create a new instance of a CMP entity bean, and therefore insert data into the database, the create() method on the bean’s home interface must be invoked. They look like this: EntityBeanClass ejbCreateXXX(parameters), where EntityBeanClass is an Entity Bean you are trying to instantiate, ejbCreateXXX(parameters) methods are used for creating Entity Bean instances according to the parameters specified and to some programmer-defined conditions.
A bean’s home interface may declare zero or more create() methods, each of which must have 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.
2. finder methods: The methods in the home interface that begin with “find” are called the find methods. These are used to query the EJB server for specific entity beans, based on the name of the method and arguments passed. Unfortunately, there is no standard query language defined for find methods, so each vendor will implement the find method differently. In CMP entity beans, the find methods are not implemented with matching methods in the bean class; containers implement them when the bean is deployed in a vendor specific manner. The deployer will use vendor specific tools to tell the container how a particular find method should behave. Some vendors will use object-relational mapping tools to define the behavior of a find method while others will simply require the deployer to enter the appropriate SQL command.
There are two basic kinds of find methods: single-entity and multi-entity. Single-entity find methods return a remote reference to the one specific entity bean that matches the find request. If no entity beans are found, the method throws an ObjectNotFoundException . Every entity bean must define the single-entity find method with the method name findByPrimaryKey(), which takes the bean’s primary key type as an argument.
The multi-entity find methods return a collection ( Enumeration or Collection type) of entities that match the find request. If no entities are found, the multi-entity find returns an empty collection.
3. remove methods: These methods (you may have up to 2 remove methods, or don’t have them at all) allow the client to physically remove Entity beans by specifying either Handle or a Primary Key for the Entity Bean.
4. home methods: These methods are designed and implemented by a developer, and EJB specification doesn’t have any requirements for them except the need to throw a RemoteException is each home method.
Ques 13. Can I develop an Entity Bean without implementing the create() method in the home interface?
As per the specifications, there can be ‘ZERO’ or ‘MORE’ create() methods defined in an Entity Bean. In cases where create() method is not provided, the only way to access the bean is by knowing its primary key, and by acquiring a handle to it by using its corresponding finder method. In those cases, you can create an instance of a bean based on the data present in the table. All one needs to know is the primary key of that table. i.e. a set a columns that uniquely identify a single row in that table. Once this is known, one can use the ‘getPrimaryKey()’ to get a remote reference to that bean, which can further be used to invoke business methods.
Ques 14. Can the primary key in the entity bean be a Java primitive type such as int?
The primary key can’t be a primitive type–use the primitive wrapper classes, instead. For example, you can use java.lang.Integer as the primary key class, but not int (it has to be a class, not a primitive).
Ques 15. 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:
· 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.
· Stateful Session Beans can have arguments (more than one create method)
Ques 16. What is the difference between ejbCreate() and ejbPostCreate?
The purpose of ejbPostCreate() is to perform clean-up database operations after SQL INSERTs (which occur when ejbCreate() is called) when working with CMP entity beans. ejbCreate() is called before database INSERT operations. You need to use ejbPostCreate() to define operations, like set a flag, after INSERT completes successfully.
Ques 17. What are the optional clauses in EJB QL?
WHERE and ORDERBY clauses are optional in EJB QL where as SELECT and FROM are required clauses.
Ques 18. Can I invoke Runtime.gc() in an EJB?
You shouldn’t. What will happen depends on the implementation, but the call will most likely be ignored.
Most helpful rated by users:
- What are the different kinds of enterprise beans?
- What is Entity Bean?
- What is Session Bean?
- What is the difference between Container-Managed Persistent (CMP) bean and Bean-Managed Persistent(BMP)?
- What are the methods of Entity Bean?