spring-attic / spring-cloud-gcp

Integration for Google Cloud Platform APIs with Spring
Apache License 2.0
704 stars 693 forks source link

datastore: BeforeDeleteEvent is not fired if deletion was performed inside of performTransaction method #2670

Closed gendolf3d closed 3 years ago

gendolf3d commented 3 years ago

Describe the bug BeforeDeleteEvent is not fired if deletion was performed inside of performTransaction method.

Sample Having the following code to delete entity by Key:

@RequiredArgsConstructor
@Service
public class MyEntityClass {
    private final DatastoreTemplate datastoreTemplate;

    public void deleteRole(Key key) {
        datastoreTemplate.performTransaction(transactionalRepo -> {
            transactionalRepo.deleteById(key, MyEntity.class);
            return null;
        });
    }
}

Please note that deletion is performed inside of performTransaction method and the code below has been never called:

@Slf4j
@Component
public class DeleteEventHandler {

    @EventListener
    public void handleBeforeDeleteEvent(BeforeDeleteEvent event) {
        if (event == null) {
            log.debug("Received BeforeDeleteEvent null");
            return;
        }

        for (Key key : event.getKeys()) {
            // do some stuff
            log.info("The entity with key: {} is going to be deteted", key);
        }
    }
}

Note:

  1. This issue is reproducible for all kind events. Tested on thse:
    BeforeSaveEvent
    AfterSaveEvent
    BeforeDeleteEven
    AfterDeleteEvent
  2. The same issue if call performTransaction on Datastore Repository
  3. The issue is not reproducible if method annotated as @Transactional and inside of this there is no performTransaction
gendolf3d commented 3 years ago

Checked the code and issue has been fixed since 1.2.7.RELEASE