0% found this document useful (0 votes)
18 views7 pages

Ejb Interview Questions

This document provides a comprehensive overview of EJB 3.0 interview questions, covering essential concepts such as EJB types (session beans, entity beans, message-driven beans), their lifecycle, and annotations used in EJB. It also explains transaction management, dependency injection, and the role of JNDI in EJB. Additionally, it outlines various callback methods and the differences between stateful and stateless beans, along with their respective attributes and functionalities.

Uploaded by

SRIHARSHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Ejb Interview Questions

This document provides a comprehensive overview of EJB 3.0 interview questions, covering essential concepts such as EJB types (session beans, entity beans, message-driven beans), their lifecycle, and annotations used in EJB. It also explains transaction management, dependency injection, and the role of JNDI in EJB. Additionally, it outlines various callback methods and the differences between stateful and stateless beans, along with their respective attributes and functionalities.

Uploaded by

SRIHARSHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EJB - INTERVIEW QUESTIONS

http://www.tutorialspoint.com/ejb/ejb_interview_questions.htm Copyright © tutorialspoint.com

Dear readers, these EJB 3.0 Interview Questions have been designed specially to get you
acquainted with the nature of questions you may encounter during your interview for the subject
of EJB 3.0. As per my experience good interviewers hardly plan to ask any particular question
during your interview, normally questions start with some basic concept of the subject and later
they continue based on further discussion and what you answer:

What is EJB?

EJB stands for Enterprise Java Beans. EJB is an essential part of a J2EE platform. J2EE platform have
component based architecture to provide multi-tiered, distributed and highly transactional
features to enterprise level applications.

EJB provides an architecture to develop and deploy component based enterprise applications
considering robustness, high scalability and high performance. An EJB application can be deployed
on any of the application server compliant with J2EE 1.3 standard specification.

What are the benefits of EJB?

Following are the key benefits of EJB.

Simplified development of large scale enterprise level application.

Application Server/ EJB container provides most of the system level services like transaction
handling, logging, load balancing, persistence mechanism, exception handling and so on.
Developer has to focus only on business logic of the application.

EJB container manages life cycle of ejb instances thus developer needs not to worry about
when to create/delete ejb objects.

What is a Session Bean in EJB?

Session bean stores data of a particular user for a single session. It can be stateful or stateless. It is
less resource intensive as compared to entity beans. Session bean gets destroyed as soon as user
session terminates.

What is Stateful Session Bean in EJB?

A stateful session bean is a type of enterprise bean which preserve the conversational state with
client. A stateful session bean as per its name keeps associated client state in its instance
variables. EJB Container creates a separate stateful session bean to process client's each request.
As soon as request scope is over, statelful session bean is destroyed.

What is Stateless Session Bean in EJB?

A stateless session bean is a type of enterprise bean which is normally used to do independent
operations. A stateless session bean as per its name does not have any associated client state, but
it may preserve its instance state. EJB Container normally creates a pool of few stateless bean's
objects and use these objects to process client's request. Because of pool, instance variable values
are not guaranteed to be same across lookups/method calls.

What is Entity Bean in EJB?

Entity beans represents persistent data storage. User data can be saved to database via entity
beans and later on can be retrived from the database in the entity bean.

What is Message Driven Bean in EJB?

A message driven bean is a type of enterprise bean which is invoked by EJB container when it
receives a message from queue or topic. Message driven bean is a stateless bean and is used to
do task asynchronously.

When a local session bean is used in EJB?


If ejb client is in same environment where ejb session bean is to be deployed then we use local
session bean.

What a remote session bean is used in EJB?

if ejb client is in different environment where ejb session bean is to be deployed then we use
remote session bean.

What are the differences between stateful session bean and stateless session bean?

Following are the differences between stateful session bean and stateless session bean:

EJB Container creates a separate stateful session bean to process client's each
request.whereas EJB Container normally creates a pool of few stateless bean's objects and
use these objects to process client's request.

As soon as request scope is over, statelful session bean is destroyed but stateless bean
remains active.

