protected void triggerReloadWithNewTransaction(String id) { if (TransactionHelper.isTransactionMarkedRollback()) { throw new AssertionError("The calling transaction is marked rollback"); } else if (TransactionHelper.isTransactionActive()) { // should flush // the calling // transaction TransactionHelper.commitOrRollbackTransaction(); TransactionHelper.startTransaction(); } try { try { triggerReload(id); } catch (RuntimeException cause) { TransactionHelper.setTransactionRollbackOnly(); throw cause; } } finally { if (TransactionHelper.isTransactionActiveOrMarkedRollback()) { boolean wasRollbacked = TransactionHelper.isTransactionMarkedRollback(); TransactionHelper.commitOrRollbackTransaction(); TransactionHelper.startTransaction(); if (wasRollbacked) { TransactionHelper.setTransactionRollbackOnly(); } } } }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { initIfNeeded(); if (request instanceof HttpServletRequest) { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; PathDescriptor pd = engine.getRequestConfiguration().getMatchingConfiguration(req); Config config = new Config(req, pd); AbstractWebContext ctx = initRequest(config, req, resp); if (config.txStarted) { resp = new BufferingHttpServletResponse(resp); } boolean completedAbruptly = true; try { preRequest(req, resp); chain.doFilter(request, resp); postRequest(req, resp); completedAbruptly = false; } finally { if (completedAbruptly) { TransactionHelper.setTransactionRollbackOnly(); } try { cleanup(config, ctx, req, resp); } catch (TransactionRuntimeException e) { // commit failed, report this to the client before stopping buffering resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); throw e; } finally { if (config.txStarted) { ((BufferingHttpServletResponse) resp).stopBuffering(); } } } } else { chain.doFilter(request, response); } }
@Override public void handleEvent(Event event) { if (!Framework.isDevModeSet()) { log.info("Do not flush the directory caches: dev mode is not set"); return; } if (!ReloadEventNames.RELOAD_EVENT_ID.equals(event.getId())) { return; } try { RepositoryManager rm = Framework.getService(RepositoryManager.class); // Transaction management final boolean txStarted = !TransactionHelper.isTransactionActive() && TransactionHelper.startTransaction(); boolean txSucceed = false; try { new UnrestrictedSessionRunner(rm.getDefaultRepositoryName()) { @Override public void run() { DocumentRoutingService service = Framework.getLocalService(DocumentRoutingService.class); service.importAllRouteModels(session); } }.runUnrestricted(); txSucceed = true; } finally { if (txStarted) { if (!txSucceed) { TransactionHelper.setTransactionRollbackOnly(); log.warn("Rollbacking import of route models"); } TransactionHelper.commitOrRollbackTransaction(); } } } catch (NuxeoException e) { log.error("Error while reloading the route models", e); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Transaction main = threadBound.get(); if (main == null) { // first call in thread try { main = TransactionHelper.lookupTransactionManager().getTransaction(); if (main != null) { if (main.getStatus() != Status.STATUS_MARKED_ROLLBACK) { main.registerSynchronization(this); session.afterBegin(); threadBound.set(main); } } } catch (NamingException e) { // no transaction manager, ignore } catch (Exception e) { log.error("Error on transaction synchronizer registration", e); } checkTxActiveRequired(method); } try { return method.invoke(session, args); } catch (Throwable t) { if (TransactionHelper.isTransactionActive() && needsRollback(method, t)) { TransactionHelper.setTransactionRollbackOnly(); } if (t instanceof InvocationTargetException) { Throwable tt = ((InvocationTargetException) t).getTargetException(); if (tt != null) { throw tt; } } throw t; } }