类org.hibernate.dialect.lock.OptimisticEntityLockException源码实例Demo

下面列出了怎么用org.hibernate.dialect.lock.OptimisticEntityLockException的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
public void testExplicitOptimisticLocking() throws InterruptedException {
    try {
        doInJPA(entityManager -> {
            try {
                final Post post = entityManager.find(Post.class, 1L, LockModeType.OPTIMISTIC);
                PostComment comment = new PostComment();
                comment.setId(1L);
                comment.setReview("Good one.");
                comment.setPost(post);
                lockUpgrade(entityManager, post);
                ready.set(true);
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (OptimisticEntityLockException expected) {
        LOGGER.info("Failure: ", expected);
    }
    endLatch.await();
}
 
@Test
public void testExplicitOptimisticLocking() {

    try {
        doInTransaction(session -> {
            final Product product = (Product) session.get(Product.class, 1L, new LockOptions(LockMode.OPTIMISTIC));

            executeSync(() -> {
                doInTransaction(_session -> {
                    Product _product = (Product) _session.get(Product.class, 1L);
                    assertNotSame(product, _product);
                    _product.setPrice(BigDecimal.valueOf(14.49));
                });
            });

            OrderLine orderLine = new OrderLine(product);
            session.persist(orderLine);
        });
        fail("It should have thrown OptimisticEntityLockException!");
    } catch (OptimisticEntityLockException expected) {
        LOGGER.info("Failure: ", expected);
    }
}
 
@Test
public void testExplicitOptimisticLocking() throws InterruptedException {
    try {
        doInTransaction(session -> {
            try {
                final Product product = (Product) session.get(Product.class, 1L, new LockOptions(LockMode.OPTIMISTIC));
                OrderLine orderLine = new OrderLine(product);
                session.persist(orderLine);
                lockUpgrade(session, product);
                ready.set(true);
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (OptimisticEntityLockException expected) {
        LOGGER.info("Failure: ", expected);
    }
    endLatch.await();
}
 
public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) {
	super(ex.getMessage(), ex);
}
 
public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) {
	super(ex.getMessage(), ex);
}
 
public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) {
	super(ex.getMessage(), ex);
}
 
public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) {
	super(ex.getMessage(), ex);
}
 
public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) {
	super(ex.getMessage(), ex);
}
 
public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) {
	super(ex.getMessage(), ex);
}
 
 类所在包
 类方法
 同包方法