JBoss Community

Unable to catch RuntimeException in client EJB

created by Bibhu Jena in EJB3 - View the full discussion

Hi,

 

I want to throw a runtime exception from my dao layer ,and catch it in service layer to parse the exception and show a meaningful message to user.

But, when I do this , EJB wraps this exception with RolledBackException and the client EJB is not able to catch it as it expects a SecurityBreakException  which extends RuntimeException.

 

I can't throw a checked exception from the class as it implements HibernateListenrs.

 

Adding sample code snippet for more clarity:

 

 

// after every save/update/delete the follwing class get triggered

 

public class DataChangeListener implements PostDeleteEventListener, PostUpdateEventListener, PostInsertEventListener, Initializable {

public void onPostInsert(final PostInsertEvent event) {

//check  if the user is not allowed to do so ,throw a SecurityBreakException

// else do nothing and let the transaction commit.

}

public void onPostUpdate(PostUpdateEvent event) {

//same as onPostInsert

}

public void onPostDelete(PostDeleteEvent event) {

//same as onPostInsert

}

 

 

@Local public interface SaveUserDataService{

public void save(Person person);

}

 

//client EJB which expects a SecurityBreakException

@Stateful SaveUserDataServiceBean implements SaveUserDataService{

public void save(Person person){

try{ //code to call dao layer to save a person

} catch (SecurityBreakException e) {

// parse the exception and show a meaningful message to user

          }

      }

}

 

 

//The exception class

public class SecurityBreakException extends RuntimeException {

private static final long serialVersionUID = 1L;

String message;

public SecurityBreakException(String message) {

super(message); this.message = message;

      }

}

 

Please guide.

Reply to this message by going to Community

Start a new discussion in EJB3 at Community