Example usage for org.hibernate.exception LockAcquisitionException LockAcquisitionException

List of usage examples for org.hibernate.exception LockAcquisitionException LockAcquisitionException

Introduction

In this page you can find the example usage for org.hibernate.exception LockAcquisitionException LockAcquisitionException.

Prototype

public LockAcquisitionException(String string, SQLException root) 

Source Link

Usage

From source file:com.ephesoft.dcma.da.service.BatchInstanceServiceImpl.java

License:Open Source License

/**
 * API to get a batch by applying Hibernate level optimistic locking and to set lock owner ino db.
 * /* ww w .j a  v a  2  s .  c o m*/
 * @param identifier long
 * @param lockOwner {@link ServerRegistry}
 * @throws LockAcquisitionException in case of error
 */
@Transactional
@Override
public void lockBatch(long batchId, ServerRegistry lockOwner) throws LockAcquisitionException {
    BatchInstance batchInstance = batchInstanceDao.getBatch(batchId, LockMode.UPGRADE_NOWAIT);
    if (batchInstance.getLockOwner() == null) {
        batchInstance.setLockOwner(lockOwner);
        batchInstanceDao.saveOrUpdate(batchInstance);
    } else {
        throw new LockAcquisitionException(DataAccessConstant.BATCH_ALREADY_LOCKED, null);
    }
}

From source file:com.ephesoft.dcma.da.service.BatchInstanceServiceImpl.java

License:Open Source License

/**
 * API to acquire lock on a batch instance.
 * //from   w w  w. j  ava2  s . c om
 * @param batchId long
 * @throws LockAcquisitionException in case of error
 */
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public void lockBatch(long batchId) throws LockAcquisitionException {
    BatchInstance batchInstance = batchInstanceDao.getBatch(batchId, LockMode.UPGRADE_NOWAIT);
    if (batchInstance.getStatus() == BatchInstanceStatus.NEW) {
        batchInstance.setStatus(BatchInstanceStatus.LOCKED);
        batchInstanceDao.saveOrUpdate(batchInstance);
    } else {
        throw new LockAcquisitionException(DataAccessConstant.BATCH_ALREADY_LOCKED, null);
    }
}

From source file:com.vmware.bdd.dal.DAL.java

License:Open Source License

/**
 * Helper routine for wrapping a piece of code in a Hibernate transaction.
 *
 * @param obj -- the body of the transaction.
 * @param readOnly -- true if the writes are to be disallowed
 * @param retriesLeft -- the max number of times to retry on lock-acquisition exceptions.
 * 0 if retries are to be disallowed.//from www . ja va  2s.  c om
 **/
@SuppressWarnings("deprecation")
private static <T> T inTransactionDoWork(Saveable<T> obj, boolean readOnly, int retriesLeft) {
    T retval;
    while (true) {
        Session sn = getSession();
        Transaction tx = null;
        FlushMode flushMode = null;
        boolean doRndRollback = ConfigInfo.isDebugEnabled() && stressTxnRollback && (rnd.nextInt() % 5) == 0;
        AuAssert.check(!isInTransaction()); // Disallow nesting for now.
        try {
            tx = sn.beginTransaction();

            if (readOnly && tx != null) {
                flushMode = sn.getFlushMode();
                sn.setFlushMode(FlushMode.MANUAL);
            }

            sn.connection().setReadOnly(readOnly);
            retval = obj.body();
            if (doRndRollback) {
                logger.warn("randomly rollback the transaction");
                throw new LockAcquisitionException("Random Rollback", new SQLException("Random Rollback"));
            }

            if (flushMode != null) {
                sn.setFlushMode(flushMode);
            }

            tx.commit();
            break; // must come right after commit
        } catch (Throwable ex) {
            if (tx != null) {
                if (flushMode != null) {
                    sn.setFlushMode(flushMode);
                }
                tx.rollback();
                flushTransactionCallbacks(false);
            }
            // Strip off the BddException wrapper if a callee added it.
            Throwable realEx = (ex instanceof BddException) ? ex.getCause() : ex;
            if (isRetryable(realEx)) {
                if (retriesLeft > 0) {
                    if (!doRndRollback) {
                        retriesLeft--;
                        reportRetry(retriesLeft, realEx);
                    }
                } else {
                    throw TxRetryException.wrap(realEx, doRndRollback);
                }
            } else if (isUniqViolation(realEx)) {
                throw UniqueConstraintViolationException.wrap((ConstraintViolationException) realEx);
            } else {
                throw BddException.wrapIfNeeded(ex, "Exception in a DAL transaction");
            }
        }
    }
    flushTransactionCallbacks(true);
    return retval;
}

