Showing posts with label ejb interview. Show all posts
Showing posts with label ejb interview. Show all posts

EJB Interview Questions

EJB Interview Questions
1.What is the need of Remote and Home interface. Why cantit be in one?
The main reason is because there is a clear division of roles and responsibilities between the two interfaces. The home interface is your way to communicate with the container, that is who is responsible 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.
2.Can I develop an Entity Bean without implementing the create() method in the home interface?
As per the specifications, there can be ‘ZERO’ or ‘MORE’ create() methods defined in an Entity Bean. In cases where create() method is not provided, the only way to access the bean is by knowing its primary key, and by acquiring a handle to it by using its corresponding finder method. In those cases, you can create an instance of a bean based on the data present in the table. All one needs to know is the primary key of that table. i.e. a set a columns that uniquely identify a single row in that table. Once this is known, one can use the ‘getPrimaryKey()’ to get a remote reference to that bean, which can further be used to invoke business methods.
3.What is the difference between Context, InitialContext and Session Context? How they are used?
javax.naming.Context is an interface that provides methods for binding a name to an object. It’s much like the RMI Naming.bind() method.
javax.naming.InitialContext is a Context and provides implementation for methods available in the Context interface.
Where as SessionContext is an EJBContext object that is provided by the EJB container to a SessionBean in order for the SessionBean to access the information and/or services or the container.
There is EntityContext too which is also and EJBContext object that’ll be provided to an EntityBean for the purpose of the EntityBean accessing the container details. In general, the EJBContext (SessionContext and EntityContext), AppletContext and ServletContext help the corresponding Java objects in knowing about its ‘context’ [environment in which they run], and to access particular information and/or service. Whereas, the javax.naming.Context is for the purpose of ‘NAMING’ [by the way of referring to] an object.
4.Why an onMessage call in Message-driven bean is always a separate transaction?
EJB 2.0 specification: “An onMessage call is always a separate transaction, because there is never a transaction in progress when the method is called.”
When a message arrives, it is passed to the Message Driven Bean through the onMessage() method, that is where the business logic goes.
Since there is no guarantee when the method is called and when the message will be processed, is the container that is responsible of managing the environment, including transactions.
5.Why are ejbActivate() and ejbPassivate() included for stateless session bean even though they are never required as it is a nonconversational bean?
To have a consistent interface, so that there is no different interface that you need to implement for Stateful Session Bean and Stateless Session Bean.
Both Stateless and Stateful Session Bean implement javax.ejb.SessionBean and this would not be possible if stateless session bean is to remove ejbActivate and ejbPassivate from the interface.
6.Static variables in EJB should not be relied upon as they may break in clusters.Why?
Static variables are only ok if they are final. If they are not final, they will break the cluster. What that means is that if you cluster your application server (spread it across several machines) each part of the cluster will run in its own JVM.
Say a method on the EJB is invoked on cluster 1 (we will have two clusters - 1 and 2) that causes value of the static variable to be increased to 101. On the subsequent call to the same EJB from the same client, a cluster 2 may be invoked to handle the request. A value of the static variable in cluster 2 is still 100 because it was not increased yet and therefore your application ceases to be consistent. Therefore, static non-final variables are strongly discouraged in EJBs.
7.If I throw a custom ApplicationException from a business method in Entity bean which is participating in a transaction, would the transaction be rolled back by container?
EJB Transaction is automatically rolled back only when a SystemException (or a subtype of it) is thrown.
Your ApplicationExceptions can extend from javax.ejb.EJBException, which is a sub class of RuntimeException. When a EJBException is encountered the container rolls back the transaction. EJB Specification does not mention anything about Application exceptions being sub-classes of EJBException.
You can tell container to rollback the transaction, by using setRollBackOnly on SessionContext/EJBContext object as per type of bean you are using.
8.Does Stateful Session bean support instance pooling?
Stateful Session Bean conceptually doesn’t have instance pooling.
9.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.
10.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.
11.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)
12.How to implement an entity bean which the PrimaryKey is an autonumeric field
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, the bean 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 the entity 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.)
13.What is clustering?
Clustering is grouping machines together to transparantly provide enterprise services. Clustering is an essential piece to solving the needs for today’s large websites.
The client does not know the difference between approaching one server or approaching a cluster of servers.
14.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.
15.If my session bean with single method insert record into 2 entity beans, how can know that the process is done in same transaction (the attributes for these beans are Required)?
If your session bean is using bean-managed transactions, you can ensure that the calls are handled in the same transaction by :
javax.transaction.UserTransaction tran= null;
try{
tran=ctx.getUserTransaction();
tran.begin();
myBeanHome1.create(….);
myBeanHome2.create(…);
tran.commit();
}catch(…){}
You may want to check if you’re already running in a transaction by calling tran.getStatus().
16.When should I adopt BMP and when I should use CMP?
You can use CMP and BMP beans in the same application… obviously, a bean can be BMP or CMP, not both at the same time (they are mutually exclusive).
There is a common approach that is normally used and considered a good one. You should start developing CMP beans, unless you require some kind of special bean, like multi-tables, that cannot be completely realized with a single bean. Then, when you realize that you need something more or that you would prefer handling the persistence (performance issue are the most common reason), you can change the bean from a CMP to a BMP.

