20 EJB Interview Questions and Answers - Freshers, Experienced

Dear Readers, Welcome to EJB Interview questions with answers and explanation. These 20 solved EJB questions will help you prepare for technical interviews and online selection tests conducted during campus placement for freshers and job interviews for professionals.

After reading these tricky EJB questions, you can easily attempt the objective type and multiple choice type questions on EJB.

What is EJB important in perspective of Java?

Enterprise JavaBeans (EJB) is a technology used on the server-side. It has a component architecture that is very essential for the maintenance and the developing a Java Platform. This is the technology that allows development of distributed, transactional, secure and portable applications made using the Java language. It allows modular approach to be taken to implement the enterprise level applications using the APIs that is provided with the technology itself. It provides a back end that allows easy writing of the business code for the enterprise applications. This provides the solutions for the problems to implement integrity, persistence and security. It gives transaction processing, concurrency control and other services like security, remote procedure call, etc.

What is component-based development architecture?

Component is the basic entity that is used in Java beans. This is the term that is dealt with all the applications used and written. The codes are written using the components that are pre-defined and allow easy way for the user to implement it. EJB components allow easy development and provide an interface to allow other components to reassemble the code using the java code. The bean is used to call the server side components and run them on the client side. Security services are provided with the concept of bean and it allows easy implementation of the architecture by using the modular and component based approach.

What are the different types of enterprise beans exist?

EJB uses the container to hold on the beans. There are two major types of bean that exist and they are as follows:
1. Stateful bean :
It consists of the business objects that call the client and deal with the session to access the client side session and their activities.

2. Stateless or Singleton bean :
It consists of business objects that have no association with one another. It allows a single bean instance to be associated with only one client at a particular time. This means that no concurrent processes can run at the same time and it is not allowed to be used. If any concurrent request is made for the processes then the container route the instance to another instance. This way the session can be thread safe and instance variables can be used.

Explain with an example Stateful session beans.

Stateful Session Beans :
It consists of business object to deal with the session throughout and it also allow the access to the bean instance that is limited to only one client at a time. The serialization of the request is being done if any concurrent access request is made to a single bean then using the @AccessTimeout the container of the server throws an exception that is being caught and processed on the basis of the rules defined. The stateful bean state can be changed according to the requirement and free up the memory according to the demands that are rising for the processes to be solved. The example that can be given in this case is checking a web store and logging in. The session will be maintained till the customer logs out.

Explain with an example Stateless session beans

Stateless Session Beans :
It also a business object that is not associated with any other object. In this the instance variables are used to call a single method from the client side to the bean and the moment the customer performs some activities these changes are lost and the call is made to different methods. The instances are pooled and the client can only access a specific bean at a time. It has high overhead due to maintainability of the client sessions but, it uses less resource intensive environment to execute the tasks. The example for this is sending an email for the customer support as it is just one way operation and request reply and request from both the ends at a single time so it is handled by stateless session beans.

Explain with an example Singleton session beans.

Singleton Session Beans :
It allows the global object to be shared within the java virtual machine. It provides concurrent access to only one instance of the bean at a time and it is controlled by the container or the bean handles it itself. This is also called the bean managed container by using the @Lock that keeps a lock on the session object to allow the read and write operations to be locked till one process is accessing. The read and write lock can’t explicitly request the container to start any operation. If the request has to be used then the @Startup is used to call them. The example is used of price list that is global everywhere and need to be updated all the time. So, it allows the updation and the query handling to take place simultaneously.

What is the function of Message drive beans?

Message driven bean :
It allows the execution to be done to trigger the method calls and allow the messages to be provided with high level of abstraction. The services are used of JMS (Java Message service) that allow the message queue to be created and a configuration files is being created using the @MessageDriven syntax. The message driven beans allow the event-driven processes not to have the client view that are Local, Remote, No-interface and listen to the incoming messages that are stored in the queue. It can support many message protocols that allow synchronous and asynchronous transfer of the methods that have been called. The example is sending of the update to more than one node or the updates to more than one system.

