Posted by: Ajai Singh | July 10, 2006

Some EJB Questions

Hi Mayank Thanks for ur valuable comments. I will post the basics of java on my next Blog.

This Blog will be very helpful who are preparing for Sun Certification of EJB. In this blog I have tried to put most of the EJB related questions. Hope it will be useful for u guys all over the java world.

What are the different kinds of enterprise beans?
Different kind of enterrise beans are Stateless session bean, Stateful session bean, Entity bean, Message-driven bean.

What is Session Bean?
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.

What is Entity Bean?
The entity bean is used to represent data in the database.

What are the methods of Entity Bean?
An entity bean consists of 4 groups of methods, create methods.

What is the difference between Container-Managed Persistent (CMP) bean and Bean-Managed Persistent(BMP) ?
Container-managed persistence (CMP) and bean-managed persistence (BMP). With CMP, the container manages the persistence of the entity bean.

What are the callback methods in Entity beans?
Callback methods allows the container to notify the bean of events in its life cycle. The callback methods are defined in the javax.ejb.EntityBean interface.

What is software architecture of EJB?
Session and Entity EJBs consist of 4 and 5 parts respectively, a remote interface.

Can Entity Beans have no create() methods?
Yes. In some cases the data is inserted NOT using Java application,.

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.

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 bean’s home.

What are transaction isolation levels in EJB?
Transaction_read_uncommitted, Transaction_read_committed, Transaction_repeatable_read.

What is EJB Query Language ?
The EJB query language is used to specify a query over container-managed entity beans. The language is similar to SQL. An EJB query is independent of the bean’s mapping to a persistent store.

List out some responsibilities of EJB container.
coordinating system services, controlling threads, managing security and bean lifecycle.

Can file descriptors be used in an EJB application?
No, to avoid any hassles in ejb component distibutability feature file descriptors are disallowed. If allowed a feature like dropping a ear file in different environments with diff operating systems will face a problem as they will have their file hierarchy style.

What are the limitations with regard to Java Reflection in EJB?
In general Java Reflection is allowed in EJB, only those apis which inspect the private or protected variables are disallowed.

Are class loaders allowed in ejb.
Yes only custom class loaders are not allowed.

Are native code allowed in EJB.
No it will cause portability problem so its not allowed.

If EJB specification doesnot allow an EJB to listen to a socket or open a file, can i overcome it by using the helper or dependent classes.
No, any restriction related to EJB also applies to their helper classes.

What is the lifecycle of Entity Bean ?
An entity bean instance is in one of the following THREE states:
1.DOES NOT EXIST state.
2.POOLED state. An instance in the pooled state is not associated with any particular entity object identity.
3.READY state. An instance in the ready state is assigned an entity object identity.

Which one is disallowed in ejb – thread creation or thread management?
Anything related to thread is containers job, so neither creation nor management is allowed. EJB framework is developed to ease the developer from technical aspects and focus on business aspects, so thread creation is controlled by ejb container.

Can we develop a networking application with client socket connections in EJB?
yes we can as long as we dont leave the connection open all the time.

which one is disallowed in EJB reading a file or writing a file?
Both are not allowed as files are not transactional resources, also file hierarchy structure will interfere with the component distributability feature.

Final vs Nonfinal static fields in EJB, which one is allowed and which one is not allowed? reason them.
As Nonfinal static fields cannot be maintained/updated properly in the case of distributed environment its not supported. Final static fields dont have the problem of updation so its allowed. This question is related to runtime distribution problem.

Can we develop a networking application with an EJB acting as a socket listener, if yes how and if no why?
If an EJB is used as a socket listener then it has to be awake all the time listening for incoming calls and so it cannot be passivated. which will not result in being a better system.

 How EJB Invocation happens?
Retrieve Home Object reference from Naming Service via JNDI. Return Home Object reference to the client. Create me a new EJB Object through Home Object interface. Create EJB Object from the Ejb Object. Return EJB Object reference to the client. Invoke business method using EJB Object reference. Delegate request to Bean (Enterprise Bean).

What happens if remove( ) is never invoked on a session bean?
In case of a stateless session bean it may not matter if we call or not as in both cases nothing is done. The number of beans in cache is managed by the container. In case of stateful session bean, the bean may be kept in cache till either the session times out, in which case the bean is removed or when there is a requirement for memory in which case the data is cached and the bean is sent to free pool.

How can I call one EJB from inside of another EJB?
EJBs can be clients of other EJBs. It just works. Use JNDI to locate the Home Interface of the other bean, then acquire an instance reference, and so forth.

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.

Can the primary key in the entity bean be a Java primitive type such as int?
The primary key can’t be a primitive type. Use the primitive wrapper classes, instead. For example, you can use java.lang.Integer as the primary key class, but not int (it has to be a class, not a primitive).

What is the advantage of using Entity bean for database operations, over directly using JDBC API to do database operations? When would I use one over the other?
Entity Beans actually represents the data in a database. It is not that Entity Beans replaces JDBC API. There are two types of Entity Beans Container Managed and Bean Mananged. In Container Managed Entity Bean – Whenever the instance of the bean is created the container automatically retrieves the data from the DB/Persistance storage and assigns to the object variables in bean for user to manipulate or use them. For this the developer needs to map the fields in the database to the variables in deployment descriptor files (which varies for each vendor). In the Bean Managed Entity Bean – The developer has to specifically make connection, retrive values, assign them to the objects in the ejbLoad() which will be called by the container when it instatiates a bean object. Similarly in the ejbStore() the container saves the object values back the the persistance storage. ejbLoad and ejbStore are callback methods and can be only invoked by the container. Apart from this, when you use Entity beans you dont need to worry about database transaction handling, database connection pooling etc. which are taken care by the ejb container.

Brief description about local interfaces?
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 container-managed persistence.

What are the special design care that must be taken when you work with local interfaces?
It is important to understand that the calling semantics of local interfaces are different from those of remote interfaces. For example, remote interfaces pass parameters using call-by-value semantics, while local interfaces use call-by-reference. This means that in order to use local interfaces safely, application developers need to carefully consider potential deployment scenarios up front, then decide which interfaces can be local and which remote, and finally, develop the application code with these choices in mind. While EJB 2.0 local interfaces are extremely useful in some situations, the long-term costs of these choices, especially when changing requirements and component reuse are taken into account, need to be factored into the design decision.

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, message-driven beans are different from stateless session EJBs (and other types of EJBs) in several significant ways: Message-driven 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.


Responses

  1. hi buddy,

    Nice post. Is it interview questions?

  2. Hey Ajai, good going. I am impressed with your efforts to facilitating me and others to brush-up their skills. Also add some example code.
    Thanks! Cheers for stepping into the blog world!!!!

  3. Hey Ajai, good going. I am impressed with your efforts for facilitating me and others to brush-up their skills. Also add some example code.
    Thanks! Cheers for stepping into the blog world!!!!

  4. I have to say, that I can not agree with you in 100%, but it’s just my IMHO, which indeed could be wrong.
    p.s. You have an awesome template . Where have you got it from?

  5. hi ajay, good,but u can add some related code to clearly understanding purpose.THANKS….WE CHEER UP

  6. I noticed that this is not the first time you write about the topic. Why have you chosen it again?


Leave a response

Your response:

Categories