javax.persistence.PessimisticLockScope Java Examples

The following examples show how to use javax.persistence.PessimisticLockScope. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 );
}
 
Example #2
Source File: PessimisticLockScopesIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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();
}
 
Example #3
Source File: PessimisticLockScopesIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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();
}
 
Example #4
Source File: PessimisticLockScopesIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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();
}
 
Example #5
Source File: HibernateCascadeLockComponentTest.java    From vladmihalcea.wordpress.com with Apache License 2.0 5 votes vote down vote up
@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;
        }
    });
}
 
Example #6
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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 );
		}
	}
}