@Test
  public void testBuildLockRequest() {
    doInJPA(
        this::entityManagerFactory,
        entityManager -> {
          log.info("testBuildLockRequest");
          Person person = new Person("John Doe");
          Phone home = new Phone("123-456-7890");
          Phone office = new Phone("098-765-4321");
          person.getPhones().add(home);
          person.getPhones().add(office);
          entityManager.persist(person);
          entityManager.flush();
        });
    doInJPA(
        this::entityManagerFactory,
        entityManager -> {
          Long id = 1L;
          // tag::locking-buildLockRequest-example[]
          Person person = entityManager.find(Person.class, id);
          Session session = entityManager.unwrap(Session.class);
          session
              .buildLockRequest(LockOptions.NONE)
              .setLockMode(LockMode.PESSIMISTIC_READ)
              .setTimeOut(LockOptions.NO_WAIT)
              .lock(person);
          // end::locking-buildLockRequest-example[]
        });

    doInJPA(
        this::entityManagerFactory,
        entityManager -> {
          Long id = 1L;
          // tag::locking-buildLockRequest-scope-example[]
          Person person = entityManager.find(Person.class, id);
          Session session = entityManager.unwrap(Session.class);
          session
              .buildLockRequest(LockOptions.NONE)
              .setLockMode(LockMode.PESSIMISTIC_READ)
              .setTimeOut(LockOptions.NO_WAIT)
              .setScope(true)
              .lock(person);
          // end::locking-buildLockRequest-scope-example[]
        });
  }
Example #2
0
  @Test
  public void test5lock3() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    int id = 1;
    User user = (User) session.get(User.class, id);
    session
        .buildLockRequest(LockOptions.UPGRADE)
        .setLockMode(LockMode.PESSIMISTIC_WRITE)
        .setTimeOut(60000)
        .lock(user);
    System.out.println(user);

    User user2 = (User) session.get(User.class, id);
    System.out.println(user2);

    tx.commit();
    session.close();
  }
Example #3
0
 public LockRequest buildLockRequest(LockOptions lockOptions) {
   return session.buildLockRequest(lockOptions);
 }