A stateful session bean is a type of enterprise bean which preserve the conversational state
with client. A stateful session bean as per its name keeps associated client state in its
instance variables Whereas because of pool of stateless session beans, instance variable
values are not guaranteed to be same across lookups/method calls in stateless session
beans.

What are the key components of persistence API in EJB?

Following are the key components of persistence API in EJB:

Entity - A persistent object representing the data-store record. It is good to be serializable.

EntityManager - Persistence interface to do data operations like add/delete/update/find on


persistent objectentity. It also helps to execute queries using Query interface.

Persistence unit persistence. xml - Persistence unit describes the properties of persistence
mechanism.

Data Source ∗ ds. xml - Data Source describes the data-store related properties like
connection url. user-name,password etc.

Is Message Driven bean a stateless bean?

Message driven bean is a stateless bean and is used to do task asynchronously.

Explain @javax.ejb.Stateless annotation.

@javax.ejb.Stateless annotation specifies that a given ejb class is a stateless session


bean.Following are its attributes:

name - Used to specify name of the session bean.

mappedName - Used to specify the JNDI name of the session bean.

description - Used to provide description of the session bean.

Explain @javax.ejb.Stateful annotation.

@javax.ejb.Stateful annotation specifies that a given ejb class is a stateful session bean.Following
are its attributes:

name - Used to specify name of the session bean.

mappedName - Used to specify the JNDI name of the session bean.

description - Used to provide description of the session bean.

Explain @javax.ejb.MessageDrivenBean annotation.


@javax.ejb.MessageDrivenBean annotation specifies that a given ejb class is a message driven
bean. Following are its attributes: name - Used to specify name of the message driven bean.
messageListenerInterface - Used to specify message listener interface for the message driven
bean. activationConfig - Used to specify the configuration details of the message-driven bean in
operational environment of the message driven bean. mappedName - Used to specify the JNDI
name of the message driven bean. description - Used to provide description of the message driven
bean.

Explain @javax.ejb.EJB annotation.

@javax.ejb.EJB annotation is used to specify or inject a dependency as ejb instance into another
ejb. Following are its attributes:

name - Used to specify name which will be used to locate the referenced bean in
environment.

beanInterface - Used to specify the interface type of the referenced bean.

beanName - Used to provide name of the referenced bean.

mappedName - Used to specify the JNDI name of the referenced bean.

description - Used to provide description of the referenced bean.

Explain @javax.ejb.Local annotation.

@javax.ejb.Local annotation is used to specify Local interfaces of a session bean. This local
interface states the business methods of the session bean whichcanbestatelessorstateful.

This interface is used to expose the business methods to local clients which are running in same
deployment/application as EJB.

Following are its attributes:

value - Used to specify the list of local interfaces as an array of interfaces.

Explain @javax.ejb.Remote annotation.

@javax.ejb.Remote annotation is used to specify Remote interfaces of a session bean. This remote
interface states the business methods of the session bean whichcanbestatelessorstateful.

This interface is used to expose the business methods to remote clients which are running in
different deployment/application as EJB.

Following are its attributes:

value - Used to specify the list of remote interfaces as an array of interfaces.

Explain @javax.ejb.ActivationConfigProperty annotation.

@javax.ejb.ActivationConfigProperty annotation is used to specify properties required for a


message driven bean. For example end point, destination, message selector etc.

This annotation is passed as a parameter to activationConfig attribute of


javax.ejb.MessageDrivenBean annotation. Following are its attributes:

propertyName - name of the property.

propertyValue - value of the property.

Explain @javax.ejb.PostActivate annotation.

@javax.ejb.PostActivate annotation is used to specify callback method of ejb lifecycle. This method
will be called when EJB container just activated/reactivated the bean instance.

This interface is used to expose the business methods to local clients which are running in same
deployment/application as EJB.
What is Callback in EJB?

Callback is a mechanism by which life cycle of an enterprise bean can be intercepted. EJB 3.0
specification has specified callbacks for which callback handler methods are to be created. EJB
Container calls these callbacks. We can define callback methods in the ejb class itself or in a
separate class. EJB 3.0 has provided many annotations for callbacks.

