You are on page 1of 6

EJB Interview Questions & Answers

What is session Facade? Session Facade is a design pattern to access the Entity bean through local interface than accessing directly. It increases the performance over the network. In this case we call session bean which on turn call entity bean. What technologies are included in J2EE? The main technologies in J2EE are: Enterprise JavaBeansTM (EJBsTM), JavaServer PagesTM (JSPsTM), Java Servlets, the Java Naming and Directory InterfaceTM (JNDITM), the Java Transaction API (JTA), CORBA, and the JDBCTM data access API. What is EJB role in J2EE? EJB technology is the core of J2EE. It enables developers to write reusable and portable server-side business logic for the J2EE platform. What is Enterprise JavaBeans (EJB) container? It manages the execution of enterprise beans for J2EE applications.

Enterprise beans and their container run on the J2EE server. What is the new basic requirement for a CMP entity bean class in 2.0 from that of ejb 1.1? It must be abstract class. The container extends it and implements methods which are required for managing the relationships Whats 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. 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 How EJB Invocation happens? Step 1: Retrieve Home Object reference from Naming Service via JNDI. step 2: Return Home Object reference to the client. step 3: Create me a new EJB Object through Home Object interface. step 4: Create EJB Object from the Ejb Object step 5: Return EJB Object reference to the client. step 6: Invoke business method using EJB Object reference. step 7: Delegate request to Bean (Enterprise Bean). 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.

Are enterprise beans allowed to use Thread.sleep()? Enterprise beans make use of the services provided by the EJB container, such as life-cycle management. To avoid conflicts with these services, enterprise beans are restricted from performing certain operations: Managing or synchronizing threads What are Local Interfaces? Describe. 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 containermanaged persistence. What are transaction isolation levels in EJB? 1. Transaction_read_uncommitted- Allows a method to read uncommitted data from a DB(fast but not wise). 2. Transaction_read_committed- Guarantees that the data you are getting has been committed. 3. Transaction_repeatable_read Guarantees that all reads of the database will be the same during the transaction (good for read and update operations). 4. Transaction_serializable- All the transactions for resource are performed serial. What is the difference between Container-Managed Persistent (CMP) bean and Bean-Managed

Persistent(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 beans state with the database is handled automatically by the container. This means that the bean developer doesnt nee d 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. A CMP bean developer doesnt need to worry about JDBC code and t ransactions, because the Container performs database calls and transaction management instead of the programmer. Vendor tools are used to map the entity fields to the database and absolutely no database access code is written in the bean class. All table mapping is specified in the deployment descriptor. Otherwise, a BMP bean developer takes the load of linking an application and a database on his shoulders.

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.BMP beans are not 100% databaseindependent, because they may contain database-specific code, but CMP beans are unable to perform complicated

DML (data manipulation language) statements. EJB 2.0 specification introduced some new ways of querying database (by using the EJB QL query language). What is bean managed transaction? If a developer doesnt want a Container to manage transactions, its possible to implement all database operations manually by writing the appropriate JDBC code. This often leads to productivity increase, but it makes an Entity Bean incompatible with some databases and it enlarges the amount of code to be written. All transaction management is explicitly performed by a developer. What are transaction attributes? The transaction attribute specifies how the Container must manage transactions for a method when a client invokes the method via the enterprise beans home or component interface or when the method is invoked as the result of the arrival of a JMS message. (Suns EJB Specification) Below is a list of transactional attributes: 1. NotSupported transaction context is unspecified. 2. Required beans 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. What is the difference between Message Driven Beans and Stateless Session beans? In several ways, the dynamic creation and allocation of message-driven bean instances mimics the behavior of stateless session EJB instances, which exist only for the duration of a particular method call. However, messagedriven beans are different from stateless session EJBs (and other types of EJBs) in several significant ways: Messagedriven beans process multiple JMS messages asynchronously, rather than processing a serialized sequence of method calls. Message-driven beans have no home or remote interface, and therefore cannot be directly accessed by internal or external clients. Clients interact with message-driven beans only indirectly, by sending a message to a JMS Queue or Topic. Only the container directly interacts with a message-driven bean by creating bean instances and passing JMS messages to those instances as necessary. The Container maintains the entire lifecycle of a message-driven bean; instances cannot be created or removed as a result of client requests or other API calls.

EJB Two Marks Q & A


May 2, 2008 at 9:57 am (EJB, Technical Questions, Two Marks) (EJB, Q&A)
What is EJB? EJB stands for Enterprise Java Bean and is widely-adopted server side component architecture for J2EE. It enables rapid development of mission-critical application that are versatile, reusable and portable across middleware while protecting IT investment and preventing vendor lock-in. What is the difference between EJB and Java beans? EJB is a specification for J2EE server, not a product; Java beans may be a graphical component in IDE.

What is EJB role in J2EE? EJB technology is the core of J2EE. It enables developers to write reusable and portable server-side business logic for the J2EE platform. 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, lifecycle, 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 neutralany protocol can be utilized like IIOP,JRMP, HTTP, DCOM,etc. 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 How many enterprise beans? There are three kinds of enterprise beans: 1. session beans, 2. entity beans, and 3. Message-driven beans. What is message-driven bean? A message-driven bean combines features of a session bean and a Java Message Service (JMS) message listener, allowing a business component to receive JMS. A message-driven bean enables asynchronous clients to access the business logic in the EJB tier. What are Entity Bean and Session Bean? Entity Bean is a Java class which implements an Enterprise Bean interface and provides the implementation of the business methods. There are two types: Container Managed Persistence (CMP) and Bean-Managed Persistence (BMP). Session Bean is used to represent a workflow on behalf of a client. There are two types: Stateless and Stateful. Stateless bean is the simplest bean. It doesnt maintain any conversational state with clients between method invocations. Stateful bean maintains state between invocations. What is Session Bean? Explain A session bean is a non-persistent object that implements some business logic running on the server. One way to think of a session object is as a logical extension of the client program that runs on the server. Session beans are used to manage the interactions of entity and other session beans,access resources, and generally perform tasks on behalf of the client. There are two basic kinds of session bean: stateless and stateful. Stateless session beans are made up of business methods that behave like procedures; they operate only on the arguments passed to them when they are invoked. Stateless beans are called stateless because they are transient; they do not maintain business st ate between method invocations. Each invocation of a stateless business method is independent from previous invocations. Because

stateless session beans are stateless, they are easier for the EJB container to manage, so they tend to process requests faster and use fewer resources. Stateful session beans encapsulate business logic and state specific to a client. Stateful beans are called stateful because they do maintain business state between method invocations, held in memory and not persistent. Unlike stateless session beans, clients do not share stateful beans. When a client creates a stateful bean, that bean instance is dedicated to service only that client. This makes it possible to maintain conversational state, which is business state that can be shared by methods in the same stateful bean. 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) What are the services provided by container? Container services are totally depends upon the vendor implementation. But more or less mos t of the vendors suppots the basic services like, 1. LifeCycle Management It is Automatic. 2. Session Management it is used by Developer coded callback methods 3. Transaction Management it is used by configuring deployment descriptor (DD). 4. 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. What is Deployment descriptor? A deployment descriptor is an XML file packaged with the enterprise beans in an EJB JAR file or an EAR file. It contains metadata describing the contents and structure of the enterprise beans, and runtime transaction and security information for the EJB container. How many EJB Objects are created for a Bean? For a Session bean one EJB object for one bean instance. For entity bean it depends , if two users are accessing one row at time then one EJB object is used for both the beans other wise for each bean one EJB object. What is re-entrant. Is session beans reentrant. Is entity beans reentrant?

