javax.persistence.EntityManager.lock()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(303)

本文整理了Java中javax.persistence.EntityManager.lock()方法的一些代码示例,展示了EntityManager.lock()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager.lock()方法的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
方法名:lock

EntityManager.lock介绍

[英]Lock an entity instance that is contained in the persistence context with the specified lock mode type.

If a pessimistic lock mode type is specified and the entity contains a version attribute, the persistence provider must also perform optimistic version checks when obtaining the database lock. If these checks fail, the OptimisticLockException will be thrown.

If the lock mode type is pessimistic and the entity instance is found but cannot be locked:

  • the PessimisticLockException will be thrown if the database locking failure causes transaction-level rollback
  • the LockTimeoutException will be thrown if the database locking failure causes only statement-level rollback
    [中]使用指定的锁定模式类型锁定持久性上下文中包含的实体实例。
    如果指定了悲观锁模式类型,并且实体包含版本属性,则持久性提供程序在获取数据库锁时还必须执行乐观版本检查。如果这些检查失败,将抛出OptimisticLockException
    如果锁定模式类型为悲观,并且找到实体实例但无法锁定:
    *如果数据库锁定失败导致事务级回滚,将抛出PessimisticLockException
    *如果数据库锁定失败仅导致语句级回滚,则将抛出LockTimeoutException

代码示例

代码示例来源:origin: rapidoid/rapidoid

@Override
public void lock(Object entity, LockModeType lockMode) {
  em.lock(entity, lockMode);
}

代码示例来源:origin: rapidoid/rapidoid

@Override
public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
  em.lock(entity, lockMode, properties);
}

代码示例来源:origin: rapidoid/rapidoid

@Override
public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
  em().lock(entity, lockMode, properties);
}

代码示例来源:origin: rapidoid/rapidoid

@Override
public void lock(Object entity, LockModeType lockMode) {
  em().lock(entity, lockMode);
}

代码示例来源:origin: Impetus/Kundera

em.lock(null, LockModeType.NONE, null);
Assert.fail("Should have gone to catch block!");

代码示例来源:origin: org.evolvis.bsi/kolab-ws

@Override
public void
lock(Object entity, LockModeType lockMode)
{
  delegate.lock(entity, lockMode);
}

代码示例来源:origin: aerogear/aerogear-unifiedpush-server

/**
 * Pessimistic write lock on entity
 */
@Override
public void lock(T entity) {
  entityManager.lock(entity, LockModeType.PESSIMISTIC_WRITE);
}

代码示例来源:origin: BottegaIT/ddd-leaven-v2

public void save(A aggregate) {
  if (entityManager.contains(aggregate)){
    //locking Aggregate Root logically protects whole aggregate
    entityManager.lock(aggregate, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
  }
  else{
    entityManager.persist(aggregate);
  }
}

代码示例来源:origin: org.apache.deltaspike.modules/deltaspike-data-module-impl

@Override
public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties)
{
  entityManager().lock(entity, lockMode, properties);
}

代码示例来源:origin: org.jboss.seam/jboss-seam

protected void lock(Object entity, LockModeType lockMode)
{
 getEntityManager().lock(entity, lockMode);
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testPessimisticNoWaitJPA() throws InterruptedException {
  LOGGER.info("Test PESSIMISTIC_READ blocks PESSIMISTIC_WRITE, NO WAIT fails fast");
  doInJPA(entityManager -> {
    Post post = entityManager.find(Post.class, 1L);
    entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE,
      Collections.singletonMap("javax.persistence.lock.timeout", 0)
    );
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testPessimisticTimeoutJPA() throws InterruptedException {
  doInJPA(entityManager -> {
    Post post = entityManager.find(Post.class, 1L);
    entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE,
      Collections.singletonMap("javax.persistence.lock.timeout",
        TimeUnit.SECONDS.toMillis(3))
    );
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testCascadeLockOnManagedEntityWithJPA() throws InterruptedException {
  LOGGER.info("Test lock cascade for managed entity");
  doInJPA(entityManager -> {
    Post post = entityManager.find(Post.class, 1L);
    entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap(
      AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED
    ));
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testCascadeLockOnManagedEntityWithJPA() throws InterruptedException {
  LOGGER.info("Test lock cascade for managed entity");
  doInJPA(entityManager -> {
    Post post = entityManager.find(Post.class, 1L);
    entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap(
      AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED
    ));
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testCascadeLockOnManagedEntityWithAssociationsUninitializedAndJpa() throws InterruptedException {
  LOGGER.info("Test lock cascade for managed entity");
  doInJPA(entityManager -> {
    Post post = entityManager.find(Post.class, 1L);
    entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap(
      AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED
    ));
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testCascadeLockOnManagedEntityWithJPA() throws InterruptedException {
  LOGGER.info("Test lock cascade for managed entity");
  doInJPA(entityManager -> {
    Post post = entityManager.find(Post.class, 1L);
    entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap(
      AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED
    ));
  });
}

代码示例来源:origin: org.apache.openejb/openejb-core

public void lock(final Object entity, final LockModeType lockMode) {
  assertTransactionActive();
  final Timer timer = Op.lock.start(this);
  try {
    getEntityManager().lock(entity, lockMode);
  } finally {
    timer.stop();
  }
}

代码示例来源:origin: org.apache.openejb/openejb-core

public void lock(final Object entity, final LockModeType lockMode, final Map<String, Object> properties) {
  assertTransactionActive();
  final Timer timer = Op.lock.start(this);
  try {
    getEntityManager().lock(entity, lockMode, properties);
  } finally {
    timer.stop();
  }
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testPessimisticWriteAfterFetch() {
  doInJPA(entityManager -> {
    Post post = entityManager.find(Post.class, 1L);
    entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE);
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testPessimisticWriteAfterFetchWithDetachedForJPA() {
  Post post = doInJPA(entityManager -> {
    return entityManager.find(Post.class, 1L);
  });
  try {
    doInJPA(entityManager -> {
      entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE);
    });
  } catch (IllegalArgumentException e) {
    assertEquals("entity not in the persistence context", e.getMessage());
  }
}

相关文章