Custom exceptions thrown from service layer with spring transaction support

The title explains the preconditions:

  • spring beans (conforming to an interface), using @Transactional (or <tx:advice>)
  • custom exception is thrown from within the service methods
  • There is a little trick – if the exception is unckecked (extends RuntimeException), it is being wrapped in a TransactionRollbackException.

    So if you are expecting a custom exception (in a unit test, for example), it must be checked. Which is completely logical, of course, but people are often tempted by the easy usage of unchecked exceptions (no explicit need to declare or catch them).

    Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: