Java Code Examples for org.springframework.dao.OptimisticLockingFailureException

The following examples show how to use org.springframework.dao.OptimisticLockingFailureException. These examples are extracted from open source projects. 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 Project: sdn-rx   Source File: ReactiveNeo4jTemplate.java    License: Apache License 2.0 6 votes vote down vote up
private <Y> Mono<Long> saveRelatedNode(Object relatedNode, Class<Y> entityType, NodeDescription targetNodeDescription,
	@Nullable String inDatabase) {

	return determineDynamicLabels((Y) relatedNode, (Neo4jPersistentEntity<?>) targetNodeDescription, inDatabase)
		.flatMap(t -> {
			Y entity = t.getT1();
			DynamicLabels dynamicLabels = t.getT2();

			return neo4jClient.query(() -> renderer.render(
				cypherGenerator.prepareSaveOf(targetNodeDescription, dynamicLabels)))
				.in(inDatabase)
				.bind((Y) entity)
				.with(neo4jMappingContext.getRequiredBinderFunctionFor(entityType))
				.fetchAs(Long.class).one();
		})
		.switchIfEmpty(Mono.defer(() -> {
			if (((Neo4jPersistentEntity) targetNodeDescription).hasVersionProperty()) {
				return Mono.error(() -> new OptimisticLockingFailureException(OPTIMISTIC_LOCKING_ERROR_MESSAGE));
			}
			return Mono.empty();
		}));
}
 
Example 2
Source Project: sdn-rx   Source File: Neo4jTemplate.java    License: Apache License 2.0 6 votes vote down vote up
private <Y> Long saveRelatedNode(Object entity, Class<Y> entityType, NodeDescription targetNodeDescription, @Nullable String inDatabase) {

		DynamicLabels dynamicLabels = determineDynamicLabels(entity, (Neo4jPersistentEntity) targetNodeDescription, inDatabase);
		Optional<Long> optionalSavedNodeId = neo4jClient
			.query(() -> renderer
				.render(cypherGenerator.prepareSaveOf(targetNodeDescription, dynamicLabels)))
			.in(inDatabase)
			.bind((Y) entity).with(neo4jMappingContext.getRequiredBinderFunctionFor(entityType))
			.fetchAs(Long.class).one();

		if (((Neo4jPersistentEntity) targetNodeDescription).hasVersionProperty() && !optionalSavedNodeId.isPresent()) {
			throw new OptimisticLockingFailureException(OPTIMISTIC_LOCKING_ERROR_MESSAGE);
		}

		return optionalSavedNodeId.get();
	}
 
Example 3
/**
 * Give it a number of tries to update the event/meeting object into DB
 * storage if this still satisfy the pre-condition regardless some changes
 * in DB storage
 * 
 * @param meeting
 *            a SignupMeeting object.
 * @param currentTimeslot
 *            a SignupTimeslot object.
 * @param cancelAction
 *            a boolean value
 * @return a SignupMeeting object, which is a refreshed updat-to-date data.
 * @throws Exception
 *             throw if anything goes wrong.
 */
private SignupMeeting handleVersion(SignupMeeting meeting, SignupTimeslot currentTimeslot, boolean cancelAction)
		throws Exception {
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {				
			meeting = signupMeetingService.loadSignupMeeting(meeting.getId(), userId, siteId);
			this.signupEventTrackingInfo = new SignupEventTrackingInfoImpl();
			this.signupEventTrackingInfo.setMeeting(meeting);
			currentTimeslot = meeting.getTimeslot(currentTimeslot.getId());
			if (currentTimeslot.isCanceled() == cancelAction)
				throw new SignupUserActionException(Utilities.rb
						.getString("someone.already.changed.ts.cancel_status"));

			cancel(currentTimeslot, cancelAction);
			signupMeetingService.updateSignupMeeting(meeting,isOrganizer);
			return meeting;
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}

	throw new SignupUserActionException(Utilities.rb.getString("failed.cancel_or_restore_ts"));
}
 
Example 4
Source Project: sakai   Source File: EditComment.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * 
 * @param newComment
 * 			takes in a string new comment
 * @return
 * @throws Exception
 */
