Search

Complete reference to EJB - Enterprise Java Beans interview questions basic to advanced level : SE to Team Lead level Part - 2

Question: What is J2EE?
Answer: J2EE Stands for Java 2 Enterprise Edition. J2EE is an environment for developing and deploying enterprise applications. J2EE specification is defined by Sun Microsystems Inc. The J2EE platform is one of the best platform for the development and deployment of enterprise applications. The J2EE platform is consists of a set of services, application programming interfaces (APIs), and protocols, which provides the functionality necessary for developing multi-tiered, web-based applications. You can download the J2EE SDK and development tools from http://java.sun.com/
Question: What do you understand by a J2EE module?
Answer: A J2EE module is a software unit that consists of one or more J2EE components of the same container type along with one deployment descriptor of that type. J2EE specification defines four types of modules:
a) EJB
b) Web
c) application client and
d) resource adapter
  
In the J2EE applications modules can be deployed as stand-alone units. Modules can also be assembled into J2EE applications.    

Question: Tell me something about J2EE component?
Answer: J2EE component is a self-contained functional software unit supported by a container and configurable at deployment time. The J2EE specification defines the following J2EE components:

  • Application clients and applets are components that run on the client.
  • Java servlet and JavaServer Pages (JSP) technology components are Web components that run on the server.
  • Enterprise JavaBeans (EJB) components (enterprise beans) are business components that run on the server. J2EE components are written in the Java programming language and are compiled in the same way as any program in the language. The difference between J2EE components and "standard" Java classes is that J2EE components are assembled into a J2EE application, verified to be well formed and in compliance with the J2EE specification, and deployed to production, where they are run and managed by the J2EE server or client container.
Question: What are the contents of web module?
Answer: A web module may contain:
a) JSP files
b) Java classes
c) gif and html files and
d) web component deployment descriptors
  
Question: Differentiate between .ear,  .jar and .war files.
Answer: These files are simply zipped file using java jar tool. These files are created for different purposes. Here is the description of these files:
.jar files: These files are with the .jar extenstion. The .jar files contains the libraries, resources and accessories files like property files.
.war files: These files are with the .war extension. The war file contains the web application that can be deployed on the any servlet/jsp container. The .war file contains jsp, html, javascript and other files for necessary for the development of web applications.
.ear files: The .ear file contains the EJB modules of the application.
  

Question: What is the difference between Session Bean and Entity Bean?
Answer:
Session Bean: Session is one of the EJBs and it represents a single client inside the Application Server. Stateless session is easy to develop and its efficient. As compare to entity beans session beans require few server resources.

A session bean is similar to an interactive session and is not shared; it can have only one client, in the same way that an interactive session can have only one user. A session bean is not persistent and it is destroyed once the session terminates.
 
Entity Bean: An entity bean represents persistent global data from the database. Entity beans data are stored into database.
    

Question: Why J2EE is suitable for the development distributed multi-tiered enterprise applications?
Answer: The J2EE platform consists of multi-tiered distributed application model. J2EE applications allows the developers to design and implement the business logic into components according to business requirement. J2EE architecture allows the development of multi-tired applications and the developed applications can be installed on different machines depending on the tier in the multi-tiered J2EE environment . The J2EE application parts are:

a) Client-tier components run on the client machine.
b) Web-tier components run on the J2EE server.
c) Business-tier components run on the J2EE server and the
d) Enterprise information system (EIS)-tier software runs on the EIS servers
    

