java - Can you do traditional Servlet Filtering with JAX-RS/Jersey? -
imagine have filter starts database transaction, processes request, , then attempts commit transaction.
dofilter(...) { ... transaction.begin(); filterchain.dofilter(request, response); transaction.commit(); }
using jersey, there problems:
- using filter, jersey servlet container commits/flushes response before execution returns filter. so, if commit fails, can't modify return code failure. also, exceptions won't caught jax-rs exceptionmapper.
using containerrequestfilter/containerresponsefilter.
public containerrequest filter(containerrequest request) { ... }
public containerresponse filter(containerrequest request, containerresponse response) { ... }
this allows exceptions bubble exceptionmapper, splits logic on 2 separate methods/interfaces. problem if there's exception doesn't map response, containerresponsefilter never called, can't clean up.
what's preferred way handle in jax-rs environment? there way configure flushing of response, or there class or interface i'm overlooking?
i've been researching bit jax-rs/resteasy application. 2 options considering before reading question:
- write
exceptionmapper<throwable>
(orexceptionmapper<daoexception>
customdaoexception
) , handle there or incontainerresponsefilter
executed because ofexceptionmapper<?>
handling exceptions. - before
transaction.begin()
, check current state of transaction, , roll there if needed.
there problems both options.
- i find
exceptionmapper<throwable>
broad, whileexceptionmapper<daoexception>
might miss other exception, again leaving transaction not cleaned up. - rolling transaction on next request might take long time, possibly causing locking issues other transactions.
so after reading question, i'm thinking:
- using
containerrequestfilter
start transactions (in combination@namebinding
annotation construction). - using
containerresponsefilter
commit transactions (if resource method has not yet closed it). - using
filter
make sure transaction closed, , if not, roll back.
Comments
Post a Comment