rh banner

Sunday, 4 January 2015

EJB - Interview Questions and answers for experienced

EJB - Interview Questions and answers for experienced

What is EJB ?

EJB is a standard for building server side components in JAVA. It specifies an agreement between components and application servers that enables any component to run in any application server. EJB components are deployable and can be imported in to an application server which hosts these components. EJB are not intended for client side they are server side components. They are specially meant for complex server side operations like executing complex algorithms or high volume business transactions.

what are the different kind of EJB’s ?

Session beans
Session beans are construct in EJB. They represent business logic of an application. They represent a group of logical related functionality. For instance you can have a customer session bean which can have functionality like customer interest calculation, customer reporting, customer tax calculation etc. But as these functionalities logically belong to customer they will be included in the customer session bean. In short session bean has business logic.
There are two types of session beans:-
Stateless: - they do not maintain state across method calls. So every time client makes a call it’s like a new object from scratch. Stateful— These beans can hold client state across method invocations. This is possible with the use of instance variables declared in the class definition. Every time the client calls it they can get there previous states. Stateless session bean provide greater scalability as EJB container does not have to maintain state across method invocations. Storing state for EJB container is huge activity.
Entity beans
Entity bean represent persistent data in an EJB application. They provide object-oriented abstraction to a relational database. When Session bean needs to access data it called the entity beans. Entity beans do read, write, update and delete from tables. For instance you have a customer table then you will have customer entity bean which maps to the table and can do the CRUD (Create, Read, Update and Delete) operation to the customer table. From architecture angle you can think entity bean as the data access layer of a system.

Message-driven beans

There are situations in project where you would like to communicate asynchronously with some other systems. This is achieved by using message-driven beans. For instance when user places order you would like to submit it asynchronously to the order system and then move ahead with some other work. You would then later comeback after sometime to see if the order is completely or the order system will notify you about the completion of the order.

how do you decide whether you should use session, entity or message driven bean?
Session beans should only implement business logic and work flow.
Entity beans are data objects and represent persistent data. They are only responsible to do database activities like add, update and delete
Message-driven beans are used for receiving asynchronous messages from other systems.

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

Is it possible to write two EJB’s that share the same Remote and Home interfaces, and have different bean classes? if so, what are the advantages/disadvantages? - It’s certainly possible. In fact, there’s an example that ships with the Inprise Application Server of an Account interface with separate implementations for CheckingAccount and SavingsAccount, one of which was CMP and one of which was BMP.

Can an EJB send asynchronous notifications to its clients? - Asynchronous notification is a known hole in the first versions of the EJB spec. The recommended solution to this is to use JMS, which is becoming available in J2EE-compliant servers. The other option, of course, is to use client-side threads and polling. This is not an ideal solution, but it’s workable for many scenarios.

Is there a guarantee of uniqueness for entity beans? - There is no such guarantee. The server (or servers) can instantiate as many instances of the same underlying Entity Bean (with the same PK) as it wants. However, each instance is guaranteed to have up-to-date data values, and be transactionally consistent, so uniqueness is not required. This allows the server to scale the system to support multiple threads, multiple concurrent requests, and multiple hosts.

What’s new in the EJB 2.0 specification? - Following are the main features supported in EJB 2.0: Integration of EJB with JMS, Message Driven Beans, Implement additional Business methods in Home interface which are not specific for bean instance, EJB QL.

What is the need of Remote and Home interfaces. Why can’t there be one? - In a few words, I would say that the main reason is because there is a clear division of roles and responsabilities between the two interfaces. The home interface is your way to communicate with the container, that is who is responsable of creating, locating even removing one or more beans. The remote interface is your link to the bean, that will allow you to remotely access to all its methods and members. As you can see there are two distinct elements (the container and the beans) and you need two different interfaces for accessing to both of them.

What is the difference between Java Beans and EJB? - Java Beans are client-side objects and EJBs are server side object, and they have completely different development, lifecycle, purpose.

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. You should leave system level management like garbage collection for the container to deal with. After all, that’s part of the benefit of using EJBs, you don’t have to manage resources yourself.

What is EJB QL? 