Question: Why do understand by a container?
Answer: Normally, thin-client multi-tiered applications are hard to write because they involve many lines of intricate code to handle transaction and state management, multithreading, resource pooling, and other complex low-level details. The component-based and platform-independent J2EE architecture makes J2EE applications easy to write because business logic is organized into reusable components. In addition, the J2EE server provides underlying services in the form of a container for every component type. Because you do not have to develop these services yourself, you are free to concentrate on solving the business problem at hand (Source: http://java.sun.com/j2ee/1.3/docs/tutorial/doc/Overview4.html ).

In short containers are the interface between a component and the low-level platform specific functionality that supports the component. The application like Web, enterprise bean, or application client component must be assembled and deployed on the J2EE container before executing.
  

Question: What are the services provided by a container?
Answer: The services provided by container are as follows:
a) Transaction management for the bean
b) Security for the bean
c) Persistence of the bean
d) Remote access to the bean
e) Lifecycle management of the bean
f) Database-connection pooling
g) Instance pooling for the bean
   

Question: What are types of J2EE clients?
Answer: J2EE clients are the software that access the services components installed on the J2EE container. Following are the J2EE clients:
a) Applets
b) Java-Web Start clients
c) Wireless clients
d) Web application

Question: What is Deployment Descriptor?
Answer: A deployment descriptor is simply an XML(Extensible Markup Language) file with the extension of .xml. Deployment descriptor describes the component deployment settings. Application servers reads the deployment descriptor to deploy the components contained in the deployment unit. For example ejb-jar.xml file is used to describe the setting of the EJBs.

Question: What do you understand by JTA and JTS?
Answer: JTA stands for Java Transaction API and JTS stands for Java Transaction Service. JTA provides a standard interface which allows the developers to demarcate transactions in a manner that is independent of the transaction manager implementation. The J2EE SDK uses the JTA transaction manager to implement the  transaction. The code developed by developers does not calls the JTS methods directly, but only invokes the JTA methods. Then JTA internally invokes the JTS routines. JTA is a high level transaction interface used by the application code to control the transaction.
    

Question: What is JAXP?
Answer: The Java API for XML Processing (JAXP) enables applications to parse and transform XML documents independent of a particular XML processing implementation. JAXP or Java API for XML Parsing is an optional API provided by Javasoft. It provides basic functionality for reading, manipulating, and generating XML documents through pure Java APIs. It is a thin and lightweight API that provides a standard way to seamlessly integrate any XML-compliant parser with a Java application.
More at http://java.sun.com/xml/
   

Question: What is J2EE Connector architecture?
Answer: J2EE Connector Architecture (JCA) is a Java-based technology solution for connecting application servers

grey_loader
and enterprise information systems (EIS) as part of enterprise application integration (EAI) solutions. While JDBC is specifically used to connect Java EE applications to databases, JCA is a more generic architecture for connection to legacy systems (including databases). JCA was developed under the Java Community Process as JSR 16 (JCA 1.0) and JSR 112 (JCA 1.5). As of 2006, the current version of JCA is version 1.5. The J2EE Connector API is used by J2EE tools developers and system integrators to create resource adapters. Home page for J2EE Connector architecture http://java.sun.com/j2ee/connector/.
   
Question: What is difference between Java Bean and Enterprise Java Bean?
Answer: Java Bean as is a plain java class with member variables and getter setter methods. Java Beans are defined under JavaBeans specification as Java-Based software component model which includes the features like introspection, customization,  events,  properties and  persistence.
Enterprise JavaBeans or EJBs for short are Java-based software components that comply with Java's  EJB specification. EJBs are delpoyed on the EJB container and executes in the EJB container. EJB is not that simple,  it is used for building distributed applications. Examples of EJB are Session Bean, Entity Bean and Message Driven Bean. EJB is used for server side programming whereas java bean is a client side. Bean is only development but the EJB is developed and then deploy on EJB Container.
   

