Java源码示例:javax.persistence.PessimisticLockScope

示例1
private void setDefaultProperties() {
	properties.putIfAbsent( AvailableSettings.FLUSH_MODE, getHibernateFlushMode().name() );
	properties.putIfAbsent( JPA_LOCK_SCOPE, PessimisticLockScope.EXTENDED.name() );
	properties.putIfAbsent( JPA_LOCK_TIMEOUT, LockOptions.WAIT_FOREVER );
	properties.putIfAbsent( JPA_SHARED_CACHE_RETRIEVE_MODE, CacheModeHelper.DEFAULT_RETRIEVE_MODE );
	properties.putIfAbsent( JPA_SHARED_CACHE_STORE_MODE, CacheModeHelper.DEFAULT_STORE_MODE );
}
 
示例2
@Test
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException {
    EntityManager em = getEntityManagerWithOpenTransaction();
    PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "JOHN", "SMITH", new BigDecimal(4.5));
    em.persist(employee);
    em.getTransaction()
        .commit();
    em.close();

    // NORMAL SCOPE
    EntityManager em2 = getEntityManagerWithOpenTransaction();
    PessimisticLockingEmployee foundEmployee = em2.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
    em2.getTransaction()
        .rollback();

    // EXTENDED SCOPE
    Map<String, Object> map = new HashMap<>();
    map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);

    EntityManager em3 = getEntityManagerWithOpenTransaction();
    foundEmployee = em3.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
    em3.getTransaction()
        .rollback();

    em2.close();
    em3.close();
}
 
示例3
@Test
public void givenEntityWithElementCollection_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
    EntityManager em = getEntityManagerWithOpenTransaction();
    Address address = new Address("Poland", "Warsaw");
    Customer customer = new Customer(1L, "JOE", "DOE", Arrays.asList(address));
    em.persist(customer);
    em.getTransaction()
        .commit();
    em.close();

    // NORMAL SCOPE
    EntityManager em2 = getEntityManagerWithOpenTransaction();
    Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
    em2.getTransaction()
        .rollback();

    // EXTENDED SCOPE
    Map<String, Object> map = new HashMap<>();
    map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);

    EntityManager em3 = getEntityManagerWithOpenTransaction();
    foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
    em2.getTransaction()
        .rollback();

    em2.close();
    em3.close();
}
 
示例4
@Test
public void givenEntityWithOneToMany_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
    EntityManager em = getEntityManagerWithOpenTransaction();
    PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE");
    PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student);
    student.setCourses(Arrays.asList(course));
    em.persist(course);
    em.persist(student);
    em.getTransaction()
        .commit();
    em.close();

    // NORMAL SCOPE
    EntityManager em2 = getEntityManagerWithOpenTransaction();
    PessimisticLockingCourse foundCourse = em2.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
    em2.getTransaction()
        .rollback();

    // EXTENDED SCOPE
    Map<String, Object> map = new HashMap<>();
    map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);

    EntityManager em3 = getEntityManagerWithOpenTransaction();
    foundCourse = em3.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
    em3.getTransaction()
        .rollback();

    em2.close();
    em3.close();
}
 
示例5
@Test
public void test() {

    final Long parentId = cleanAndSaveParent();

    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Post post = entityManager.find(Post.class, parentId);
            entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap("javax.persistence.lock.scope", (Object) PessimisticLockScope.EXTENDED));
            return null;
        }
    });
}
 
示例6
private void setLockOptions(Map<String, Object> props, LockOptions options) {
	Object lockScope = props.get( JPA_LOCK_SCOPE );
	if ( lockScope instanceof String && PessimisticLockScope.valueOf( ( String ) lockScope ) == PessimisticLockScope.EXTENDED ) {
		options.setScope( true );
	}
	else if ( lockScope instanceof PessimisticLockScope ) {
		boolean extended = PessimisticLockScope.EXTENDED.equals( lockScope );
		options.setScope( extended );
	}
	else if ( lockScope != null ) {
		throw new PersistenceException( "Unable to parse " + JPA_LOCK_SCOPE + ": " + lockScope );
	}

	Object lockTimeout = props.get( JPA_LOCK_TIMEOUT );
	int timeout = 0;
	boolean timeoutSet = false;
	if ( lockTimeout instanceof String ) {
		timeout = Integer.parseInt( ( String ) lockTimeout );
		timeoutSet = true;
	}
	else if ( lockTimeout instanceof Number ) {
		timeout = ( (Number) lockTimeout ).intValue();
		timeoutSet = true;
	}
	else if ( lockTimeout != null ) {
		throw new PersistenceException( "Unable to parse " + JPA_LOCK_TIMEOUT + ": " + lockTimeout );
	}

	if ( timeoutSet ) {
		if ( timeout == LockOptions.SKIP_LOCKED ) {
			options.setTimeOut( LockOptions.SKIP_LOCKED );
		}
		else if ( timeout < 0 ) {
			options.setTimeOut( LockOptions.WAIT_FOREVER );
		}
		else if ( timeout == 0 ) {
			options.setTimeOut( LockOptions.NO_WAIT );
		}
		else {
			options.setTimeOut( timeout );
		}
	}
}