If we define the entity bean as being reentrant, multiple clients can connect to the Entity bean & execute methods within the entity bean concurrently. Container takes care of synchronization. If we define the entity bean as non-reentrant and many clients connect to it concurrently to execute a method, exception is thrown What is EJB container? An EJB container is a run-time environment that manages one or more enterprise beans. The EJB container manages the life cycles of enterprise bean objects, coordinates distributed transactions, and implements object security. Generally, each EJB container is provided by an EJB server and contains a set of enterprise beans that run on the server. What is EJB server? An EJB server is a high-level process or application that provides a run-time environment to support the execution of server applications that use enterprise beans. An EJB server provides a JNDI-accessible naming service, manages and coordinates the allocation of resources to client applications, provides access to system resources, and provides a transaction service. An EJB server could be provided by, for example, a database or application server. 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. 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 acces to all methods and members. What is the difference between a Server, a Container, and a Connector? An EJB server is an application, usually a product such as BEA WebLogic, that provides (or should provide) for concurrent client connections and manages system resources such as threads, processes, memory, database connections, network connections, etc. An EJB container runs inside (or within) an EJB server, and provides deployed EJB beans with transaction and security management, etc. The EJB container insulates an EJB bean from the specifics of an underlying EJB server by providing a simple, standard API between the EJB bean and its container. A Connector provides the ability for any Enterprise Information System (EIS) to plug into any EJB server which supports the Connector architecture. See Suns J2EE Connectors for more in -depth information on Connectors. Why is ejbFindByPrimaryKey mandatory? An Entity Bean represents persistent data that is stored outside of the EJB Container/Server. The ejbFindByPrimaryKey is a method used to locate and load an Entity Bean into the container, similar to a SELECT statement in SQL. By making this method mandatory, the client programmer can be assured that if they have the primary key of the Entity Bean, then they can retrieve the bean without having to create a new bean each time which would mean creating duplications of persistent data and break the integrity of EJB. What is the default transaction attribute for an EJB? There is no default transaction attribute for an EJB. Section 11.5 of EJB v1.1 spec says that the deployer must specify a value for the transaction attribute for those methods having container managed transaction. In WebLogic, the default transaction attribute for EJB is SUPPORTS. What is software architecture of EJB? Session and Entity EJBs consist of 4 and 5 parts respetively: 1. A remote interface (a client interacts with it), 2. A home interface (used for creating objects and for declaring business methods), 3. A bean object (an object, which actually performs business logic and EJB-specific operations). 4. A deployment descriptor (an XML file containing all information required for maintaining the EJB) or a set of deployment descriptors (if you are using some container-specific features). 5. A Primary Key class is only Entity bean specific. 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.

You might also like