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:

  1. 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.
  2. 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:

  1. write exceptionmapper<throwable> (or exceptionmapper<daoexception> custom daoexception) , handle there or in containerresponsefilter executed because of exceptionmapper<?> handling exceptions.
  2. before transaction.begin(), check current state of transaction, , roll there if needed.

there problems both options.

  1. i find exceptionmapper<throwable> broad, while exceptionmapper<daoexception> might miss other exception, again leaving transaction not cleaned up.
  2. 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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -