Object aroundInvoke()

in jpa-cdi/src/main/java/org/apache/aries/jpa/cdi/TransactionalInterceptor.java [48:129]


    Object aroundInvoke(InvocationContext invocationContext) throws Exception {

        TransactionManager transactionManager = transactionSupport.getTransactionManager();
        boolean active = isTransactionActive(transactionManager);
        TransactionExtension extension = beanManager.getExtension(TransactionExtension.class);
        Transactional attr = extension.getTransactionAttribute(invocationContext.getMethod());
        Boolean requiresNew = requiresNew(active, attr.value());

        boolean debug = log.isDebugEnabled();

        if (debug) {
            log.debug("Invoking transactional method {}, attr = {}, active = {}, requiresNew = {}",
                    invocationContext.getMethod(), attr.value(), active, requiresNew);
        }

        // Suspend the current transaction if transaction attribute is // REQUIRES_NEW or NOT_SUPPORTED.
        Transaction previous = null;
        if ((requiresNew != Boolean.FALSE) && active) {
            if (debug) {
                log.debug("Suspending the current transaction");
            }
            previous = transactionManager.suspend();
        }

        try {
            if (requiresNew == Boolean.TRUE) {
                if (debug) {
                    log.debug("Starting a new transaction");
                }
                transactionManager.begin();
            }

            Object result;
            try {
                result = invocationContext.proceed();
            } catch (Exception e) {
                if (requiresNew == Boolean.FALSE) {
                    if (needsRollback(attr, e)) {
                        transactionManager.setRollbackOnly();
                    }
                } else if (requiresNew == Boolean.TRUE) {
                    if (needsRollback(attr, e)) {
                        if (debug) {
                            log.debug("Rolling back the current transaction");
                        }
                        transactionManager.rollback();
                    } else {
                        if (debug) {
                            log.debug("Committing the current transaction");
                        }
                        transactionManager.commit();
                    }
                }

                throw e;
            }

            if (requiresNew == Boolean.TRUE) {
                if (transactionManager.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
                    if (debug) {
                        log.debug("Rolling back the current transaction");
                    }
                    transactionManager.rollback();
                } else {
                    if (debug) {
                        log.debug("Committing the current transaction");
                    }
                    transactionManager.commit();
                }
            }

            return result;
        } finally {
            // Resume the previous transaction if it was suspended.
            if (previous != null) {
                if (debug) {
                    log.debug("Resuming the previous transaction");
                }
                transactionManager.resume(previous);
            }
        }
    }