Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The JPA spec differentiates between system exceptions and application exceptions. I am a bit confused about where the line is drawn exactly. My guesstimate:

An application exception is an exception that your code or libraries used by your code throw, explicitly or implicitly.

  • Does this include all exceptions, runtime and checked regardless the source?

A system exception is probably an exception thrown by the persistence provider. It certainly contains all subclasses of javax.persistence.PersistenceException.

  • What about other exceptions thrown by provider code?
  • What about exceptions thrown by other Java EE libraries?
  • Does it make a difference if the exception is wrapped in an EJBException?

How can I influence the behaviour by using the ApplicationException annotation? I have never seen it being used yet.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

An application exception shall be thrown when there is a business-logic error, as opposed to a system error.

There is an important difference: application exceptions do not automatically cause a transaction to rollback. The client has an opportunity to recover after an application exception is thrown.

Application exceptions are sent to the client without being repackaged as an EJBException. Therefore, you can use them to report validation errors or business logic problems, and they will reach the client.

Does this include all exceptions, runtime and checked regardless the source?

No. By default, application exceptions are the exceptions that do not extend RuntimeException or RemoteException. You can change this, as described below.

How can I influence the behaviour by using the ApplicationException annotation?

You can use @ApplicationException(rollback=true) if you want the transaction to be rolled back automatically.

You can also use the annotation on subclasses of RuntimeException and RemoteException, in order to avoid wrapping as EJBExceptions, and define their automatic rollback behavior.

What about exceptions thrown by other Java EE libraries?

They will follow the same rules, but you can use the XML descriptor to declare third party classes as application exceptions (with or without automatic rollback).

What about other exceptions thrown by provider code?

Not sure, I think you'd rarely see a non system error (Remote or Runtime exceptions) coming from provider code.

Does it make a difference if the exception is wrapped in an EJBException?

Yes. It will impact how you handle exceptions in the client code.

(Ref: Enterprise JavaBeans 3.0, Bill Burke, O'Reilly)

I hope it helps a bit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...