Tuesday, 26 June 2012

EJB FAQs-->4

EJB interview questions and answers

61). Why did I get a LockTimedOutException?

A.    When you get a LockTimedOutException while invoking a stateful session EJB, one of two things has occurred:

* You have <allow-concurrent-calls> set to true in your weblogic-ejb-jar.
xml descriptor and your call timed out while waiting to be processed. The timeout used in this case is the value <trans-timeout-seconds> element of the weblogic-ejb-jar.xml descriptor or its default value of 30 seconds.
* You do not have <allow-concurrent-calls> set to true and you attempt to invoke a stateful session bean that is already busy processing another request. In this case, the second method call will not block and a LockTimedOutException will be thrown immediately.

62). What is the life cycle of MDB?

The lifetime of an MDB instance is controlled by the container. Only two states exist: Does not exist and Ready , as illustrated in the following figure:
The life of an MDB instance starts when the container invokes newInstance() on the MDB class to create a new instance. Next, the container calls setMessageDrivenContext() followed by ejbCreate() on the instance. The bean then enters the Ready state and is ready to consume messages.
When a message arrives for the bean, the container invokes the onMessage() method of one of the available instances, passing a Message object in argument. Message s can be consumed and processed concurrently by using multiple instances of the same type.
The container invokes ejbRemove() on the bean instance when it no longer needs the instance. The bean instance can perform clean up operations here.

63). Can an entity bean be a listener for JMS messages?

No. Message driven beans should be used to consume JMS messages.

64). What is Entity Bean. What are the various types of Entity Bean?

Entity bean represents the real data which is stored in the persistent storage like Database or file system. For example, There is a table in Database called Credit_card. This table contains credit_card_no,first_name, last_name, ssn as colums and there are 100 rows in the table. Here each row is represented by one instance of the entity bean and it is found by an unique key (primary key) credit_card_no.

There are two types of entity beans.
1) Container Managed Persistence(CMP)
2) Bean Managed Presistence(BMP)

65). What is IIOP ?

It is Internet Inter Object Resource Broker Protocl

66). Why don't stateful session beans have a pool?

Stateful session beans get instantiated once for each seperate client request and it stores the client information in it, there is no threading concept in EJB hence if there will be an instance pool will exist then there is a possiblity of information leak between different session objects.

therefore there is no concept of instance pooling in stateful session bean.

67). Without using entity beans can we do database transactions?

Without using entity beans we can do database transactions through Springs .Spring can be used to configure declarative transaction management, remote access to your logic using RMI or web services, mailing facilities and various options in persisting your data to a database

68). What is the use of using session facade design pattern in EJB'S?

There are many uses, important one is to reduce network traffic I you are calling many EJB from your Servlet then this is not advised, because it has to make many network trips, so what you do you call a Stateless session bean and this in turn calls other EJB, since they are in same container there is less network calls other thing you can do now is you can convert them to LOCAL EJB which has not network calls. This increases your server bandwidthJ. Problem solver this is good for a highly available system.

69). What is the difference between session and entity beans? When 
should I use one or the other?

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)

70). 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 it’s 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 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 EJBs 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. Consider the case where your EJB needs to support a non HTTP-based client. This higher level of abstraction will be flexible enough to support it.


71).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.

72).what are Container-Managed Transactional attributes ?

Not Supported

The bean is not involved in a transaction. If the bean invoker
calls the bean while involved in a transaction, the invoker's transaction is suspended, the bean executes, and when the bean returns, the invoker's transaction is resumed.

Required

The bean must be involved in a transaction. If the invoker is involved in a transaction, the bean uses the invoker's transaction. If the invoker is not involved in a transaction,
the container starts a new transaction for the bean.
Supports

Whatever transactional state that the invoker is involved in is used for the bean. If the invoker has begun a transaction, the invoker's transaction context is used by the bean. If the invoker is not involved in a transaction, neither is the bean.
RequiresNew

Whether or not the invoker is involved in a transaction, this bean starts a new transaction that exists only for itself. If the invoker calls while involved in a transaction, the invoker's transaction is suspended until the bean completes.

Mandatory


The invoker must be involved in a transaction before invoking this bean. The bean uses the invoker's transaction context.

Never

The bean is not involved in a transaction. Furthermore, the invoker cannot be involved in a transaction when calling the bean. If the invoker is involved in a transaction, a RemoteException is thrown


73).How is persistence implemented in enterprise beans?


