Example usage for org.springframework.dao DataAccessException contains

List of usage examples for org.springframework.dao DataAccessException contains

Introduction

In this page you can find the example usage for org.springframework.dao DataAccessException contains.

Prototype

public boolean contains(@Nullable Class<?> exType) 

Source Link

Document

Check whether this exception contains an exception of the given type: either it is of the given class itself or it contains a nested cause of the given type.

Usage

From source file:org.kuali.rice.ken.service.impl.ConcurrentJob.java

/**
 * Main processing method which invokes subclass implementations of template methods
 * to obtain available work items, and process them concurrently
 * @return a ProcessingResult object containing the results of processing
 *///ww  w .  ja v  a  2 s  .c o m
@SuppressWarnings("unchecked")
public ProcessingResult run() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] STARTING RUN");
    }

    final ProcessingResult result = new ProcessingResult();

    // retrieve list of available work items in a transaction
    Collection<T> items = null;
    try {
        items = (Collection<T>) createNewTransaction().execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus txStatus) {
                return takeAvailableWorkItems();
            }
        });
    } catch (DataAccessException dae) {
        if (dae instanceof OptimisticLockingFailureException
                || dae.contains(OptimisticLockingFailureException.class)
                || dae.contains(OptimisticLockException.class)) {
            // anticipated in the case that another thread is trying to grab items
            LOG.info("Contention while taking work items: " + dae.getMessage());
        } else {
            // in addition to logging a message, should we throw an exception or log a failure here?
            LOG.error("Error taking work items", dae);
            Throwable t = dae.getMostSpecificCause();
            if (t != null && t instanceof SQLException) {
                SQLException sqle = (SQLException) t;
                if (sqle.getErrorCode() == ORACLE_00054
                        && StringUtils.contains(sqle.getMessage(), "resource busy")) {
                    // this is expected and non-fatal given that these jobs will run again
                    LOG.warn("Select for update lock contention encountered: " + sqle.getMessage());
                } else if (sqle.getErrorCode() == ORACLE_00060
                        && StringUtils.contains(sqle.getMessage(), "deadlock detected")) {
                    // this is bad...two parties are waiting forever somewhere...
                    // database is probably wedged now :(
                    LOG.error("Select for update deadlock encountered! " + sqle.getMessage());
                }
            }
        }
        return result;
    } catch (UnexpectedRollbackException ure) {
        LOG.error("UnexpectedRollbackException", ure);
        return result;
    } catch (TransactionException te) {
        LOG.error("Error occurred obtaining available work items", te);
        result.addFailure("Error occurred obtaining available work items: " + te);
        return result;
    }

    Collection<Collection<T>> groupedWorkItems = groupWorkItems(items, result);

    // now iterate over all work groups and process each
    Iterator<Collection<T>> i = groupedWorkItems.iterator();
    List<Future> futures = new ArrayList<Future>();
    while (i.hasNext()) {
        final Collection<T> workUnit = i.next();

        LOG.info("Processing work unit: " + workUnit);
        /* performed within transaction */
        /* executor manages threads to run work items... */
        futures.add(executor.submit(new Callable() {
            public Object call() throws Exception {
                ProcessingResult result = new ProcessingResult();
                try {
                    Collection<?> successes = (Collection<Object>) createNewTransaction()
                            .execute(new TransactionCallback() {
                                public Object doInTransaction(TransactionStatus txStatus) {
                                    return processWorkItems(workUnit);
                                }
                            });
                    result.addAllSuccesses(successes);
                } catch (Exception e) {
                    LOG.error("Error occurred processing work unit " + workUnit, e);
                    for (final T workItem : workUnit) {
                        LOG.error("Error occurred processing work item " + workItem, e);
                        result.addFailure("Error occurred processing work item " + workItem + ": " + e);
                        unlockWorkItemAtomically(workItem);
                    }
                }
                return result;
            }
        }));
    }

    // wait for workers to finish
    for (Future f : futures) {
        try {
            ProcessingResult workResult = (ProcessingResult) f.get();
            result.add(workResult);
        } catch (Exception e) {
            String message = "Error obtaining work result: " + e;
            LOG.error(message, e);
            result.addFailure(message);
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] FINISHED RUN - " + result);
    }

    return result;
}