private void doFirstTimeFilter( ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setAttribute(TRANSACTION_FILTER_MARKER_ATTRIBUTE, TRANSACTION_FILTER_MARKER_ATTRIBUTE); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext()); PlatformTransactionManager txManager = (PlatformTransactionManager) ctx.getBean("transactionManager"); TransactionAttribute def = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus tx = null; try { tx = txManager.getTransaction(def); } catch (TransactionException txe) { logger.error("FATAL: not able to start a transaction to service this request."); throw new ServletException(txe); } try { chain.doFilter(request, response); } catch (Exception e) { logger.error( "Exception occurred servicing this request, rolling back the current transaction..."); try { txManager.rollback(tx); } catch (TransactionException txe) { logger.error( "Exception occurred rolling back the current transaction: " + txe.getMessage()); } throw new ServletException(e); } finally { if (!tx.isCompleted()) { try { txManager.commit(tx); } catch (TransactionException txe) { logger.error( "Exception occurred committing the current transaction: " + txe.getMessage()); throw new ServletException(txe); } } } }
public void insert(List<InvoicedOrderLine> invoicedOrderLines) throws BillingServiceException { TransactionCallback<Integer> insertInvoicedOrderLines = new InsertInvoicedOrderLines(invoicedOrderLines, invoiceCreatedAx); Integer returnValue; try { returnValue = transactionTemplate.execute(insertInvoicedOrderLines); } catch (TransactionException es) { // this could happen, for instance, if the db is unreachable. Therefore throw an integration // disturbance throw BillingServiceException.createIntegrationDisturbance(es.getMessage()); } if (returnValue == DB_FUNCTION_ERROR_CODE) { throw BillingServiceException.createBugDisturbance( "BUG_DISTURBANCE : Error when inserting invoiced order lines: " + ((InsertInvoicedOrderLines) insertInvoicedOrderLines).getFailedOrderLine()); } }