What are the callback annotations for stateless bean?

Following is the list of callback annotations for stateless bean:

@PostConstruct - method is invoked when a bean is created for the first time.

@PreDestroy - method is invoked when a bean is removed from the bean pool or is
destroyed.

What are the callback annotations for stateful bean?

Following is the list of callback annotations for stateful bean:

@PostConstruct - method is invoked when a bean is created for the first time.

@PreDestroy - method is invoked when a bean is removed from the bean pool or is
destroyed.

@PostActivate - method is invoked when a bean is loaded to be used.

@PrePassivate - method is invoked when a bean is put back to bean pool.

What are the callback annotations for message driven bean?

Following is the list of callback annotations for message driven bean:

@PostConstruct - method is invoked when a bean is created for the first time.

@PreDestroy - method is invoked when a bean is removed from the bean pool or is
destroyed.

What are the callback annotations for entity bean?

Following is the list of callback annotations for entity bean:

@PrePersist - method is invoked when an entity is created in database.

@PostPersist - method is invoked after an entity is created in database.

@PreRemove - method is invoked when an entity is deleted from the database.

@PostRemove - method is invoked after an entity is deleted from the database.

@PreUpdate - method is invoked before an entity is to be updated in the database.

@PostLoad - method is invoked when a record is fetched from database and loaded into the
entity.

What is a timer service in EJB?

Timer Service is a mechanism using which scheduled application can be build. For example, salary
slip generation on 1st of every month. EJB 3.0 specification has specified @Timeout annotation
which helps in programming the ejb service in a stateless or message driven bean. EJB Container
calls the method which is annotated by @Timeout.

EJB Timer Service is a service provided by Ejb container which helps to create timer and to
schedule callback when timer expires.

Which annoatation is used to inject an ejb into another ejb?

@EJB annotation is used to inject other EJB reference.


Which annoatation is used to inject an datasource into an ejb?

@Resource annotation is used to inject an datasource into an ejb.

Which annoatation is used to inject singleton services like timer service into an ejb?

@Resource annotation is used to inject singleton services like timer service into an ejb.

Which annoatation is used to inject SessionContext into an ejb?

@Resource annotation is used to inject SessionContext into an ejb.

How EJB implements dependency injection?

EJB 3.0 specification provides annotations which can be applied on fields or setter methods to
inject dependencies. EJB Container uses the global JNDI registry to locate the dependency.

What is an EJB Interceptor?

EJB 3.0 provides specification to intercept business methods calls using methods annotated with
@AroundInvoke annotation. An interceptor method is called by ejbContainer before business
method call it is intercepting.

What is class level interceptor in EJB?

Class level interceptor is invoked for every method of the bean. Class level interceptor can be
applied both by annotation or via xmlejb − jar. xml.

What is default interceptor in EJB?

Default interceptor is invoked for every bean within deployment.Default interceptor can be applied
only via xml ejb − jar. xml.

What is method level interceptor in EJB?

Method level interceptor is invoked for a particular method of the bean. Method level interceptor
can be applied both by annotation of via xmlejb − jar. xml.

Explain @Embeddable annotation.

EJB 3.0 provides option to embed JAVA POJO PlainOldJavaObject into an entity bean and allows to map
column names with the methods of the embedded POJO class. A java POJO to be embedded must
be annotated as @Embeddable.

Explain @Lob annotation.

EJB 3.0 provides support for Blob and Clob types using @Lob annotation.

Which types of java classes can be mapped using @Lob annotation?

Following java types can be mapped using @Lob annotation:

java.sql.Blob

java.sql.Clob

byte[]

String

Serializable Object

What is a transaction?

A transaction is a single unit of work items which follows the ACID properties. ACID stands for
Atomic, Consistent,Isolated and Durable.

What ACID stands for?


ACID stands for Atomic, Consistent,Isolated and Durable.

Explain ACID in context of transaction management.

Atomic - If any of work item fails, the complete unit is considered failed. Success meant all
items executes successfully.

Consistent - A transaction must keep the system in consistent state.