Question: What is the difference between JTS and JTA?
Answer: In any J2EE application transaction management is one of the most crucial requirements of the application. Given the complexity of today's business requirements, transaction processing occupies one of the most complex segments of enterprise level distributed applications to build, deploy and maintain.  JTS specifies the implementation of a Java transaction manager. JTS specifies the implementation of a Transaction Manager which supports the Java Transaction API (JTA) 1.0 This transaction manager supports the JTA, using which application servers can be built to support transactional Java applications. Internally the JTS implements the Java mapping of the OMG OTS 1.1 specifications. The Java mapping is specified in two packages: org.omg.CosTransactions and org.omg.CosTSPortability. The JTS thus provides a new architecture for transactional application servers and applications, while complying to the OMG OTS 1.1 interfaces internally. This allows the JTA compliant applications to interoperate with other OTS 1.1 complaint applications through the standard IIOP. Java-based applications and Java-based application servers access transaction management functionality via the JTA interfaces. The JTA interacts with a transaction management implementation via JTS. Similarly, the JTS can access resources via the JTA XA interfaces or can access OTS-enabled non-XA resources. JTS implementations can interoperate via CORBA OTS interfaces.

The JTA specifies an architecture for building transactional application servers and defines a set of interfaces for various components of this architecture. The components are: the application, resource managers, and the application server. The JTA specifies standard interfaces for Java-based applications and application servers to interact with transactions, transaction managers, and resource managers JTA transaction management provides a set of interfaces utilized by an application server to manage the beginning and completion of transactions. Transaction synchronization and propagation services are also provided under the domain of transaction management.

In the Java transaction model, the Java application components can conduct transactional operations on JTA compliant resources via the JTS. The JTS acts as a layer over the OTS. The applications can therefore initiate global transactions to include other OTS transaction managers, or participate in global transactions initiated by other OTS compliant transaction managers.
   

Question: Can Entity Beans have no create() methods?
Answer: Entity Beans can have no create() methods. Entity Beans have no create() method, when entity bean is not used to store the data in the database. In this case entity bean is used to retrieve the data from database.
  

Question: What are the call back methods in Session bean?
Answer: Callback methods are called by the container to notify the important events to the beans in its life cycle.  The callback methods are defined in the javax.ejb.EntityBean interface.The callback methods example are ejbCreate(), ejbPassivate(), and ejbActivate().
  

Question: What is bean managed transaction?
Answer: In EJB transactions can be maintained by the container or developer can write own code to maintain the transaction. If a developer doesn’t want a Container to manage transactions, developer can write own code to maintain the database transaction.
   

Question: What are transaction isolation levels in EJB?
Answer: Thre are four levels of transaction isolation are:
* Uncommitted Read
* Committed Read
* Repeatable Read
* Serializable
The four transaction isolation levels and the corresponding behaviors are described below:

Isolation Level
Dirty Read
Non-Repeatable Read
Phantom Read
Read Uncommitted
Possible
Possible
Possible
Read Committed
Not possible
Possible
Possible
Repeatable Read
Not possible
Not possible
Possible
Serializable
Not possible
Not possible
Not possible

An enterprise(Business organization) offers business services to the customers.
-To computerize these services offered by the enterprise, whatever computer application we develop is kwon as an enterprise application.
Examples: 1. Banking application
2. Insurance application
-Business projects are nothing but enterprise application.
Enterprise Application Architecture:
-Any enterprise application will have the following logical partitions (Tiers).
1. Client Tier
2. Present Tier
3. Business Tier
4. Persistent Tier (Integration)
5. Data Tier (Database)
-Tier is nothing but a division or part.
-All java developers are developing presentation tier, business tier and persistent tier only



                    Struts,JSF,springMVC   SpringAOP     Hibernate,JPA,ibatis,spring
Database
 
Ejb entity beans
 
Ejb session beans
 

Servlets, jsp
 

Client tier           
 
.cl

 


