swetha
swetha

Reputation: 111

@OnWriteError method not updating the DB record status

I have an itemWriter that writes items to the table. When writing, I get a ConstraintViolationException and I am using SkipPolicy to skip that Exception and i have implemented @OnWriteError method.So it works fine to catch and skip the exception and calls the @OnWriteError method inside the listener. Inside @OnWriteError method i am calling another method to mark the event as failed when there is an exception caught.

@OnWriteError

public void onWriteError(Exception exception,List items ) {

    log.info("WRITE ERROR LISTENER CALLED");

    Student student = items.get(items.size()-1);

    studnetManager.updateStatuss(student, "FAILED);

}

public void updateStatuss(Student student, Status status) {

        Student.setStatus(status);

        StudentRepository.save(Student);

}

updateStatuss() gets called successfully but the student record is not marked as FAILED. Can anyone help me understand this ?

Upvotes: 0

Views: 605

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31640

As mentioned in the Javadoc, ItemWriteListener#onWriteError will be called in a transaction that is going to be rolled back.

So you need to call that code in a separate transaction (either annotate updateStatus with @Transactional(propagation = "REQUIRES_NEW") or programmatically call the transactional code using a TransactionTemplate).

Upvotes: 1

Related Questions