Can you control when passivation occurs? --Ejb interview

Can you control when passivation occurs? --Ejb interview
The developer, according to the specification, cannot directly control when passivation occurs. Although for Stateful Session Beans, the container cannot passivate an instance that is inside a transaction. So using transactions can be a a strategy to control passivation. The ejbPassivate() method is called during passivation, so the developer has control over what to do during this exercise and can implement the require optimized logic. Some EJB containers, such as BEA WebLogic, provide the ability to tune the container to minimize passivation calls. Taken from the WebLogic 6.0 DTD - “The passivation-strategy can be either “default” or “transaction”. With the default setting the container will attempt to keep a working set of beans in the cache. With the “transaction” setting, the container will passivate the bean after every transaction (or method call for a non-transactional invocation).”

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?

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.

EJB interview Questions -Java

EJB interview Questions -Java
Does each stateless session bean have its own EJB object?
What are the call back methods in Session bean
When should we use session bean/entity bean?
What is the main use of using interface?
How to insert new row and link like Edit and Delete
Can we use session beans & entity beans in single application?
What is lazy loading ?
Why don't stateful session beans have a pool?
What is the difference between CMP 1.1 and CMP 2.0
How many EJB Objects are created for a Bean?
What is the difference between CMP and BMP?
Why we are not keeping EJB class file in war
Already we have http session in servlets.
Which deployment descriptor used in struts?
What is deployment descriptor
What are the different algorithms used for clustering?
What is EJB Query Language?
Is stateful session beans are scalable?
What is abstract schema ?
Is Decorator an EJB design pattern
What is the life cycle of Stateless session bean?
Does Stateful Session bean support instance pooling?
Can I map more than one table in a CMP?
Can a Session Bean be defined without EJB Create() method?
What is the difference between sendRedirect() and
What is an EJB Context?
What is EJB architecture (components)
What is the difference between EJB and J2EE ?
What is the difference between normal Java object and EJB?
When you will chose Stateful session bean and Stateless session bean?
What is difference between Servlet and JSP ?
What is CMR ?
What is the lifecycle of Entity Bean?
What are simple rules that a Primary key class has to follow?
Without home and remote interfaces cant we implement ejb?
What is the difference between EJB Create() and EJB PostCreate()
What are advantages and disadvantages of CMP and BMP
How do I know what the developer named the bean?
What is the life cycle of MDB
What is IIOP ?
What is the importance of the Narrow class in RMI?
What is the difference between local interface and remote interface
When we use abstract class and interface ?
What is the difference between ejbStore() and ejbLoad()
How we deploy in weblogic?
What are the optional clauses in EJB QL?
What is the difference between session context
How to invoke Servlet from EJB components?
How to achieve clustering and connection pooling in ejb
What is the difference between interface and abstract class?
Why java is system independent?
What is deployment descriptor?
How do you check whether the session is active in
What is Entity Bean. What are the various types of Entity Bean
Who makes the EJB Object class?
What are the call back methods in Entity bean?
What is default state of session bean in a ejbjar.xml
What is Message Driven Bean
What is business delicate ? Use ? and what is session
What is the need of two Objects i.e EJB Home object
Is stateless Session bean create() method contains
Can I use session beans and hibernate (instead of entity beans)
What is the difference synchronized block and normal block?
What is session synchronization in EJB
What is Message Driven Bean?
What is the difference between EAR, JAR and WAR file?
What is the difference between sessioncontext and entitycontext
What is local interface. How values will be passed?
What is the use of using session facade design pattern in EJB'S?
Is instance pooling necessary for entity beans?
What is the default transaction attribute in transactions ?
Can we use instance variables in Stateless session beans
What are the various transaction attributes and differences
How Non Java Client access EJB.
Which are features in EJB 2.0 ? and which are features in EJB 3.0?
What is request dispatcher?
What is the difference between EJB Store() and EJB Load()?
What are web services?
Can undefined primary keys are possible with Entity beans
What happens when an interface extends another interface?
How many EJB Objects are created for a Bean
What is Entity Bean. What are the various types of Entity Bean?
Can I develop an Entity Bean without implementing the create
What is the life cycle of Stateful session bean
What is the difference between optimistic locking and pessimistic locking
Give a scenario where you have used stateless session beans
What is return type of create method of an entity bean
What is the use of activate, passivate methods in EJB?
What is the difference between Java Bean and EJB?
what is the difference between HTTP Session and Stateful
Can I invoke Runtime.gc() in an EJB?
Can I develop an Entity Bean without implementing the create
When two entity beans are said to be identical
How to configure the jdbc driver in welogic server
What is design pattern ? use ?
How do J2EE application servers in general and the Weblogic
What is the difference between distributed transactions
How will you propagate exception thrown inside session
What are the services provided by container?
What are the file used in hibernate?
What is ejbdoc? Difference between connector, server, container?
What is meant by Serialization and Externalization?
If i throw a custom Application Exception from a business method
What is the difference between EJB and RMI
How can we call CMP ?
What is the difference between find and select methods in EJB?
What is Session Bean. What are the various types of Session Bean
Is it possible to share an Http Session between a JSP and EJB
What are the various isolation levels in a transaction
What is the difference between JNDI context
What is difference between EJB 1.1 and EJB 2.0
What are the call back methods in Entity bean
How Non Java Client access EJB.
What is static block?
What kind of bean (entity/session) will you use if there
How to transfer a bulk amount of tabular data from a JSP page
Without using entity beans can we do database transactions?
With EJB 1.1 specs, why is unsetSessionContext
If session has thrown Application Exception would
Why are ejbActivate() and ejb Passivate() included
What is local interface
How EJBQL are implemented when it is necessary?
How stateful session bean remembers it's client state
Why we need the transactions?
What is difference between EJB 1.1 and EJB 2.0?
What is the difference between Stateful session bean and Stateless session bean?
Why does EJB needs two interfaces (Home and Remote Interface)
What are simple rules that a Primary key class has to follow ?
What is the difference between EAR, JAR and WAR file ?
What is ACID?
How is Stateful Session bean maintain their states with client?
What is the difference between ejbCreate() and ejbPostCreate
What is Instance pooling ?
How garbage collector works
Why an Message call in Message-driven bean is always a seperate transaction?
Is it possible to invoke multiple Session beans from one Session
When you will chose CMP and BMP?
What is handle in EJB?
How to implement an entity bean which the Primary Key is an auto numeric?
What is Instance pooling?
Does stateless Session bean create() method contain any parameters?
What is parent class of Hashtable?
What is EJB ?