Client Tier        Presentation         Business tier       Persistent tier              Data tier
-User interaction is only with client tier only not any of the tiers.
-servlets and jsp’s are used to develop presentation tier.
-presentation tier provides the services to business tier.
-client tier is nothing but user interaction layer.
-Database accessing logic or data accessing from database is nothing but persisting tier.
-Java developers are two kinds.
1. Web component developers-presentation tier
2. Business component developers-business tier, persistent tier
-If we want to use only Sun Technologies to build presentation, business and persistent tiers, the following  things are available.
Presentation Tier: Servlets, Jsp’s
Business Tier:  Ejb Session beans
Persistent Tier: Ejb Entity beans-> went out of industry due to maintenance, nobody use the entity beans.
-Ejb3.0 comes up with lightweight session, entity beans.
-JPA- Java Persistent API.
-JPA and EJB3.0 are from Sun Microsystems.
-JSF is from Sun Micro Systems but not Struts.
What are the differences between EJB 3.0 & Hibernate
Hibernate Vs EJB 3.0 :-
Hibernate 
EJB 3.0 
Session–Cache or collection of loaded objects relating to a single unit of work 
Persistence Context-Set of entities that can be managed by a given EntityManager is defined by a persistence unit 
XDoclet Annotations used to support Attribute Oriented Programming 
Java 5.0 Annotations used to support Attribute Oriented Programming 
Defines HQL for expressing queries to the database 
Defines EJB QL for expressing queries 
Supports Entity Relationshipsthrough mapping files and annotations in JavaDoc 
Support Entity Relationshipsthrough Java 5.0 annotations 
Provides a Persistence Manager APIexposed via the Session, Query, Criteria, and Transaction API 
Provides and Entity Manager Interface for managing CRUD operations for an Entity 
Provides callback support through lifecycle, interceptor, and validatable interfaces 
Provides callback support through Entity Listener and Callback methods 
Entity Relationships are unidirectional. Bidirectional relationships are implemented by two unidirectional relationships 
Entity Relationships are bidirectional or unidirectional 





EJB Architecture:
                                                                                 EJB container
Middleware services
 
HomeObject
 
client
 
                             3    
Bean
 
EJBObject
 
                                                                            7                                                                                                          5                                                                                                                                                                        6                      4                                                                                
    1         2                  10                                                 9                                                                                                                                                     8                                                                                                                                                                                                                                             
JNDI Service or server                                                                                                           

1)      Client looks for HomeObject Referece.
2)      JNDI service gives the home object reference to the client.
3)      Client makes a call on the HomeObject to create EJBObject.
4)      HomeObject calls for the creation of EJBObject.
5)      HomeObject gives the EJBObject reference to the client.
6)      Client makes a method call on the EJBObject.
7)      Middleware services are invoked by the EJB.(i.e Networking, concurrency, security, declarative nature of service, Naming service, Transaction, Resource Pooling).
8)      Object provided by the business methods.
EJBObject delegates the method calls to the bean instance.
9)      After executing the method the result return the bean to the EJBObject.
10)   EJBObject returns the result to the client.
EJB
1). What are the Differences between EJB 3.0 and EJB 2.1?

Differences are:
1) EJB 3.0 allows developers to program EJB components as ordinary Java objects with ordinary Java business interfaces rather than as heavy weight components like EJB 2 (home, remote).

2) In EJB 3.0 you can use annotation or deployment descriptors but in EJB 2 you have to use deployment descriptors.

3) EJB 3 introduced persistence API for database access. In EJB 2 you can use Entity bean.

4) EJB 3.0 is much faster the EJB2
2). What are the key features of the EJB technology?
1.    EJB components are server-side components written entirely in the Java programming language
2. EJB components contain business logic only - no system-level programming & services, such as transactions, security, life-cycle, threading, persistence, etc. are automatically managed for the EJB component by the EJB server.
3. EJB architecture is inherently transactional, distributed, portable multi-tier, scalable and secure.
4. EJB components are fully portable across any EJB server and any OS.
5. EJB architecture is wire-protocol neutral–any protocol can be utilized like IIOP,JRMP, HTTP, DCOM,etc.
3). What is the difference between EJB and RMI
Both of them are java solution for distributed computing.
RMI offers remote access to an object running in another 
JVM and no other services.
But EJB offers far more services than RMI apart from remote method calling. EJB leverages this remote-object feature of RMI and ORB (RMI-IIOP) which can be called by any COBRA client, but also provides other services such as persistence, transaction management, security, resource management, object pooling and messaging.
4).What are the ways for a client application to get an EJB object?
1.    The client has the JNDI name of the EJB object; this name is used to get the EJB object.