From source file:com.vmware.bdd.service.impl.TestService.java

License:Open Source License

@Override
@Transactional/* w w w  . j ava  2s.  c o  m*/
@RetryTransaction(7)
public void retryFive() {
    logger.info("retryFive:: start");
    counter++;
    if (counter < 5) {
        throw new LockAcquisitionException("sample dal exception", new SQLException("1111"));
    }
}

From source file:com.vmware.bdd.service.impl.TestService.java

License:Open Source License

@Override
@Transactional//from   www.  jav a2  s . c om
@RetryTransaction(10)
public void retryForever() {
    logger.info("retryForever:: start");
    throw new LockAcquisitionException("sample dal exception", new SQLException("1111"));
}

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testExceptions() throws HibernateException {
    SQLException sqlEx = new SQLException("argh", "27");

    final JDBCConnectionException jcex = new JDBCConnectionException("mymsg", sqlEx);
    try {//from ww  w  . j a v  a2s  .  c o m
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw jcex;
            }
        });
        fail("Should have thrown DataAccessResourceFailureException");
    } catch (DataAccessResourceFailureException ex) {
        // expected
        assertEquals(jcex, ex.getCause());
        assertTrue(ex.getMessage().indexOf("mymsg") != -1);
    }

    final SQLGrammarException sgex = new SQLGrammarException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw sgex;
            }
        });
        fail("Should have thrown InvalidDataAccessResourceUsageException");
    } catch (InvalidDataAccessResourceUsageException ex) {
        // expected
        assertEquals(sgex, ex.getCause());
        assertTrue(ex.getMessage().indexOf("mymsg") != -1);
    }

    final LockAcquisitionException laex = new LockAcquisitionException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw laex;
            }
        });
        fail("Should have thrown CannotAcquireLockException");
    } catch (CannotAcquireLockException ex) {
        // expected
        assertEquals(laex, ex.getCause());
        assertTrue(ex.getMessage().indexOf("mymsg") != -1);
    }

    final ConstraintViolationException cvex = new ConstraintViolationException("mymsg", sqlEx, "myconstraint");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw cvex;
            }
        });
        fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
        // expected
        assertEquals(cvex, ex.getCause());
        assertTrue(ex.getMessage().indexOf("mymsg") != -1);
    }

    final DataException dex = new DataException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw dex;
            }
        });
        fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
        // expected
        assertEquals(dex, ex.getCause());
        assertTrue(ex.getMessage().indexOf("mymsg") != -1);
    }

    final JDBCException jdex = new JDBCException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw jdex;
            }
        });
        fail("Should have thrown HibernateJdbcException");
    } catch (HibernateJdbcException ex) {
        // expected
        assertEquals(jdex, ex.getCause());
        assertTrue(ex.getMessage().indexOf("mymsg") != -1);
    }

    final PropertyValueException pvex = new PropertyValueException("mymsg", "myentity", "myproperty");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw pvex;
            }
        });
        fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
        // expected
        assertEquals(pvex, ex.getCause());
        assertTrue(ex.getMessage().indexOf("mymsg") != -1);
    }

    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw new PersistentObjectException("");
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException ex) {
        // expected
    }

    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw new TransientObjectException("");
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException ex) {
        // expected
    }

    final ObjectDeletedException odex = new ObjectDeletedException("msg", "id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw odex;
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException ex) {
        // expected
        assertEquals(odex, ex.getCause());
    }

    final QueryException qex = new QueryException("msg");
    qex.setQueryString("query");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw qex;
            }
        });
        fail("Should have thrown InvalidDataAccessResourceUsageException");
    } catch (HibernateQueryException ex) {
        // expected
        assertEquals(qex, ex.getCause());
        assertEquals("query", ex.getQueryString());
    }

    final UnresolvableObjectException uoex = new UnresolvableObjectException("id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw uoex;
            }
        });
        fail("Should have thrown HibernateObjectRetrievalFailureException");
    } catch (HibernateObjectRetrievalFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(uoex, ex.getCause());
    }

    final ObjectNotFoundException onfe = new ObjectNotFoundException("id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw onfe;
            }
        });
        fail("Should have thrown HibernateObjectRetrievalFailureException");
    } catch (HibernateObjectRetrievalFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(onfe, ex.getCause());
    }

    final WrongClassException wcex = new WrongClassException("msg", "id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw wcex;
            }
        });
        fail("Should have thrown HibernateObjectRetrievalFailureException");
    } catch (HibernateObjectRetrievalFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(wcex, ex.getCause());
    }

    final NonUniqueResultException nuex = new NonUniqueResultException(2);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw nuex;
            }
        });
        fail("Should have thrown IncorrectResultSizeDataAccessException");
    } catch (IncorrectResultSizeDataAccessException ex) {
        // expected
        assertEquals(1, ex.getExpectedSize());
        assertEquals(-1, ex.getActualSize());
    }

    final StaleObjectStateException sosex = new StaleObjectStateException(TestBean.class.getName(), "id");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw sosex;
            }
        });
        fail("Should have thrown HibernateOptimisticLockingFailureException");
    } catch (HibernateOptimisticLockingFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(sosex, ex.getCause());
    }

    final StaleStateException ssex = new StaleStateException("msg");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw ssex;
            }
        });
        fail("Should have thrown HibernateOptimisticLockingFailureException");
    } catch (HibernateOptimisticLockingFailureException ex) {
        // expected
        assertNull(ex.getPersistentClassName());
        assertNull(ex.getIdentifier());
        assertEquals(ssex, ex.getCause());
    }

    final HibernateException hex = new HibernateException("msg");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                throw hex;
            }
        });
        fail("Should have thrown HibernateSystemException");
    } catch (HibernateSystemException ex) {
        // expected
        assertEquals(hex, ex.getCause());
    }
}