Persistence in EJB is taken care of in two ways, depending on how you implement your beans: container managed persistence (CMP) or bean managed persistence (BMP) For CMP, the EJB container which your beans run under takes care of the persistence of the fields you have declared to be persisted with the database - this declaration is in the deployment descriptor. So, anytime you modify a field in a CMP bean, as soon as the method you have executed is finished, the new data is persisted to the database by the container. For BMP, the EJB bean developer is responsible for defining the persistence routines in the proper places in the bean, for instance, the ejbCreate(), ejbStore(), ejbRemove() methods would be developed by the bean developer to make calls to the database. The container is responsible, in BMP, to call the appropriate method on the bean. So, if the bean is being looked up, when the create() method is called on the Home interface, then the container is responsible for calling the ejbCreate() method in the bean, which should have functionality inside for going to the database and looking up the data.


74). Are we allowed to change the transaction isolation property in middle of a transaction?


No. You cannot change the transaction isolation level in the middle of transaction.

75). For Entity Beans, What happens to an instance field not mapped to any persistent storage, when the bean is passivated?


The specification infers that the container never serializes an instance of an Entity bean (unlike stateful session beans). Thus passivation simply involves moving the bean from the ready to the pooled bin. So what happens to the contents of an instance variable is controlled by the programmer. Remember that when an entity bean is passivated the instance gets logically disassociated from it’s remote object. Be careful here, as the functionality of passivation/activation for Stateless Session, Stateful Session and Entity beans is completely different. For entity beans the ejbPassivate method notifies the entity bean that it is being disassociated with a particular entity prior to reuse or for dereference.


76). What is a Message Driven Bean, what functions does a message driven bean have and how do they work in collaboration with JMS?


Message driven beans are the latest addition to the family of component bean types defined by the EJB specification. The original bean types include session beans, which contain business logic and maintain a state associated with client sessions, and entity beans, which map objects to persistent data. Message driven beans will provide asynchrony to EJB based applications by acting as JMS message consumers. A message bean is associated with a JMS topic or queue and receives JMS messages sent by EJB clients or other beans. Unlike entity beans and session beans, message beans do not have home or remote interfaces. Instead, message driven beans are instantiated by the container as required. Like stateless session beans, message beans maintain no client-specific state, allowing the container to optimally manage a pool of message-bean instances. Clients send JMS messages to message beans in exactly the same manner as they would send messages to any other JMS destination. This similarity is a fundamental design goal of the JMS capabilities of the new specification. To receive JMS messages, message driven beans implement the javax.jms.MessageListener interface, which defines a single onMessage() method. When a message arrives, the container ensures that a message bean corresponding to the message topic/queue exists (instantiating it if necessary), and calls its onMessage method passing the client’s message as the single argument. The message bean’s implementation of this method contains the business logic required to process the message. Note that session beans and entity beans are not allowed to function as message beans.


77).What is the advantage of putting an Entity Bean instance from the Ready State to Pooled state?


The idea of the Pooled State is to allow a container to maintain a pool of entity beans that has been created, but has not been yet synchronized or assigned to an EJBObject. This mean that the instances do represent entity beans, but they can be used only for serving Home methods (create or findBy), since those methods do not relay on the specific values of the bean. All these instances are, in fact, exactly the same, so, they do not have meaningful state. Jon Thorarinsson has also added: It can be looked at it this way: If no client is using an entity bean of a particular type there is no need for cachig it (the data is persisted in the database). Therefore, in such cases, the container will, after some time, move the entity bean from the Ready State to the Pooled state to save memory. Then, to save additional memory, the container may begin moving entity beans from the Pooled State to the Does Not Exist State, because even though the bean’s cache has been cleared, the bean still takes up some memory just being in the Pooled State.

78). What is Session 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.

79).If my session bean with single method insert record into 2 entity beans, how can I know that the process is done in same transaction (the 
attributes for these beans are Required)

It depends on the transaction attribute of the session bean also. You have to set the transaction attribute of the session bean either to Required or RequiresNew.


80).Can i map more than one table in a CMP?


No, you cannot map more than one table to a single CMP Entity Bean. CMP has been, in fact, designed to map a single table.

81). Difference between SessionBean remove() and EntityBean remove() method?

SessionBean remove() : inform the container of your loss of interest in this bean. Container will remove the instance.

EntityBean remove() : delete an entity bean without first instantiating it. Delete the row of the table using mentioned primary key.


82). Does each stateless session bean have its own EJBObject?


This is container specific as it is responsible for hadling the beans. There may be a 1:N or M:N relationship between EJBObject and the session Bean.

Reactions:

0 comments:

Post a Comment