2) The client has the JNDI name of the Home object, this is a more usual case; this name is used to get the Home object, then a finder method is invoked on this Home to obtain one or several entity bean objects. The client may also invoke a "create" method on the Home object to create a new EJB object (session or entity).

3) The client has got a handle on an EJB object. A handle is an object that identifies an EJB object; it may be serialized, stored, and used later by a client to obtain a reference to the EJB Object, using the getEJBObject method().

4) The client has got a reference to an EJB object, and some methods defined on the remote interface of this Enterprise Bean return EJB objects.
5). What are the different kinds of enterprise beans?
Stateless session bean- An instance of these non-persistent EJBs provides a service without storing an interaction or conversation state between methods. Any instance can be used for any client.

Stateful session bean- An instance of these non-persistent EJBs maintains state across methods and transactions. Each instance is associated with a particular client.

Entity bean- An instance of these persistent EJBs represents an object view of the data, usually rows in a database. They have a primary key as a unique identifier. Entity bean persistence can be either container-managed or bean-managed.

Message-driven bean- An instance of these EJBs is integrated with the Java Message Service (JMS) to provide the ability for message-driven beans to act as a standard JMS message consumer and perform asynchronous processing between the server and the JMS message producer.
6). What is Entity Bean?
The entity bean is used to represent data in the database. It provides an object-oriented interface to data that would normally be accessed by the JDBC or some other back-end API. More than that, entity beans provide a component model that allows bean developers to focus their attention on the business logic of the bean, while the container takes care of managing persistence,transactions, and access control.

There are two basic kinds of entity beans: container-managed ersistence (CMP) andbean-managed persistence (BMP).

Container-managed persistence beans are the simplest for the bean developer to create and the most difficult for the EJB server to support. This is because all the logic for synchronizing the bean's state with the database is handled automatically by the container. This means that the bean developer doesn't need to write any data access logic, while the EJB server is
supposed to take care of all the persistence needs automatically. With CMP, the container manages the persistence of the entity bean. Vendor tools are used to map the entity fields to the database and absolutely no database access code is written in the bean class.

The bean-managed persistence (BMP) enterprise bean manages synchronizing its state with the database as directed by the container. The bean uses a database API to read and write its fields to the database, but the container tells it when to do each synchronization operation and manages the transactions for the bean automatically. Bean-managed persistence gives the bean developer the flexibility to perform persistence operations that are too complicated for the container or to use a data source that is not supported by the container.
7). Why does EJB needs two interfaces(Home and Remote Interface)?
There is a pure division of roles between the two .
Home Interface is the way to communicate with the container which is responsible for creating , locating and removing beans and Remote Interface is the link to the bean that allows access to all methods and members
8).What is an EJB Context?
EJBContext is an interface that is implemented by the container, and it is also a part of the bean-container contract. Entity beans use a subclass of EJBContext called EntityContext. Session beans use a subclass called SessionContext. These EJBContext objects provide the bean class with information about its container, the client using the bean and the bean itself. They also provide other functions. See the API docs and the spec for more details

9). Does the container create a separate instance of the generated EJBHome and EJBObject classes?
The EJB container maintains an instance pool. The container uses these instances for the EJB Home reference irrespective of the client request. While referring the EJB Object classes the container creates a separate instance for each client request. The instance pool maintenance is up to the implementation of the container. If the container provides one, it is available otherwise it is not mandatory for the provider to implement it. Having said that, yes most of the container providers implement the pooling functionality to increase the performance of the application server. The way it is implemented is again up to the implementer.
10). What's difference between httpsession and EJB session bean ?
A session in a Servlet, is maintained by the Servlet Container through the HttpSession object, that is acquired through the request object. You cannot really instantiate a new HttpSession object, and it doesn't contains any business logic, but is more of a place where tostoreobjects.

