0

Component - 1

@KafkaListner
public void method(){
    service1.method1();
}

Service - 1

 @Transactional
 public void method1(){
    service2.method2();
 }

Service - 2

@Transactional
@Retry(Exception.class,3)
public void method2(){
    some logic ,may produce an exception
}

Expected : The retry happens three times

Actual : The retry is not happening .

org.springframework.kafka.KafkaException: Seek to current after exception; nested exception is org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void iesd.devops.monitoring.consumer.qualitygates.QualityGateTestExecutionEventsConsumer.testExecutionEventsListener(org.apache.kafka.clients.consumer.ConsumerRecord<java.lang.String, iesd.devops.monitoring.entity.qualitygates.TestExecutionEvent>)' threw exception; nested exception is javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [iesd.devops.monitoring.entity.qualitygates.gates.testgates.TestGatePayload#1]; nested exception is javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [iesd.devops.monitoring.entity.qualitygates.gates.testgates.TestGatePayload#1] at org.springframework.kafka.listener.SeekUtils.seekOrRecover(SeekUtils.java:208) ~[spring-kafka-2.9.0.jar:2.9.0] at org.springframework.kafka.listener.DefaultErrorHandler.handleRemaining(DefaultErrorHandler.java:169) ~[spring-kafka-2.9.0.jar:2.9.0] at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeErrorHandler(KafkaMessageListenerContainer.java:2817) ~[spring-kafka-2.9.0.jar:2.9.0]

instead of

ObjectOptimisticLockingException

  • where are your methods? are you calling them within the same class? this does not work because spring build proxys 'around' the class. it explained here https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-understanding-aop-proxies – Andreas Radauer Aug 08 '22 at 18:17

1 Answers1

0

Most of the time if there are problems with annotations in Spring, it's because AOP in Spring is based on proxies around the Class. https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-understanding-aop-proxies explains it. So the mechanism for recognizing the calls and exception does not see it when it comes from within the same class.

This should solve the issue:

@Service
public class MyService{
    @Autowired
    private MyService myService;
    
    public void method1(){
        ..
        myService.method2();
        ..
    }
    public void method2(){
        ..
    }
}
Andreas Radauer
  • 1,083
  • 7
  • 18