From source file:org.springframework.orm.hibernate4.HibernateTemplateTests.java

License:Apache License

@Test
public void testExceptions() {
    SQLException sqlEx = new SQLException("argh", "27");

    final JDBCConnectionException jcex = new JDBCConnectionException("mymsg", sqlEx);
    try {// w ww . ja  va  2 s  .  c  o  m
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw jcex;
            }
        });
        fail("Should have thrown DataAccessResourceFailureException");
    } catch (DataAccessResourceFailureException ex) {
        // expected
        assertEquals(jcex, ex.getCause());
        assertTrue(ex.getMessage().contains("mymsg"));
    }

    final SQLGrammarException sgex = new SQLGrammarException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw sgex;
            }
        });
        fail("Should have thrown InvalidDataAccessResourceUsageException");
    } catch (InvalidDataAccessResourceUsageException ex) {
        // expected
        assertEquals(sgex, ex.getCause());
        assertTrue(ex.getMessage().contains("mymsg"));
    }

    final LockAcquisitionException laex = new LockAcquisitionException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw laex;
            }
        });
        fail("Should have thrown CannotAcquireLockException");
    } catch (CannotAcquireLockException ex) {
        // expected
        assertEquals(laex, ex.getCause());
        assertTrue(ex.getMessage().contains("mymsg"));
    }

    final ConstraintViolationException cvex = new ConstraintViolationException("mymsg", sqlEx, "myconstraint");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw cvex;
            }
        });
        fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
        // expected
        assertEquals(cvex, ex.getCause());
        assertTrue(ex.getMessage().contains("mymsg"));
    }

    final DataException dex = new DataException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw dex;
            }
        });
        fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
        // expected
        assertEquals(dex, ex.getCause());
        assertTrue(ex.getMessage().contains("mymsg"));
    }

    final JDBCException jdex = new JDBCException("mymsg", sqlEx);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw jdex;
            }
        });
        fail("Should have thrown HibernateJdbcException");
    } catch (HibernateJdbcException ex) {
        // expected
        assertEquals(jdex, ex.getCause());
        assertTrue(ex.getMessage().contains("mymsg"));
    }

    final PropertyValueException pvex = new PropertyValueException("mymsg", "myentity", "myproperty");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw pvex;
            }
        });
        fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
        // expected
        assertEquals(pvex, ex.getCause());
        assertTrue(ex.getMessage().contains("mymsg"));
    }

    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw new PersistentObjectException("");
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException ex) {
        // expected
    }

    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw new TransientObjectException("");
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException ex) {
        // expected
    }

    final ObjectDeletedException odex = new ObjectDeletedException("msg", "id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw odex;
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException ex) {
        // expected
        assertEquals(odex, ex.getCause());
    }

    final QueryException qex = new QueryException("msg", "query");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw qex;
            }
        });
        fail("Should have thrown InvalidDataAccessResourceUsageException");
    } catch (HibernateQueryException ex) {
        // expected
        assertEquals(qex, ex.getCause());
        assertEquals("query", ex.getQueryString());
    }

    final UnresolvableObjectException uoex = new UnresolvableObjectException("id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw uoex;
            }
        });
        fail("Should have thrown HibernateObjectRetrievalFailureException");
    } catch (HibernateObjectRetrievalFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(uoex, ex.getCause());
    }

    final ObjectNotFoundException onfe = new ObjectNotFoundException("id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw onfe;
            }
        });
        fail("Should have thrown HibernateObjectRetrievalFailureException");
    } catch (HibernateObjectRetrievalFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(onfe, ex.getCause());
    }

    final WrongClassException wcex = new WrongClassException("msg", "id", TestBean.class.getName());
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw wcex;
            }
        });
        fail("Should have thrown HibernateObjectRetrievalFailureException");
    } catch (HibernateObjectRetrievalFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(wcex, ex.getCause());
    }

    final NonUniqueResultException nuex = new NonUniqueResultException(2);
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw nuex;
            }
        });
        fail("Should have thrown IncorrectResultSizeDataAccessException");
    } catch (IncorrectResultSizeDataAccessException ex) {
        // expected
        assertEquals(1, ex.getExpectedSize());
        assertEquals(-1, ex.getActualSize());
    }

    final StaleObjectStateException sosex = new StaleObjectStateException(TestBean.class.getName(), "id");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw sosex;
            }
        });
        fail("Should have thrown HibernateOptimisticLockingFailureException");
    } catch (HibernateOptimisticLockingFailureException ex) {
        // expected
        assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
        assertEquals("id", ex.getIdentifier());
        assertEquals(sosex, ex.getCause());
    }

    final StaleStateException ssex = new StaleStateException("msg");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw ssex;
            }
        });
        fail("Should have thrown HibernateOptimisticLockingFailureException");
    } catch (HibernateOptimisticLockingFailureException ex) {
        // expected
        assertNull(ex.getPersistentClassName());
        assertNull(ex.getIdentifier());
        assertEquals(ssex, ex.getCause());
    }

    final HibernateException hex = new HibernateException("msg");
    try {
        hibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) {
                throw hex;
            }
        });
        fail("Should have thrown HibernateSystemException");
    } catch (HibernateSystemException ex) {
        // expected
        assertEquals(hex, ex.getCause());
    }
}

From source file:podd.dataaccess.fedora.DeadlockHibernatePoddObjectDAOImpl.java

License:Open Source License

@Override
@Idempotent/*from w w  w  .jav  a2 s . c o  m*/
public void save(PoddObject object) throws DataAccessException {
    throw new LockAcquisitionException("Exception thrown.", new BatchUpdateException());
}

From source file:podd.dataaccess.fedora.DeadlockHibernatePoddObjectDAOImpl.java

License:Open Source License

@Override
@Idempotent// w w  w  .  j av a2s  . c om
public PoddObject load(Long id) throws DataAccessException {
    throw new LockAcquisitionException("Exception thrown.", new BatchUpdateException());
}

From source file:podd.dataaccess.fedora.DeadlockHibernatePoddObjectDAOImpl.java

License:Open Source License

@Override
@Idempotent//from  ww w. j  av  a  2  s .com
public PoddObject load(String pid) throws DataAccessException {
    throw new LockAcquisitionException("Exception thrown.", new BatchUpdateException());
}