Search code examples
sap-cloud-sdk

SAP Java SDK OData results are sometimes multiplied


I have a small JAVA BTP app that is supposed to read master data from the SAP BP. Most of the time it works very well.

However, I have a small problem with the partner functions. For some reason, the results is multiplied with each call. First I have correctly 6 partner functions, then 12, then 24 etc..

After 1-2 hours I have 6 correct results again.

My call in principle:

private void getBusinessPartnerCustomerSalesAreas() {
    Customer customer = this.businessPartner.getCustomerOrFetch();
    if (customer == null) {
        return;
    }

    this.businessPartnerCustomerSalesArea
            .addAll(customer.getCustomerSalesAreaOrFetch());

    this.getPartnerFunctions();
}

private void getPartnerFunctions() {
    this.businessPartnerCustomerSalesArea.forEach(c -> {
        this.custSalesPartnerFunctions.addAll(c.getPartnerFunctionOrFetch());
    });
}
// ...
    // partner functions output mapping
    if (!this.custSalesPartnerFunctions.isEmpty()) {
        List<CustfactsheetPartnerFunction> partnerFunctions = new ArrayList<>();
        // map partner functions to output format
        this.custSalesPartnerFunctions.forEach(p -> {
            CustfactsheetPartnerFunction partnerFunction = CustfactsheetPartnerFunction.create();
            partnerFunction.setFunction(p.getPartnerFunction());
            partnerFunction.setCustomer(p.getCustomer());
            partnerFunction.setCustomerName(p.getCustomerPartnerDescription());
            partnerFunction.setIsDefault(p.getDefaultPartner());
            partnerFunctions.add(partnerFunction);
        });
        
        returnStruc.setPartnerFunctions(partnerFunctions);
    }

Is there a caching somewhere which I don't see? Other arrays (addresses for example) are not multiplied... I use the newest SDK version. Probably I miss again only a point or so... Thx.


Solution

  • I'm not aware of any implicit caching of OData results.

    Most likely your code maintains multiple copies of similar objects.

    • Please check whether you populate this.businessPartnerCustomerSalesArea for every incoming request. It should be instantiated with every servlet call. Make sure to avoid duplicates, e.g. by leveraging HashSet implementation for your collection. It looks like you are filling it from 1:n navigation properties. Without knowing the service specification, I would assume a chance for duplicates.

    Or in case you use Spring Boot, maybe there is unexpected proxying / caching on your application.

    • If you are using @Component in the affected code, please make sure to configure the correct scope annotation argument.
    • If you are using @Cached or something, this would be an obvious case to check.
    • If you are using dependency injection, you could try to instead load concrete implementations, hard-coded. Maybe this improves the situation, and you'll know the cause.