Ad

Showing posts with label EJB. Show all posts
Showing posts with label EJB. Show all posts

Monday, 17 August 2009

To Rollback the Transaction in EJB when an exception occurs

The most common usage of EJB is to handle transactions.When an EJB bean is invoked the transaction will be started by the EJB Container.Whenever there is a problem or exceptions occurs in between the transaction,we need to rollback the transaction completely.There are two approaches EJB provides us to handle this rollback.

(1)Annotating the user defined exception as Application Exception:
       we need to identify the business exceptions that can be thrown by the business logic in our application.Then we have to annotate this exception as javax.ejb.ApplicationException,so that the container will rollback the transaction whenever this business exception is thrown in between the EJB transaction.
The below example shows us how to annotate an user defined exception as Application Exception:

@ApplicationException(rollback=true)                                                                                                    
public class UploadException extends java.lang.Exception {
}

whenever UploadException is thrown in between the EJB transaction,the EJB container will automatically rollback the transaction.
Note:All the Run time exception and the Remote Exception will be rollback automatically by the container

(2) using setRollbackonly() method in the EJB bean:
       Another approach is to use the setRollbackOnly() method of the SessionContext Class.
 The below EJB bean example will show how to use this method.

@Stateless                                                                                                                                            
public class SessionBeanSample implements sampleinterface
{
@Resource
SessionContext ctx;//Get the sessioncontext
public void uploadFile()
{
try
{
//Business Logic
}
catch(Exception e)
{
ctx.setRollbackonly();//By calling the setRollbackOnly on the sessioncontext
} //makes the container to rollback the transcation when
} //any exception occurs in the method uploadFile().

Thursday, 13 August 2009

Default JNDI Name for the EJB Bean

In the EJB version prior to 3.0 we need to specify the JNDI name for the EJB to be deployed in the server specific xml file(For weblogicit is weblogic-ejb-jar.xml).But for EJB 3.0 ,server will provide a default JNDI name for the EJB which gets deployed in the server.Hence we need to know the defaut JNDI name in which the server will bind the ejb object in theJNDI tree,so that client application can able to look up the ejb object by using this JNDI Name.

Default JNDI Name for the EJB Bean in Weblogic:
    For weblogic ,the default jndi name will be "java:comp/env/ejb/[Business interface name]"
For instance if the name of the business interface which the EJB has implemented is sampleInterface,then the default jndi name will be "java:comp/env/ejb/sampleInterface".

Default JNDI Name for the EJB Bean in JBOSS:
     For JBOSS,the default jndi Name will be "[Name of the EAR]/[EJB Bean logical Name]/[Business Interface Type]"

  • [Name of the EAR] --The Name of the EAR file which contains the EJB
  • [EJB Bean logical Name]-The logical name of the EJB bean (We normally give the logical name for the EJB bean by using annotation in the bean like @Stateless(name = "SampleBean")
  • [Business Interface Type]-The Business interface can be local or remote which depends if the Client is in the same JVM or in a deifferent JVM.We normaly give this information in the EJB bean by using annotaton like @Local(businterface.class) or @Remote(businterface.class)

For Intstance if the name of the EAR file is SamApp.ear and the logical name of the Ejb bean is sampleBean and if the busines interface is local,then the default JNDI Name will be "SamApp/sampleBean/local "

NOTE:If we want to bind the EJB with the new JNDI Name other than default one,we can do the same by overriding it in Server specific xml File.