Why EJB compliant server is necessary to use EJB components?

The beans are of no use outside the EJB container as the purpose of the EJB container is to provide the services like transaction, security, etc. So if you work outside the EJB container the whole purpose will not be solved except the business logic. And if the business logic also relies on the container like calling methods on interfaces provided by the container, then even the business logic will also get failed. The EJB server keeps and maintains the session so that the updates and easy maintainability provided to the user. It also keeps all necessary configuration file so that a system can be without any fault if it occurred before.

What is the difference between stateless session bean and singleton session bean?

- Stateless beans are used to provide increase performance in the session data, whereas the performance degrades in the case of the singleton session bean.
- Stateless beans don’t provide any association of any object with another one and it also call a single method to be used at a time. Whereas, singleton bean allow the concurrent process to call one object at a time but it maintains an updated file so that more than one object doesn’t have to call the same again and again.
- The stateless bean has no data regarding a particular client and it performs a generic task for all the clients. It implements the web service through which other services can be run and performed in a way that the session can easily be maintained using just that. Whereas, the singleton bean session allow the sharing of an application to many shared systems and allow running and accessing multiple threads at single time (concurrently).

What are the things which should be kept in mind while building a bean?

To build a bean for a particular use lot of requirements that has to be kept in mind is listed below :

a. The bean class should be coded with all the business logic's.
b. The two interfaces i.e. home and component should be coded.
c. An XML deployment descriptor should be created to manage the beans in the server.
d. The component of the interfaces and other files has to be kept in ejb-jar file. That is an archive file used to store the collection of files that uses the interface data or the data that is required to be present on the server.
e. The beans should be deployed on the server. These allow easy access to it and maintainability allow it to be maintained on the server for other purpose and uses.

How can two interfaces and a bean class be compiled?

- Interfaces allow defining the abstract type that specifies the class that has to be implemented using the interface keyword and can consist of the method signature and the constant declarations.
- The constant declarations include the static and final methods and their definitions.
- The program that is used to allow the two interfaces to be compiled with a bean class is given as :
A -d compiler flag is used for compilation of two interfaces and a bean class.
%cd ∼/pojects/advice/src
%java -d /..classes headfirst/*.java
- The above code states that all the beans or .java files are compiled in the headfirst file, then they are put into .class file in the classes directory which can be find by a going a level up from the current(src) directory.

Why is this necessary to use J2EE server with servlet and EJBs together?

It is not necessary for the J2EE server, to be used with the servlets and EJBs. It is used to provide an added functionality to allow the use of web components and EJB components. EJB components are more tightly integrated, which means you can have all of the components respect one another’s transactions and security. The choice can be made by the user for the use of a servlet as client to an enterprise bean if that bean isn’t running in the same application server. Another advantage of j2EE server is the ease with which you can deploy both components types as part of one enterprise solution.

What are the problems caused by loading native code?

There are some problems regarding the load of the native code that is being used in EJB for sometime. It is used to increase the performance of the system and allow it to be more usable in terms of loading of an application. The problems that are caused as follows:

a. It causes loss of portability as it is used on multiple platforms and native code ties the beans.
b. Stability of the system is lost as native code is written in C, so it will have pointer errors, memory leaks, etc.
c. Loss of scalability.
d. Threat to the security of the server.

Explain what are transaction attributes.

The transactions which are managed by the server are specified by transaction attributes. Various transactional attributes are:

1. NotSupported : Transaction context is unspecified.
2. Required : Bean's method invocation is made within a transactional context. If a client is not associated with a transaction, a new transaction is invoked automatically.
3. Supports : If a transactional context exists, a container acts like the transaction attribute is Required, else - like NotSupported.
4. RequiresNew : A method is invoked in a new transaction context.
5. Mandatory : If a transactional context exists, a container acts like the transaction attribute is required, else it throws a javax.ejb.TransactionRequiredException.
6. Never : A method executes only if no transaction context is specified.

State the difference between CMP and BMP.

- For CMP(Container Managed Persistence) developer does not have to write any data logic, the container manages the persistence of the entity bean. A CMP bean developer doesn't need to worry about JDBC code and transactions, because the Container performs database calls and transaction management instead of the programmer.

- BMP (Bean-managed persistence) manages the synchronization of its state and the database that is directed by the container. It uses the database API that can read and write the fields present in the database. The synchronization operation can be performed to manage the transactions automatically for the bean. BMP allows full flexibility to be given to the developer for persistence operations that are complicated for the container and doesn’t have the supported data source.

What are the categories used to contain one or more restricted components?

There are some four categories that consist of the components that are restricted to the use of it. They are as follows:

1. Run-time distribution : This consists of all the enterprise bean instances that are distributed and running in a separate java virtual machines. This distribution is based on the logical consequences of the distributed programming semantics. It includes some static fields that are used at run time.
2. Security model : There are restriction imposed to maintain the level of the security in terms of integrity and containing the authentic data and models.
3. Container control : This container consists of the coordinating system and its services that control the threads, manage security, bean lifecycle, etc. It consists of the restriction that can be preventive and can interfere with the proper functioning of the class.
4. Server model : This consists of the EJB server model, container and its component with pre-defined roles that is given to the bean programming model. This allows the restriction to be prevented by the enterprise bean classes.

What are the different types of enforcement that are present in EJB?

The restrictions are used to limit the use of the user and to allow the functions to be more secure. There are three types of enforcement that can be shown below :

1. Compatibility Test Suite (CTS) and branding :
It allows the server to pass the test and brand themselves. The branding control tools allow ensuring the compatibility between different kinds of servers. The branding specifies that the implement of specification need not to be done by enforcing all the restrictions that is applied on the enterprise beans.

2. Pre-deployment checking tools :
It allows the development tools to have different levels of checking tools that checks for the bugs and errors before finally deploying the servers. The check of performance, security and availability is being performed.

3. Security model restriction enforcement :
It allows the specification to be checked and displayed as true or not. it enforces the restriction to allow the use of Java Security model. It uses the default security setting using java.awt.AWTPermission.

Explain using a sample code the EJB as a stateless session bean.

- The EJB applications are defined as stateless session bean by using the class as @Stateless.
- This is an interface that allows the remote interface to be present for the remote clients. This is accessed through the @Remote.
- The code that is provided to show the session bean in stateless condition is given as :
import javax.ejb.Remote;
import javax.ejb.Stateless;
public class HelloWorldBean implements HelloWorldInterface
   public void helloWorld()
       System.out.println("Hello world !");

What are the ways through which the EJB control can be extended?

To extend the EJB control code it requires the following steps :
1. Create a Java interface that extends the EJB control interface.
2. Check that EJB is a session bean, if it is a session bean then extend it by using
org.apache.beehive.controls.system.ejb.SessionEJBControl, otherwise, if it is an entity bean then extend it using org.apache.beehive.controls.system.ejb.EntityEJBControl.
2. Use the Java interface with @ControlExtension that resides in the following library :
(org.apache.beehive.controls.api.bean.ControlExtension). This is the way through which the Control Annotation Processor can get the control extension using the Java interface.
3. Use Java interface to extend EJB's home and business interfaces. The business interface can consists of either the EJB's local interface or the remote interface.
4. Specifiy the EJB control that need to be looked upon. It uses JNDI name and it is used as @EJBHome.jndiName.
Describe EJB architecture
EJB architecture - The EJB architecture is an extension of Web architecture. It has an additional tier..
EJB - Services that the EJB container offers to the application developer
Services that the EJB container offers - Component Pooling : An EJB component is a server component that provides methods with business logic in distributed applications...
What is the difference between EJB and Java beans?
Difference between EJB and Java beans - Java Beans: Java beans are reusable components that can be used for customized user objects....
Post your comment