Tuesday, 26 June 2012

EJB FAQs-->3

EJB interview questions and answers


36). What is the difference between Container-Managed Persistent (CMP) bean and Bean-Managed Persistent(BMP) ?

Container-managed persistence(CMP) 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. A CMP bean developer doesn't need to worry about JDBC code and transactions, 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% database-independent, 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).

37).Can Entity Beans have no create() methods?

Yes. In some cases the data is inserted NOT using Java application, so you may only need to retrieve the information, perform its processing, but not create your own information of this kind

38). What is bean managed transaction?

If a developer doesn't want a Container to manage transactions, it's 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.

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

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

When working with
BMP entity beans, this is not necessary. You have full control over the entire process, so you can place all the necessary logic surrounding your INSERT statement directly in the ejbCreate() method.

Even if you are creating BMP entity beans, the recommendation would still be to include an empty ejbPostCreate() method. Although some application servers will not enforce it, the spec indicates that this placeholder should be there.

41).What is the difference between sessioncontext and entitycontext?

Since EnterpriseBeans live in a managed container,the container is free to call your EJB components methods at its leisure.

The
container houses the information like current status of bean,security credentials of the user currently accessing the bean in one object is called EJBContext Object.

A context represents a way for beans to perform callbacks and modify their current status

SessionContext is
EJB context for session bean

EntityContext is EJB context for entity bean

Message driven context is EJB context for message driven bean

42). What is the difference between ejbStore() and ejbLoad()?

ejbStore() will be called before ejbPassivate() and is used to store the object to persistent database.
ejbLoad() will be called before ejbActivate() and is used to retrieve the object from persistence datastore.


43). What is the difference between EAR, JAR and WAR file?

J2EE defines three types of archives:

1.
Java Archives (JAR)—A JAR file encapsulates one or more Java

classes, a manifest, and a descriptor.
JAR files are the lowest

level of archive. JAR files are used in J2EE for packaging EJBs

and client-side Java
Applications.

2. Web Archives (WAR)—WAR files are similar to JAR files, except

that they are specifically for web applications made from

Servlets,
JSPs, and supporting classes.

3. Enterprise Archives (EAR)—An EAR file contains all of the

components that make up a particular J2EE
application.

44).How to implement an entity bean which the PrimaryKey is an autonumeric?

The EJB 2 Spec (10.8.3 - Special case: Unknown primary key class) says that in cases where the PrimaryKeys are generated automatically by the underlying database, thebean provider must declare the findByPrimaryKey method to return java.lang.Object and specify the Primary Key Class as java.lang.Object in the Deployment Descriptor.

When defining the Primary Key for the Enterprise Bean, the Deployer using the Container Provider's tools will typically add additional container-managed fields to the concrete subclass of theentity bean class.

In this case, the Container must generate the Primary Key value when the entity bean instance is created (and before ejbPostCreate is invoked on the instance.)

45). Is Decorator an EJB design pattern?

No. Decorator design pattern, is the one which exhibits very low level runtime polymorphism, for the specific and single object (Instance of the class), but not for atleast for a class. It is the stuff to add specific functionality to a single & pointed object and leaves others like it unmodified. It is having close similarities like aspect stuff, but not with EJB stuff.


46). What is lazy loading?


Lazy loading means not creating an object until the first time it is accessed. Lazy loading typically looks like this:

public class Example {
private Vector data = null;

public Vector getData() {
if (this.data == null) {
this.data = new Vector();
// Load data into vector ...
}
return this.data;
}
}

This technique is most useful when you have large hierarchies of objects (such as a product catalog). You can lazy-load subordinate objects as you navigate down the hierarchy, and thereby only creat objects when you need them.


47). What is Message Driven Bean?

An MDB is essentially a message consumer that can listen to a message destination or a message endpoint and gets activated when a message arrives. By design, MDBs are anonymous in nature and hence cannot be directly invoked by a client. The only way to invoke an MDB is to send a message to the destination or endpoint to which it is listening. As MDBs are stateless in nature and are not related to any specific client, they can be pooled for concurrent processing of messages.

48). What is CMR?

CMR is an acronym for Container Managed Relation-ships.
CMR, represented by the cmr fields in the deployment descriptor, which represents the relationship exists between different entities (entity beans), which are in turn exhibiting the
database to the real world. The relationships are one-one, one-many, & many-many.
All the relations/ referential integrities will be managed by container, then the definition's in the deployment descriptor's are called as Container Managed Relationships (CMR)..

49). Can a Session Bean be defined without ejbCreate() method?

The ejbCreate() methods is part of the bean's lifecycle, so, the compiler will not return an error because there is no ejbCreate() method.

However, the
J2EE spec is explicit:

the home interface of a Stateless Session Bean must have a single create() method with no
arguments,
while the session bean class must contain exactly one ejbCreate() method, also without arguments.
Stateful
Session Beans can have arguments (more than one create method)