EJB QL is a Query Language provided for navigation across a network of enterprise beans and dependent objects defined by means of container managed persistence. EJB QL is introduced in the EJB 2.0 specification. The EJB QL query language defines finder methods for entity beans with container managed persistenceand is portable across containers and persistence managers. EJB QL is used for queries of two types of finder methods: Finder methods that are defined in the home interface of an entity bean and which return entity objects. Select methods, which are not exposed to the client, but which are used by the Bean Provider to select persistent values that are maintained by the Persistence Manager or to select entity objects that are related to the entity bean on which the query is defined.

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 an

What is in-memory replication?

The process by which the contents in the memory of one physical m/c are replicated in all the m/c in the cluster is called in-memory replication.

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.

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 Passivation and Activation in EJB?

When we are dealing with stateful session beans we need to store the client conversation of the bean so that it can be available in client’s next request. But when we talk about server it has limited resources. If the conversation of the bean is large then the server can run out of resource. So in order to preserve resources EJB server swaps this conversational data in memory to hard disk thus allowing memory to be reclaimed. This process of saving the memory data to hard disk is called as “Passivation”. Now when the client comes back the conversational data is again swapped from the hard disk to the bean. This process is called as “Activation”. The container informs the bean that its about to passivate or activate using the "ejbPassivate()" and "ejbActivate()" methods.

What are the services provided by EJB container ?

1) life cycle management of Enterprise Java beans (EJB).
2) Container manged transaction
3) Container managed persistent
4) Security etc

Enlist the Declarative Transaction types?

MANDATORY:
REQUIRED
REQUIRES_NEW
SUPPORTS
NOT_SUPPORTED
NEVER

Is ejbCreate () method mandatory while defining a Session Bean?

EjbCreate () as being part of the bean’s lifecycle, therefore, it is not mandatory for ejbCreate () method to be present and there will be no errors returned by the compiler.

Differentiate Conversational from Non-conversational interactions?

The interaction between the client and the bean is called conversational while where multi method conversations are not held with clients it is known as non-conversational interactions.

Define  ejb Create() and  EjbPostCreate ()?

When the method is called before the persistence storage is written with the bean state, it is ejbCreate ().
When the method is called after the persistence storage is written with the bean state, it is ejbPostCreate ().

What do you mean by re-entrant? Can you say that session beans as re-entrant? Can entity beans be specified as re-entrant?

If the entity bean is defined as re-entrant, then it is possible by multiple clients to associate with the Entity bean and get methods executed concurrently inside the entity bean. Synchronization is taken care of by container. There is an exception thrown when an entity beam is defined as non-re-entrant and numerous clients are connected to it concurrently to carry out a method.

How does EJB invocation take place?

Home Object reference is retrieved from the Naming Service via JNDI. Home Object reference is returned to the client. The steps are:
Created a new EJB Object via Home Object interface.
Created an EJB Object from the Ejb Object.
Returned an EJB Object reference to the client.
Invoked business method by using EJB Object reference.
Delegate requested to Bean (Enterprise Bean).

Is threading is possible in EJB?

No, Not possible because EJBs are created and managed by container and if in ejbs we allow threading containers life cycle methods will be interrupted by us and also the purpose of EJB is to perform business logic not to controlling a system or implementation level functioning so container will manage the thread and developer can concentrate on business logic.

what is Session facade?

Session Façade is one of the design pattern which is used in EJB component in this pattern session beans are used as a wrapper classes for entity bean and using session bean we interact with Entity bean .it will give clean approach towards client access to bean components and also reduce network calls so as to make the whole system of high performance.

What are the transaction Attribute ?

Transaction is group of operation or work which should be performed either completely or none to maintain the data integrity. All transaction must have ACID property(atomicity ,consistency,integrity,durability) so transaction can be said completed if we commit on successful execution and rollback on unsuccessful execution.
There are two ways of transaction management.
·          Declarative Transaction Mang.
·          Programmatic Transaction Mang.

Explain Blob/Clob

EJB 3.0 provides support for Blob and Clob types using @Lob annotation. Following java types can be mapped using @Lob annotation.
java.sql.Blob
java.sql.Clob
byte[]
String
Serializable Object

Explain 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.

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.

1 comment: