0

I using Spring boot with KafkaListener, and i trying to treat internal exceptions (ConstraintViolationException) but i dont know why the internal try catch are not working

So i have the follow code

    @KafkaListener(topics = {"someTopic"},
            groupId = "group_id")
    public void someTopicTreatment(String message) {
        someService.treatmentForSomeTopic(formatMessageToObject(message));
    }

In the treatmentForSomeTopic code i have


public void treatmentForSomeTopic(String message) {

  try {

    repository.save(message); // this can throw RuntimeException like ConstraintViolationException
  } catch (Exception e) { // IGNORED AND DON'T KNOW WHY
    boolean treated = tryToTreatError();
    if(!treated) {
      throw new CanNotTreatException();
    } 
  }

But the problem seem that the Exception is not catched in the treatmentForSomeTopic, is throw direct at the kafka treatment (if i put some try catch in the the method with @KafkaListener i can see the ConstraintViolationException), and i dont understand why.

The stack asked by @tgdavies


org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [id_fkey]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:276)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:566)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:743)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:654)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:407)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)
    at com.nicearma......TreatmentProcessor$$EnhancerBySpringCGLIB$$a9f88136.treatmentForSomeTopic(<generated>)
    at com.nicearma......someTopicTreatment(KafkaConsumer.java:38)
nicearma
  • 750
  • 2
  • 8
  • 21
  • Please add the full stack trace of the exception to your question. – tgdavies Jul 02 '22 at 13:19
  • @tgdavies stack trace added (i modify the name of the method to be the same of the example) – nicearma Jul 02 '22 at 13:26
  • 1
    As you can see from the stack trace, the exception is thrown by the TransactionInterceptor which is wrapping your method, so you cannot catch it. Have a look at https://stackoverflow.com/questions/2109476/how-to-handle-dataintegrityviolationexception-in-spring – tgdavies Jul 02 '22 at 13:29
  • Related problem: https://stackoverflow.com/questions/52456783/cannot-catch-dataintegrityviolationexception – Haoliang Jul 02 '22 at 14:53
  • You should wrap your tryToTreatError call on its own try / catch block - if it throws an exception, it’ll go straight to the listener method. – Tomaz Fernandes Jul 03 '22 at 01:20
  • Yes the problem seem to be coming from the transactional, so i changed the transactional to the repository code, that way the try catch get the error emitted in the transaction – nicearma Jul 03 '22 at 07:43

0 Answers0