Search in sources :

Example 66 with UOW

use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.

the class JaffaTransactionMessageService method getSweeperViewIfOnHoldNoOpenDependency.

/**
 * Takes a Transaction ID and returns the TransactionSweeperView if the Transaction is in the Hold state but has no
 * TransactionDependency in the Open state.  The TransactionSweeperView is a subset of the Transaction model.
 *
 * @param transactionId the ID of the Transaction in question
 * @return a TransactionSweeperView created from the Transaction with the input ID if that Transaction is in
 *         the Hold state but has no TransactionDependency in the Open state.
 * @throws FrameworkException
 */
@Override
public TransactionSweeperView getSweeperViewIfOnHoldNoOpenDependency(String transactionId) throws FrameworkException {
    UOW uow = null;
    TransactionSweeperView transactionSweeperView = null;
    try {
        uow = new UOW();
        Criteria criteria = new Criteria();
        criteria.setTable(TransactionSweeperViewMeta.getName());
        criteria.addCriteria(TransactionSweeperViewMeta.ID, transactionId);
        for (Object result : uow.query(criteria)) {
            if (result instanceof TransactionSweeperView) {
                transactionSweeperView = (TransactionSweeperView) result;
                break;
            }
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return transactionSweeperView;
}
Also used : TransactionSweeperView(org.jaffa.transaction.domain.TransactionSweeperView) Criteria(org.jaffa.persistence.Criteria) TransactionFieldCriteria(org.jaffa.transaction.apis.data.TransactionFieldCriteria) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) TransactionCriteria(org.jaffa.transaction.apis.data.TransactionCriteria) UOW(org.jaffa.persistence.UOW)

Example 67 with UOW

use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.

the class JaffaTransactionMessageService method getTransactionDependency.

/**
 * Gets a TransactionDependency with the input transaction ID and depends on ID
 *
 * @param transactionId the ID of the Transaction the dependency is for
 * @param dependsOnId   the ID of the Transaction that is being depended on
 * @return the TransactionDependency with the input transaction ID and depends on ID
 * @throws FrameworkException
 */
@Override
public TransactionDependency getTransactionDependency(String transactionId, String dependsOnId) throws FrameworkException {
    UOW uow = null;
    TransactionDependency dependency = null;
    try {
        uow = new UOW();
        Criteria criteria = new Criteria();
        criteria.setTable(TransactionDependencyMeta.getName());
        criteria.addCriteria(TransactionDependencyMeta.TRANSACTION_ID, transactionId);
        criteria.addCriteria(TransactionDependencyMeta.DEPENDS_ON_ID, dependsOnId);
        for (Object result : uow.query(criteria)) {
            if (result instanceof TransactionDependency) {
                dependency = (TransactionDependency) result;
            }
        }
    } finally {
        if (uow != null) {
            uow.close();
        }
    }
    return dependency;
}
Also used : Criteria(org.jaffa.persistence.Criteria) TransactionFieldCriteria(org.jaffa.transaction.apis.data.TransactionFieldCriteria) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) TransactionCriteria(org.jaffa.transaction.apis.data.TransactionCriteria) UOW(org.jaffa.persistence.UOW) TransactionDependency(org.jaffa.transaction.domain.TransactionDependency)

Example 68 with UOW

use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.

the class TransactionConsumer method process.

/**
 * This method is invoked directly when processing a Transaction synchronously.
 * It'll invoke the handler associated with the input transaction's payload, as obtained from the transaction configuration file.
 * In case of an error, the status of the transaction will be set to 'E'.
 *
 * @param uow                the provided uow.
 * @param transactionId      the transaction ID.
 * @param unsavedTransaction the transaction if available, otherwise null.
 */
