Java 类org.hibernate.dialect.lock.PessimisticEntityLockException 实例源码

项目:high-performance-java-persistence    文件:LockModePessimisticReadWriteIntegrationTest.java   
@Test
public void testPessimisticNoWait() {
    LOGGER.info("Test PESSIMISTIC_READ blocks PESSIMISTIC_WRITE, NO WAIT fails fast");
    Post post = doInJPA(entityManager -> {
        return entityManager.find(Post.class, 1L);
    });

    doInJPA(entityManager -> {
        entityManager.unwrap( Session.class ).lock(post, LockMode.PESSIMISTIC_WRITE);

        executeSync( () -> {
            doInJPA(_entityManager -> {
                try {
                    _entityManager
                    .unwrap(Session.class)
                    .buildLockRequest(
                        new LockOptions(LockMode.PESSIMISTIC_WRITE)
                        .setTimeOut(LockOptions.NO_WAIT))
                    .lock(post);
                    fail("Should throw PessimisticEntityLockException");
                }
                catch (PessimisticEntityLockException expected) {
                    //This is expected since the first transaction already acquired this lock
                }
            });
        } );
    });
}