50). What are the optional clauses in EJB QL?

WHERE and ORDERBY clauses are optional in EJB QL where as SELECT and FROM are required clauses.

51). Can I use session beans and hibernate (instead of entity beans) for persitance?

Yes, we can. It's same as BMP.
52). If session has thrown ApplicaitonException would you use 
EJBContext. setRollBackOnly method?

According to the EJB specification, when the ApplicationException is thrown, the EJBContext.setRollBackOnly method is not called.

Typically, an enterprise bean marks a transaction for rollback to protect data integrity before throwing an
application exception, because application exceptions do not automatically cause the Container to rollback thetransaction.

For example, an AccountTransfer bean which debits one account and credits another account could mark a transaction for rollback if it successfully performs the debit operation, but encounters a failure during the credit operation.

53). What is the difference between activation and passivation?

This would be the difference between Activation and Passivation:

While the bean is in the ready stage, the
EJB container may decide to deactivate, or passivate, the bean by moving it from memory to secondary storage. (Typically, the EJB container uses a least-recently-used algorithm to select a bean for passivation.) The EJB container invokes the bean's ejbPassivate method immediately before passivating it. If a client invokes a business method on the bean while it is in the passive stage, the EJB container activates the bean, moving it back to the ready stage, and then calls the bean's ejbActivate method.

54). How do you check whether the session is active in Stateful session bean ?

In Stateful session bean session is not itself a separate entity. it is contained in the bean it self. So in order to check tht we need the check whether the Stateful session bean is present or not which is done by just invoking the home interface with the jndi


55). What is the difference between find and select methods in EJB?


# A select method can return a persistent field (or a collection thereof) of a related entity bean. A finder method can return only a local or remote interface (or a collection of interfaces).

# Because it is not exposed in any of the local or remote interfaces, a select method cannot be invoked by a client. It can be invoked only by the methods implemented within the entity bean class. A select method is usually invoked by either a business or a home method.

# A select method is defined in the entity bean class. For bean-managed persistence, a finder method is defined in the entity bean class, but for container-managed persistence it is not.


56). What is the difference between local interface and remote interface?

We can describe the following common rules for choosing whether to use remote client view or local client view:

When you will potentially use a distributed environment (if your enterprise bean should be independent of its deployment place), you should obviously choose remote client view.

Use remote client view when you need to be sure that parameters passed between your
EJB and the client (and/or other enterprise beans) should be passed "by value" instead of "by reference." With pass-by-value, the bean will have its own copy of the data, completely separated from the copy of the data at the client. With local client view, you can do pass-by-reference, which means your bean, as well as the client, will work directly with one copy of the data. Any changes made by the bean will be seen by the client and vice versa. Pass-by-reference eliminates time/system expenses for copying data variables, which provides a performance advantage.

If you create an entity bean, you need to remember that it is usually used with a local client view. If your entity bean needs to provide access to a client outside of the existing JVM (i.e., a remote client), you typically use a session bean with a remote client view. This is the so-called Session Facade pattern, the goal of which is that the session bean provides the remote client access to the entity bean.

If you want to use container-managed relationship (CMR) in your enterprise bean, you must expose local interfaces, and thus use local client view. This is mentioned in the EJB specification.

Enterprise beans that are tightly coupled logically are good candidates for using local client view. In other words, if one enterprise bean is always associated with another, it is perfectly appropriate to co-locate them (i.e., deploy them both in one JVM) and organize them through a local interface.


57). Why CMP beans are abstract classes?


We have to provide abstract data to object mapping that maps the fields in our bean to a batabase, and abstract methods methods that corelate these fields.


58). What is the difference between normal Java object and EJB?


Java Object: is a reusable component.
EJB : is a distributed component used to develop business applications.
Container provides runtime environment for EJBs.


59). What is abstract schema?


Abstract schema is part of an entity bean's deployment descriptor which defines the bean's persistent fields and their relationship. Abstract schema is specifed for entity beans with container managed persistence. We specify the name of the Abstract schema name in the deployment descriptor. The queries written in EJB QL for the finder methods references this name. The information provided in this Abstract Schema is used by the container for persistence management and relationship management.

60). What is clustering. What are the different algorithms used for clustering?

Clustering is the use of multiple computers and storage devices to create what seems to be a single system. Clustering is often used to increase a system's availability and for load balancing on highly-trafficked Web sites.

Clustering algorithms find groups of items that are similar. For example, clustering could be used by an insurance company to group customers according to income, age, types of policies purchased and prior claims experience. It divides a data set so that records with similar content are in the same group, and groups are as different as possible from each other. Since the categories are unspecified, this is sometimes referred to as unsupervised learning.

Main
strategies of clustering:
1. Hierarchical clustering
2. K-clustering (partitioning)
3. Self Organizing Maps (SOM)
4. Hybrids (incremental)

Reactions:

0 comments:

Post a Comment