Home > front end >  Spring boot Handler dispatch failed; nested exception is java.lang.StackOverflowError when calling a
Spring boot Handler dispatch failed; nested exception is java.lang.StackOverflowError when calling a

Time:10-06

I'm using spring boot 2.2.4 with java 11.

When I use a service more than once in a rest controller in my spring boot application, it will print too many warning in the console and finally throw an exception :

WARN 21740 --- [nio-8080-exec-4] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@70823236<rs=HikariProxyResultSet@1665630462 wrapping Result set representing update count of 4>
WARN 21740 --- [nio-8080-exec-4] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@3acd228<rs=HikariProxyResultSet@368473467 wrapping Result set representing update count of 4>
ERROR 21740 --- [nio-8080-exec-4] i.m.ExceptionHandling  : Handler dispatch failed; nested exception is java.lang.StackOverflowError

This is part of the rest controller class :

@CrossOrigin
@RestController
@RequestMapping("/api/purchase")
public class PurchaseController {

    private PurchaseService purchaseService;
    
    public PurchaseController(PurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }

    @PostMapping("/create-order")
    public ResponseEntity<PurchaseResponse> createOrder() {
        Order order = purchaseService.createOrder(); // first use of service (its ok)
        String simple = purchaseService.simple(); // second use of service : this will cause the exception

        PurchaseResponse purchaseResponse = new PurchaseResponse(<some argument>);
        return new ResponseEntity<>(purchaseResponse,OK);
    }

    ...

}

and this is part of the service class:

@Service
@Transactional
@Qualifier("purchaseService")
public class PurchaseServiceImpl implements PurchaseService {

    private OrderRepository orderRepository;

    @Autowired
    public PurchaseServiceImpl(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }
    
    @Override
    public Order createOrder() {
        ...
        return new Order();
    }
    
    public String simple(){
        return "simple called !";
    }
    
    ...
    
}

please notice that body of simple() method does not matter at all and using any method will cause the exception.

Thanks in advance

CodePudding user response:

Finally, I found that the problem relates to the logic of purchaseService.createOrder() method, and it was because of a one-to-many relationship that works lazily. Changing fetch type and a few other changes, fixed the problem and it does not relate to how many times use a service in a rest controller at all!

  • Related