org.springframework.dao.OptimisticLockingFailureException Java Examples

The following examples show how to use org.springframework.dao.OptimisticLockingFailureException. 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: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #2
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #3
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #4
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #5
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #6
Source File: MoveAttendee.java    From sakai with 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 selectedAttendeeUserId
 *            an unique sakai internal user id.
 * @param selectedTimeslotId
 *            a string value
 * @return a SignupMeeting object, which is a refreshed updat-to-date data.
 * @throws Exception
 *             throw if anything goes wrong.
 * 
 */
private void handleVersion(SignupMeeting meeting, SignupTimeslot currentTimeslot, String selectedAttendeeUserId,
		String selectedTimeslotId) throws Exception {
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {
			// reset track info
			this.signupEventTrackingInfo = new SignupEventTrackingInfoImpl();
			this.signupEventTrackingInfo.setMeeting(meeting);
			meeting = signupMeetingService.loadSignupMeeting(meeting.getId(), userId, siteId);
			currentTimeslot = meeting.getTimeslot(currentTimeslot.getId());
			if (currentTimeslot.getAttendee(selectedAttendeeUserId) == null)
				throw new SignupUserActionException(Utilities.rb
						.getString("failed.move.due_to_attendee_notExisted"));

			moveAttendee(meeting, currentTimeslot, selectedAttendeeUserId, selectedTimeslotId);

			signupMeetingService.updateSignupMeeting(meeting,isOrganizer);
			return;
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}
	throw new SignupUserActionException(Utilities.rb.getString("failed.move.due_to_attendee_notExisted"));
}
 
Example #7
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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
Source File: ReplaceAttendee.java    From sakai with 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 currentTimeslot, String toBeReplacedUserId,
		SignupAttendee newAttendee) throws Exception {
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {
			this.signupEventTrackingInfo = new SignupEventTrackingInfoImpl();
			this.signupEventTrackingInfo.setMeeting(meeting);
			meeting = signupMeetingService.loadSignupMeeting(meeting.getId(), userId, siteId);
			currentTimeslot = meeting.getTimeslot(currentTimeslot.getId());
			if (currentTimeslot.getAttendee(toBeReplacedUserId) == null)
				throw new SignupUserActionException(Utilities.rb
						.getString("failed.replaced_due_to_attendee_notExisted_in_ts"));

			replace(meeting, currentTimeslot, toBeReplacedUserId, newAttendee);

			signupMeetingService.updateSignupMeeting(meeting, isOrganizer);
			return;
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}
	throw new SignupUserActionException(Utilities.rb.getString("failed.replace.someone.already.updated.db"));
}
 
Example #9
Source File: DataServiceRetryAspect.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param dataServiceRetryProperties retry properties
 */
public DataServiceRetryAspect(final DataServiceRetryProperties dataServiceRetryProperties) {
    this.retryTemplate = new RetryTemplate();
    this.retryTemplate.setRetryPolicy(
        new SimpleRetryPolicy(
            dataServiceRetryProperties.getNoOfRetries(),
            new ImmutableMap.Builder<Class<? extends Throwable>, Boolean>()
                .put(CannotGetJdbcConnectionException.class, true)
                .put(CannotAcquireLockException.class, true)
                .put(DeadlockLoserDataAccessException.class, true)
                .put(OptimisticLockingFailureException.class, true)
                .put(PessimisticLockingFailureException.class, true)
                .put(ConcurrencyFailureException.class, true)
                // Will this work for cases where the write queries timeout on the client?
                .put(QueryTimeoutException.class, true)
                .put(TransientDataAccessResourceException.class, true)
                .put(JpaSystemException.class, true)
                .build()
        )
    );
    final ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(dataServiceRetryProperties.getInitialInterval());
    backOffPolicy.setMaxInterval(dataServiceRetryProperties.getMaxInterval());
    this.retryTemplate.setBackOffPolicy(backOffPolicy);
}
 
Example #10
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #11
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #12
Source File: MoveAttendee.java    From sakai with 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 selectedAttendeeUserId
 *            an unique sakai internal user id.
 * @param selectedTimeslotId
 *            a string value
 * @return a SignupMeeting object, which is a refreshed updat-to-date data.
 * @throws Exception
 *             throw if anything goes wrong.
 * 
 */
private void handleVersion(SignupMeeting meeting, SignupTimeslot currentTimeslot, String selectedAttendeeUserId,
		String selectedTimeslotId) throws Exception {
	for (int i = 0; i < MAX_NUMBER_OF_RETRY; i++) {
		try {
			// reset track info
			this.signupEventTrackingInfo = new SignupEventTrackingInfoImpl();
			this.signupEventTrackingInfo.setMeeting(meeting);
			meeting = signupMeetingService.loadSignupMeeting(meeting.getId(), userId, siteId);
			currentTimeslot = meeting.getTimeslot(currentTimeslot.getId());
			if (currentTimeslot.getAttendee(selectedAttendeeUserId) == null)
				throw new SignupUserActionException(Utilities.rb
						.getString("failed.move.due_to_attendee_notExisted"));

			moveAttendee(meeting, currentTimeslot, selectedAttendeeUserId, selectedTimeslotId);

			signupMeetingService.updateSignupMeeting(meeting,isOrganizer);
			return;
		} catch (OptimisticLockingFailureException oe) {
			// don't do any thing
		}
	}
	throw new SignupUserActionException(Utilities.rb.getString("failed.move.due_to_attendee_notExisted"));
}
 
Example #13
Source File: AbstractTransactionAspectTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #14
Source File: LockUnlockTimeslot.java    From sakai with 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 #15
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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
Source File: RemoveWaiter.java    From sakai with 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 #18
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #19
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #20
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #21
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #23
Source File: ReactiveNeo4jTemplate.java    From sdn-rx with 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 #24
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #26
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #27
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #28
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #29
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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 #30
Source File: PersistenceTests.java    From Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud with MIT License 6 votes vote down vote up
@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());
}