public UOW process(UOW uow, String transactionId, Transaction unsavedTransaction) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("Processing Transaction " + transactionId);
    }
    // Update Transaction status to I
    Boolean postImmediate = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(Transaction.RULE_POST_IMMEDIATE));
    // If the postImmediate is true then we shall make the update to the transaction within same scope of the UOW as this must be synchronous transaction
    if (postImmediate != null && postImmediate.booleanValue()) {
        if (unsavedTransaction == null) {
            // This transaction must already be in the database.
            TransactionEngine.getInstance().updateTransactionStatusToInProcess(uow, transactionId);
        } else {
            uow.flush();
            unsavedTransaction.setStatus(Transaction.Status.I.name());
        }
    } else {
        TransactionEngine.getInstance().updateTransactionStatusToInProcess(transactionId);
    }
    boolean createdLoggingContext = false;
    try {
        Transaction transaction = (unsavedTransaction != null) ? unsavedTransaction : Transaction.findByPK(uow, transactionId);
        TransactionPayload payload = (transaction != null) ? transaction.getTransactionPayloadObject() : null;
        Object dataBean = (payload != null) ? payload.moldInternalPayload() : null;
        if (dataBean != null) {
            // Load transaction configuration
            TransactionInfo transactionInfo = ConfigurationService.getInstance().getTransactionInfo(dataBean);
            if (transactionInfo != null) {
                // Sets Log4J's MDC to enable BusinessEventLogging
                LoggingService.setLoggingContext(dataBean, transactionInfo, transaction);
                createdLoggingContext = true;
                if (log.isInfoEnabled()) {
                    log.info(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.start", null));
                }
                int retryLimit = readRule(RULE_RETRY_LIMIT, DEFAULT_RETRY_LIMIT);
                int retrySleepTimeInMillis = readRule(RULE_RETRY_SLEEP_TIME, DEFAULT_RETRY_SLEEP_TIME);
                int retryCount = 0;
                while (true) {
                    try {
                        // Invokes the handler as specified by the 'toClass and toMethod' combination in the transaction configuration
                        invokeHandler(uow, transactionInfo, dataBean);
                        break;
                    } catch (Exception e) {
                        if (postImmediate == null || !postImmediate) {
                            // Retry only if the exception is listed as a retryable exception
                            String[] exceptions = readRule(RETRY_EXCEPTION_RULE, DEFAULT_RETRY_EXCEPTIONS);
                            Exception ex = null;
                            Class<Exception> clazz = null;
                            for (String exceptionName : exceptions) {
                                if (log.isDebugEnabled()) {
                                    log.debug("Exception: " + exceptionName + " defined as a retryable exception.");
                                }
                                clazz = (Class<Exception>) Class.forName(exceptionName);
                                ex = clazz.cast(ExceptionHelper.extractException(e, clazz));
                                if (ex != null) {
                                    break;
                                }
                            }
                            if (ex != null && ++retryCount <= retryLimit) {
                                if (log.isDebugEnabled()) {
                                    log.debug(clazz.getSimpleName() + " encountered. Will sleep for " + retrySleepTimeInMillis + " milliseconds and then retry", e);
                                }
                                uow.rollback();
                                Thread.sleep(retrySleepTimeInMillis);
                                uow = new UOW();
                                if (log.isDebugEnabled()) {
                                    log.debug("Retry#" + retryCount);
                                }
                            } else {
                                throw e;
                            }
                        } else {
                            throw e;
                        }
                    }
                }
                if (log.isInfoEnabled()) {
                    log.info(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.success", null));
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("There is no transactionInfo for the Transaction. Hence nothing to process.");
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("There is no payload for the Transaction. Hence nothing to process.");
            }
        }
        TransactionField[] transactionFields = transaction.getTransactionFieldArray();
        boolean keep = false;
        if (transactionFields != null) {
            for (TransactionField tField : transactionFields) {
                if (KEEP.equals(tField.getFieldName()) && Boolean.parseBoolean(tField.getValue())) {
                    keep = true;
                    break;
                }
            }
        }
        if (log.isDebugEnabled()) {
            log.info("Finished with transaction: " + transactionId);
        }
        // Commit the UOW if it isn't a post immediate to catch any exceptions that might occur
        if (postImmediate == null || !postImmediate.booleanValue()) {
            if (keep) // Update Transaction status to S
            {
                TransactionEngine.getInstance().updateTransactionStatusToSatisfied(uow, transactionId);
            } else // Delete Transaction Record
            {
                TransactionEngine.getInstance().deleteTransaction(uow, transactionId);
            }
            try {
                uow.commit();
            } catch (Exception e) {
                log.error("Error committing UOW in TransactionConsumer.process", e);
                throw e;
            }
        } else {
            uow.flush();
            TransactionMessageDAOFactory.getTransactionMessageDAO().delete(uow, transaction);
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully processed Transaction " + transaction);
        }
    } catch (Exception e) {
        // Update Transaction status to E
        if (log.isInfoEnabled()) {
            log.info(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.error", null));
        }
        // Rollback the UOW if there is an error
        if (postImmediate == null || !postImmediate.booleanValue()) {
            try {
                uow.rollback();
            } catch (Exception exception) {
                log.error("Error rolling back UOW in transaction consumer");
            }
        }
        // Only need to update the transaction if the process is being run asynchronously.
        if (postImmediate == null || !postImmediate.booleanValue()) {
            // release the UOWs connection before creating a new one to move this transaction to the error state
            uow.rollback();
            // Update Transaction status to E
            TransactionEngine.getInstance().updateTransactionStatusToError(transactionId, e);
            log.error(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.error", null), e);
        }
        throw e;
    } finally {
        // Unset the Logging context
        if (createdLoggingContext) {
            LoggingService.unsetLoggingContext();
        }
    }
    return uow;
}
Also used : TransactionField(org.jaffa.transaction.domain.TransactionField) Transaction(org.jaffa.transaction.domain.Transaction) TransactionInfo(org.jaffa.transaction.services.configdomain.TransactionInfo) TransactionPayload(org.jaffa.transaction.domain.TransactionPayload) UOW(org.jaffa.persistence.UOW)

Example 69 with UOW

use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.

the class TransactionEngine method updateTransactionStatusToInProcess.

/**
 * Retrieves the Transaction and update it's status to I.
 * NOTE: The processing happens within the scope of a local new UOW.
 *
 * @param id the Transaction Id.
 * @throws FrameworkException    Indicates some system error.
 * @throws ApplicationExceptions Indicates application error(s).
 */
public void updateTransactionStatusToInProcess(String id) throws FrameworkException, ApplicationExceptions {
    UOW uow = null;
    try {
        uow = new UOW();
        updateTransactionStatusToInProcess(uow, id);
        uow.commit();
    } catch (Exception e) {
        throw ExceptionHelper.throwAFR(e);
    } finally {
        if (uow != null) {
            uow.rollback();
        }
    }
}
Also used : UOW(org.jaffa.persistence.UOW) DomainObjectChangedException(org.jaffa.exceptions.DomainObjectChangedException) FrameworkException(org.jaffa.exceptions.FrameworkException) MessagingException(javax.mail.MessagingException) ApplicationException(org.jaffa.exceptions.ApplicationException) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException)

Example 70 with UOW

use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.

the class TransactionEngine method updateTransactionStatusToError.

/**
 * Retrieves the Transaction and update it's status to E.
 * NOTE: The processing happens within the scope of a local new UOW.
 *
 * @param id        the Transaction Id.
 * @param exception the cause for the error.
 * @throws FrameworkException    Indicates some system error.
 * @throws ApplicationExceptions Indicates application error(s).
 */
public void updateTransactionStatusToError(String id, Exception exception) throws FrameworkException, ApplicationExceptions {
    UOW uow = null;
    try {
        uow = new UOW();
        Transaction transaction = getTransaction(uow, id);
        if (transaction == null) {
            log.error("Transaction '" + id + "' not found; cannot update it's status to E");
            throw new DomainObjectNotFoundException(TransactionMeta.getLabelToken());
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Updating Transaction '" + transaction.getId() + "' to status E");
            }
            transaction.stampError(exception);
            getTransactionDAO().save(uow, transaction);
            uow.commit();
            try {
                sendFailureNotification(transaction, exception);
            } catch (Exception e) {
                log.error("Error in sending transaction failure notification", e);
            }
        }
    } catch (Exception e) {
        log.error("Failed to set failed transaction id " + id + " to E", e);
        throw ExceptionHelper.throwAFR(e);
    } finally {
        if (uow != null) {
            uow.rollback();
        }
    }
}
Also used : Transaction(org.jaffa.transaction.domain.Transaction) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) UOW(org.jaffa.persistence.UOW) DomainObjectChangedException(org.jaffa.exceptions.DomainObjectChangedException) FrameworkException(org.jaffa.exceptions.FrameworkException) MessagingException(javax.mail.MessagingException) ApplicationException(org.jaffa.exceptions.ApplicationException) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException)

Aggregations

UOW (org.jaffa.persistence.UOW)260 Criteria (org.jaffa.persistence.Criteria)139 FrameworkException (org.jaffa.exceptions.FrameworkException)99 ApplicationException (org.jaffa.exceptions.ApplicationException)88 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)87 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)82 Iterator (java.util.Iterator)47 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)33 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)33 ArrayList (java.util.ArrayList)19 Transaction (org.jaffa.transaction.domain.Transaction)19 Map (java.util.Map)16 LinkedHashMap (java.util.LinkedHashMap)13 HashMap (java.util.HashMap)12 DateTime (org.jaffa.datatypes.DateTime)11 FormTemplate (org.jaffa.modules.printing.domain.FormTemplate)10 IPersistent (org.jaffa.persistence.IPersistent)10 Method (java.lang.reflect.Method)9 Collection (java.util.Collection)8 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)8