0
votes

I have an EJB Stateless Session Bean, like this:

public void persist(Customer customer,Child child){  
try{  
   em.persist(customer); 
   Father father = new Father();
   father.setChild(child); here child is null 
   em.persist(father);
   }catch(Exception e){  

   }    
}  

When the exception (NullPointerException) occurs the transaction is not rolling back and Customer entity is persisted, but when i catch the exception with

   public void persist(Customer customer,Child child){  
   try{  
    em.persist(customer); 
    Father father = new Father();
    father.setChild(child); here child is null 
    em.persist(father);
   }catch(EJBException e){  

   }    
} 

the transaction is rolling back, but i don“t understand why, NullPointerException extends RuntimeException.The docs say a RuntimeException cause a rollback.

1

1 Answers

1
votes

In the second example the NullPointerException is not caught, instead, you are catching an EJBException which is other runtime exception class.

As you say, when the Container intercepts the NullPointerException, the transaction is marked for rollback.

The first example catches Exception, wich is a base class (the Java exceptions are hierarchical ), therefore, any subclass of Exception such as NullPointer or EJBException is caught. In this case the Container doesn't mark the transaction for rollback.