Request Transaction and Jersey

I would like to implement a transaction per request in conjunction with Jersey resources. By this I mean that I want some place to deal with transactions, for example:

try {
  chain.doFilter(request, response);
  commitSession();
} finally {
  rollbackSession(); // no-op if already committed
  closeSession();
}      

In the past, I have done similar things with servlet filters, but this will not work on Jersey. In Jersey, the exception is caught and converted to 500 longer than the web container. Filters regain control. I believe I can stick with the Filter approach and check response codes, but that is not very attractive. I saw a couple of conversations about this, but I could not find a reliable approach using ResourceFilter, CloseableService or ServletContainer, etc. My terrible decision was to implement a custom ExceptionMapper and roll back there. Has anyone decided this cleanly?

Disclaimer - This project does not currently use Spring, so I would prefer to avoid it for this small use case.

+3

Source: https://habr.com/ru/post/1741154/


All Articles