Example usage for org.springframework.dao DeadlockLoserDataAccessException DeadlockLoserDataAccessException

List of usage examples for org.springframework.dao DeadlockLoserDataAccessException DeadlockLoserDataAccessException

Introduction

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

Prototype

public DeadlockLoserDataAccessException(String msg, Throwable cause) 

Source Link

Document

Constructor for DeadlockLoserDataAccessException.

Usage

From source file:org.zenoss.zep.dao.impl.DaoUtilsTest.java

@Test
public void testDeadlockRetry() throws Exception {
    final AtomicInteger i = new AtomicInteger();
    final int returnVal = new Random().nextInt();
    int result = DaoUtils.deadlockRetry(new Callable<Integer>() {
        @Override//from www . j  ava  2s  . co m
        public Integer call() throws Exception {
            if (i.incrementAndGet() < 5) {
                throw new DeadlockLoserDataAccessException("My fake exception", null);
            }
            return returnVal;
        }
    });
    assertEquals(i.get(), 5);
    assertEquals(result, returnVal);
}

From source file:org.zenoss.zep.dao.impl.DaoUtilsTest.java

@Test
public void testDeadlockRetryAllFailed() throws Exception {
    final AtomicInteger i = new AtomicInteger();
    try {/*w  w  w .ja  va 2s. c o  m*/
        DaoUtils.deadlockRetry(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                throw new DeadlockLoserDataAccessException(String.valueOf(i.incrementAndGet()), null);
            }
        });
        fail("Should have thrown an exception after 5 retries");
    } catch (DeadlockLoserDataAccessException e) {
        assertEquals("5", e.getMessage());
    }
}

From source file:org.zenoss.zep.dao.impl.DaoUtilsTest.java

@Test
public void testDeadlockRetryNestedException() throws Exception {
    final AtomicInteger i = new AtomicInteger();
    final int returnVal = new Random().nextInt();
    int result = DaoUtils.deadlockRetry(new Callable<Integer>() {
        @Override/*w  ww. j a  va2s . co  m*/
        public Integer call() throws Exception {
            if (i.incrementAndGet() < 5) {
                throw new RuntimeException(new DeadlockLoserDataAccessException("My fake exception", null));
            }
            return returnVal;
        }
    });
    assertEquals(i.get(), 5);
    assertEquals(result, returnVal);
}

From source file:org.spring.data.gemfire.app.dao.GemfireRegionCustomerDao.java

public Customer save(Customer customer) {
    try {/*  w  w  w.  j  a v  a  2  s .  co m*/
        if (customer.isNew()) {
            customer.setId(ID_SEQUENCE.incrementAndGet());
            customersRegion().putIfAbsent(customer.getId(), customer);
        } else {
            customersRegion().put(customer.getId(), customer);
        }

        return customer;
    } catch (CacheWriterException e) {
        throw new DataAccessResourceFailureException("write-through failed", e);
    } catch (LeaseExpiredException e) {
        throw new PessimisticLockingFailureException("global lock lease expired", e);
    } catch (LowMemoryException e) {
        throw new DataAccessResourceFailureException("low memory", e);
    } catch (PartitionedRegionStorageException e) {
        throw new DataAccessResourceFailureException("PR op failure", e);
    } catch (TimeoutException e) {
        throw new DeadlockLoserDataAccessException("global lock acquisition timeout", e);
    } catch (GemFireException e) {
        throw GemfireCacheUtils.convertGemfireAccessException(e);
    }
}

From source file:org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.java

public DataAccessException translate(String task, String sql, SQLException sqlEx) {
    if (task == null) {
        task = "";
    }//  w w w  .  jav  a  2s .  c  o m
    if (sql == null) {
        sql = "";
    }

    // First, try custom translation from overridden method.
    DataAccessException dex = customTranslate(task, sql, sqlEx);
    if (dex != null) {
        return dex;
    }

    // Check SQLErrorCodes with corresponding error code, if available.
    if (this.sqlErrorCodes != null) {
        String errorCode = null;
        if (this.sqlErrorCodes.isUseSqlStateForTranslation()) {
            errorCode = sqlEx.getSQLState();
        } else {
            errorCode = Integer.toString(sqlEx.getErrorCode());
        }

        if (errorCode != null) {

            // Look for defined custom translations first.
            CustomSQLErrorCodesTranslation[] customTranslations = this.sqlErrorCodes.getCustomTranslations();
            if (customTranslations != null) {
                for (int i = 0; i < customTranslations.length; i++) {
                    CustomSQLErrorCodesTranslation customTranslation = customTranslations[i];
                    if (Arrays.binarySearch(customTranslation.getErrorCodes(), errorCode) >= 0) {
                        if (customTranslation.getExceptionClass() != null) {
                            DataAccessException customException = createCustomException(task, sql, sqlEx,
                                    customTranslation.getExceptionClass());
                            if (customException != null) {
                                logTranslation(task, sql, sqlEx, true);
                                return customException;
                            }
                        }
                    }
                }
            }

            // Next, look for grouped error codes.
            if (Arrays.binarySearch(this.sqlErrorCodes.getBadSqlGrammarCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new BadSqlGrammarException(task, sql, sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getInvalidResultSetAccessCodes(),
                    errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new InvalidResultSetAccessException(task, sql, sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDataAccessResourceFailureCodes(),
                    errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DataAccessResourceFailureException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDataIntegrityViolationCodes(),
                    errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getCannotAcquireLockCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new CannotAcquireLockException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDeadlockLoserCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DeadlockLoserDataAccessException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getCannotSerializeTransactionCodes(),
                    errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new CannotSerializeTransactionException(buildMessage(task, sql, sqlEx), sqlEx);
            }
        }
    }

    // We couldn't identify it more precisely - let's hand it over to the SQLState fallback translator.
    if (logger.isDebugEnabled()) {
        logger.debug("Unable to translate SQLException with errorCode '" + sqlEx.getErrorCode()
                + "', will now try the fallback translator");
    }
    return this.fallbackTranslator.translate(task, sql, sqlEx);
}