public SignupMeeting updateComment(String newComment) throws Exception {
	this.modifiedComment = newComment;
	boolean isOrganizer = originalMeeting.getPermission().isUpdate();
	String currentUserId = sakaiFacade.getCurrentUserId();
	
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {
			SignupMeeting upToDateMeeting = signupMeetingService.loadSignupMeeting(originalMeeting.getId(), currentUserId, siteId);
			checkPrecondition(upToDateMeeting, relatedTimeslotId);
			if(isOrganizer)
				upToDateMeeting.setPermission(originalMeeting.getPermission());
			signupMeetingService.updateSignupMeeting(upToDateMeeting, isOrganizer);
			upToDateMeeting = signupMeetingService.loadSignupMeeting(originalMeeting.getId(), currentUserId, siteId);
			
			return upToDateMeeting;
			
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}
	throw new SignupUserActionException(Utilities.rb.getString("someone.already.updated.db"));
}
 
Example 5
@Test
public void transactionExceptionPropagatedWithCallbackPreference() throws Throwable {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(exceptionalMethod, txatt);

	MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager();

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	checkTransactionStatus(false);
	try {
		itb.exceptional(new OptimisticLockingFailureException(""));
		fail("Should have thrown OptimisticLockingFailureException");
	}
	catch (OptimisticLockingFailureException ex) {
		// expected
	}
	checkTransactionStatus(false);

	assertSame(txatt, ptm.getDefinition());
	assertFalse(ptm.getStatus().isRollbackOnly());
}
 
Example 6
Source Project: sakai   Source File: LockUnlockTimeslot.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Give it a number of tries to update the event/meeting object into DB
 * storage if this still satisfy the pre-condition regardless some changes
 * in DB storage
 * 
 * @param meeting
 *            a SignupMeeting object.
 * @param currentTimeslot
 *            a SignupTimeslot object.
 * @param lockAction
 *            a boolean value
 * @return a SignupMeeting object, which is a refreshed updat-to-date data.
 * @throws Exception
 *             throw if anything goes wrong.
 */
private SignupMeeting handleVersion(SignupMeeting meeting, SignupTimeslot currentTimeslot, boolean lockAction)
		throws Exception {
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {
			meeting = signupMeetingService.loadSignupMeeting(meeting.getId(), userId, siteId);
			currentTimeslot = meeting.getTimeslot(currentTimeslot.getId());
			if (currentTimeslot.isLocked() == lockAction)
				throw new SignupUserActionException(Utilities.rb
						.getString("someone.already.changed.ts.lock_status"));

			currentTimeslot.setLocked(lockAction);

			signupMeetingService.updateSignupMeeting(meeting,isOrganizer);
			return meeting;
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}

	throw new SignupUserActionException(Utilities.rb.getString("failed.lock_or_unlock_ts"));
}
 
Example 7
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 8
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).get();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setName("n2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ProductEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("n1", updatedEntity.getName());
}
 
Example 9
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 10
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).block();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    StepVerifier.create(repository.save(entity2)).expectError(OptimisticLockingFailureException.class).verify();

    // Get the updated entity from the database and verify its new sate
    StepVerifier.create(repository.findById(savedEntity.getId()))
        .expectNextMatches(foundEntity ->
            foundEntity.getVersion() == 1 &&
            foundEntity.getName().equals("n1"))
        .verifyComplete();
}
 
Example 11
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    RecommendationEntity entity1 = repository.findById(savedEntity.getId()).block();
    RecommendationEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2).block();

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    RecommendationEntity updatedEntity = repository.findById(savedEntity.getId()).block();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 12
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 13
Source Project: sakai   Source File: RemoveWaiter.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Give it a number of tries to update the event/meeting object into DB
 * storage if this still satisfy the pre-condition regardless some changes
 * in DB storage
 */
private void handleVersion(SignupMeeting meeting, SignupTimeslot timeslot, SignupAttendee waiter) throws Exception {
	boolean success = false;
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {
			meeting = reloadMeeting(meeting.getId());
			prepareRemoveFromWaitingList(meeting, timeslot, waiter);
			signupMeetingService.updateSignupMeeting(meeting, isOrganizer);
			success = true;
			break; // add attendee is successful
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}
	if (!success)
		throw new SignupUserActionException(Utilities.rb.getString("someone.already.updated.db"));

}
 
Example 14
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    RecommendationEntity entity1 = repository.findById(savedEntity.getId()).block();
    RecommendationEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2).block();

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    RecommendationEntity updatedEntity = repository.findById(savedEntity.getId()).block();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 15
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 16
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).block();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    StepVerifier.create(repository.save(entity2)).expectError(OptimisticLockingFailureException.class).verify();

    // Get the updated entity from the database and verify its new sate
    StepVerifier.create(repository.findById(savedEntity.getId()))
        .expectNextMatches(foundEntity ->
            foundEntity.getVersion() == 1 &&
            foundEntity.getName().equals("n1"))
        .verifyComplete();
}
 
