users@jersey.java.net

Re: [Jersey] Database Transactions with Jersey

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 11 Aug 2010 11:05:38 +0200

On Aug 11, 2010, at 10:50 AM, Christian Helmbold wrote:

>
>
> Hello,
>
> I've read older discussions [1][2] about database transactions with
> JAX-RS, but
> can't figure out what would be a good way to handle transactions.
>
> I want to use only JAX-RS interfaces and have two ideas how to handle
> transactions:
>
> 1) Using a ServletFilter.
>

Servlet also supports a ServletRequestListener.


> 2) Starting and committing or rolling back transactions around
> @Transactional
> annotated resource methods with Guice AOP [3].
>
> The mentioned discussions confused me more than they gave me an
> idea. Especially
> I'm in doubt whether the ServletFilter approach would work properly
> due to the
> exception handling of JAX-RS.

Servlet filters or listeners should always be called. It is Jersey
filters where there are some cases where a registered response filter
will not be called (if an exception cannot be mapped to a response, if
a previous response filter throws an exception or an error occurs when
serializing the entity).


> Would it work properly? Would the AOP approach
> work properly? Which approach would you prefer?
>

I think if you are using Guice already the AOP approach may be a nice
solution.

You need to be careful of the case of handling lazy DB access.
Sometimes the serialization of the entity requires access to DB stuff
(e.g. JPA beans) so the transaction needs to be kept open until the
response entity has been fully serialized.

Paul.

> Christian
>
>
> [1] http://markmail.org/thread/orcctyu7hczx3d5w
> [2] http://markmail.org/thread/aefgmci6r2thkqxc
> [3] http://code.google.com/docreader/#p=google-guice&s=google-
> guice&t=AOP
>
> --
> http://scala-forum.org/
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>