Isolated - Each transaction executes independent of any other transaction.

Durable - Transaction should survive system failure if it has been executed or committed.

What is Container Managed Transactions?

In this type, container manages the transaction states.

What is Bean Managed Transactions?

In this type, developer manages the life cycle of transaction states.

Which are the attributes of Container Managed Transactions?

EJB 3.0 has specified following attributes of transactions which EJB containers implement:

REQUIRED - Indicates that business method has to be executed within transaction otherwise
a new transaction will be started for that method.

REQUIRES_NEW - Indicates that a new transaction is to be started for the business method.

SUPPORTS - Indicates that business method will execute as part of transaction.

NOT_SUPPORTED - Indicates that business method should not be executed as part of


transaction.

MANDATORY - Indicates that business method will execute as part of transaction otherwise
exception will be thrown.

NEVER - Indicates if business method executes as part of transaction then an exception will
be thrown.

Which are the attributes of Bean Managed Transactions?

In Bean Managed Transactions, Transactions can be managed by handling exceptions at


application level. Following are the key points to be considered:

Start - When to start a transaction in a business method.

Sucess - Identify success scenario when a transaction is to be committed.

Failed - Identify failure scenario when a transaction is to be rollback.

Which are the attributes of Container Managed Security?

EJB 3.0 has specified following attributes/annotations of security which EJB containers implement:

DeclareRoles - Indicates that class will accept those declared roles. Annotations are applied
at class level.

RolesAllowed - Indicates that a method can be accessed by user of role specified. Can be
applied at class level resulting which all methods of class can be accessed buy user of role
specified.

PermitAll - Indicates that business method is accessible to all. Can be applied at class as
well as at method level.

DenyAll - Indicates that business method is not accessible to any of user specified at class or
at method level.
What is JNDI? Explain its terms in terms of EJB.

JNDI stands for Java Naming and Directory Interface. It is a set of API and service interfaces. Java
based applications use JNDI for naming and directory services. In context of EJB, there are two
terms:

Binding - This refers to assigning a name to an ejb object which can be used later.

Lookup - This refers to looking up and getting an object of ejb.

Explain annotation in EJB to do the database entity relationships/mappings.

EJB 3.0 provides option to define database entity relationships/mappings like one to one, one to
many, many to one and many to many relationships. Following are the relevant annotations:

OneToOne - Objects are having one to one relationship. For example, a passenger can
travel using a single ticket at time.

OneToMany - Objects are having one to many relationship. For example, a father can have
multiple kids.

ManyToOne - Objects are having many to one relationship. For examples, multiple kids
having a single mother.

ManyToMany - Objects are having many to many relationship. For examples, a book can
have mutiple authors and a author can write multiple books.

What is EJBQL?

EJB 3.0, ejb query language is quite handy to write custom queries without worrying about
underlying database details. It is quite similar to HQL, hibernate query language and is often
referred by name EJBQL.

What is Application level Exception in EJB?

If business rule is voilated or exception occurs while executing the business logic. Then EJB
container treats it as Application level exception.

What is System level Exception in EJB?

Any exception which is not caused by business logic or business code. RuntimeException,
RemoteException are SystemException. For example, error during ejb lookup.Then EJB container
treats such exception as System level exception.

How EJB Container handles exceptions?

When Application Exception occurs, ejb container intercepts the exception but returns the same to
the client as it is. It does not roll back the transaction unless it is specified in code by
EJBContext.setRollBackOnly method. EJB Container does not wrap the exception in case of
Application Exception.

When System Exception occurs, ejb container intercepts the exception, rollbacks the transaction
and start the clean up tasks. It wraps the exception into RemoteException and throws it to the
client.

What is Next ?
Further you can go through your past assignments you have done with the subject and make sure
you are able to speak confidently on them. If you are fresher then interviewer does not expect you
will answer very complex questions, rather you have to make your basics concepts very strong.

Second it really doesn't matter much if you could not answer few questions but it matters that
whatever you answered, you must have answered with confidence. So just feel confident during
your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the very
best for your future endeavor. Cheers :-)
Processing math: 100%

You might also like