A session in EJB is maintained using the SessionBeans. You design beans that can contain business logic, and that can be used by the clients. You have two different session beans: Stateful and Stateless. The first one is somehow connected with a single client. It maintains the state for that client, can be used only by that client and when the client "dies" then the session bean is "lost".

A Stateless Session Bean doesn't maintain any state and there is no guarantee that the same client will use the same stateless bean, even for two calls one after the other. The lifecycle of a Stateless Session EJB is slightly different from the one of a Stateful Session EJB. Is EJB Container's responsibility to take care of knowing exactly how to track each session and redirect the request from a client to the correct instance of a Session Bean. The way this is done is vendor dependant, and is part of the contract.
11). What are the key benefits of the EJB technology? 
1. Rapid application development
2. Broad industry adoption
3. Application portability
4. Protection of IT investment
12). Why do we have a remove method in both EJBHome and EJBObject?
With the EJBHome version of the remove, you are able to delete an entity bean without first instantiating it (you can provide a Primary Key object as a parameter to the remove method). The home version only works for entity beans. On the other hand, the Remote interface version works on an entity bean that you have already instantiated. In addition, the remote version also works on session beans (stateless and stateful) to inform the container of your loss of interest in this bean.
13). What are the services provided by container?
Container services are totally depends upon the "vendor implementation". But more or less most of the vendors suppots the basic services like, 
LifeCycle Management - It is Automatic... 
Resource Management-Creating and destroying the objects based the current load of requests for better usage of memory.
Session Management - it is used by Developer coded callback methods...
Transaction Management - it is used by configuring deployment 
descriptor (DD) ...
Security management - it is used by configuring deployment descriptor (DD) ...
The other services, if any will be in advanced versions, and depends on Vendor specific.

14). 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 its 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.The ?pass-by-reference? can be used between EJBs Remote Interfaces, as they are remote references. While it IS possible to pass an HttpSession as a parameter to an EJB object, it is considered to be ?bad practice (1)? in terms of object oriented design. This is because you are creating an unnecessary coupling between back-end objects (ejbs) and front-end objects (HttpSession). Create a higher-level of abstraction for your ejb’s api. Rather than passing the whole, fat, HttpSession (which carries with it a bunch of http semantics), create a class that acts as a value object (or structure) that holds all the data you need to pass back and forth between front-end/back-end

15). What is the difference between a Coarse Grained? Entity Bean and a Fine Grained? Entity Bean?

A ?fine grained? entity bean is pretty much directly mapped to one relational table, in third normal form. A ?coarse grained? entity bean is larger and more complex, either because its attributes include values or lists from other tables, or because it ?owns? one or more sets of dependent objects. Note that the coarse grained bean might be mapped to a single table or flat file, but that single table is going to be pretty ugly, with data copied from other tables, repeated field groups, columns that are dependent on non-key fields, etc. Fine grained entities are generally considered a liability in large systems because they will tend to increase the load on several of the EJB server?s subsystems (there will be more objects exported through the distribution layer, more objects participating in transactions, more skeletons in memory, more EJB Objects in memory, etc.)
16). Does Stateful Session bean support instance pooling?

Stateful Session Bean conceptually doesn't have instance pooling.
What is the difference between JavaBean and EJB?
A Java Bean is a software component written in the Java programming language that conforms to the JavaBeans component specification. The JavaBeans APIs became part of the "core" Java APIs as of the 1.1 release of the JDK.

The JavaBeans specification defines a Java-based software component model that adds a number of features to the Java programming language. Some of these features include:

* introspection
* customization
* events
* properties
* persistence

Enterprise JavaBeans (EJBs) are Java-based software components that are built to comply with Java's EJB specification and run inside of an EJB 
container supplied by a J2EE provider. An EJB container provides distributed 

No comments:

Post a Comment