Example 17
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    RecommendationEntity entity1 = repository.findById(savedEntity.getId()).block();
    RecommendationEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2).block();

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    RecommendationEntity updatedEntity = repository.findById(savedEntity.getId()).block();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 18
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 19
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).block();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    StepVerifier.create(repository.save(entity2)).expectError(OptimisticLockingFailureException.class).verify();

    // Get the updated entity from the database and verify its new sate
    StepVerifier.create(repository.findById(savedEntity.getId()))
        .expectNextMatches(foundEntity ->
            foundEntity.getVersion() == 1 &&
            foundEntity.getName().equals("n1"))
        .verifyComplete();
}
 
Example 20
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 21
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).block();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    StepVerifier.create(repository.save(entity2)).expectError(OptimisticLockingFailureException.class).verify();

    // Get the updated entity from the database and verify its new sate
    StepVerifier.create(repository.findById(savedEntity.getId()))
        .expectNextMatches(foundEntity ->
            foundEntity.getVersion() == 1 &&
            foundEntity.getName().equals("n1"))
        .verifyComplete();
}
 
Example 22
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    RecommendationEntity entity1 = repository.findById(savedEntity.getId()).block();
    RecommendationEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2).block();

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    RecommendationEntity updatedEntity = repository.findById(savedEntity.getId()).block();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 23
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 24
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).block();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    StepVerifier.create(repository.save(entity2)).expectError(OptimisticLockingFailureException.class).verify();

    // Get the updated entity from the database and verify its new sate
    StepVerifier.create(repository.findById(savedEntity.getId()))
        .expectNextMatches(foundEntity ->
            foundEntity.getVersion() == 1 &&
            foundEntity.getName().equals("n1"))
        .verifyComplete();
}
 
Example 25
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    RecommendationEntity entity1 = repository.findById(savedEntity.getId()).block();
    RecommendationEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2).block();

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    RecommendationEntity updatedEntity = repository.findById(savedEntity.getId()).block();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 26
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 27
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).block();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    StepVerifier.create(repository.save(entity2)).expectError(OptimisticLockingFailureException.class).verify();

    // Get the updated entity from the database and verify its new sate
    StepVerifier.create(repository.findById(savedEntity.getId()))
        .expectNextMatches(foundEntity ->
            foundEntity.getVersion() == 1 &&
            foundEntity.getName().equals("n1"))
        .verifyComplete();
}
 
Example 28
Source Project: sakai   Source File: RemoveWaiter.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Give it a number of tries to update the event/meeting object into DB
 * storage if this still satisfy the pre-condition regardless some changes
 * in DB storage
 */
private void handleVersion(SignupMeeting meeting, SignupTimeslot timeslot, SignupAttendee waiter) throws Exception {
	boolean success = false;
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {
			meeting = reloadMeeting(meeting.getId());
			prepareRemoveFromWaitingList(meeting, timeslot, waiter);
			signupMeetingService.updateSignupMeeting(meeting, isOrganizer);
			success = true;
			break; // add attendee is successful
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}
	if (!success)
		throw new SignupUserActionException(Utilities.rb.getString("someone.already.updated.db"));

}
 
Example 29
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ReviewEntity entity1 = repository.findById(savedEntity.getId()).get();
    ReviewEntity entity2 = repository.findById(savedEntity.getId()).get();

    // Update the entity using the first entity object
    entity1.setAuthor("a1");
    repository.save(entity1);

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    try {
        entity2.setAuthor("a2");
        repository.save(entity2);

        fail("Expected an OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {}

    // Get the updated entity from the database and verify its new sate
    ReviewEntity updatedEntity = repository.findById(savedEntity.getId()).get();
    assertEquals(1, (int)updatedEntity.getVersion());
    assertEquals("a1", updatedEntity.getAuthor());
}
 
Example 30
@Test
public void optimisticLockError() {

    // Store the saved entity in two separate entity objects
    ProductEntity entity1 = repository.findById(savedEntity.getId()).block();
    ProductEntity entity2 = repository.findById(savedEntity.getId()).block();

    // Update the entity using the first entity object
    entity1.setName("n1");
    repository.save(entity1).block();

    //  Update the entity using the second entity object.
    // This should fail since the second entity now holds a old version number, i.e. a Optimistic Lock Error
    StepVerifier.create(repository.save(entity2)).expectError(OptimisticLockingFailureException.class).verify();

    // Get the updated entity from the database and verify its new sate
    StepVerifier.create(repository.findById(savedEntity.getId()))
        .expectNextMatches(foundEntity ->
            foundEntity.getVersion() == 1 &&
            foundEntity.getName().equals("n1"))
        .verifyComplete();
}