How to skip(not rollback) for exception in @Transactional method?

Viewed 156

My goal is to validate update logic before it queries to the database. Also this validation should not rollback or fail on request. It should just skip failed iteration in for-loop logic(logging would be enough) and go next iteration.

I created custom Hibernate interceptor, that validates request "before SQL querying" at the end of transaction. That's how I understand it.

I check current and previous field value in boolean onFlushDirty overriten method and throw org.hibernate.CallbackException(as it throws in Interceptor interface method signature) if validation failes.

In my non-transactional service method I have for loop where I execute another method that can cause this exception. repository.save() method is transactional and method throws JpaSystemException instead of CallbackException when validation fails... So I catch it and don't throw anything - just logging. That's why failed iteration is skipped and service method continues its work.

But I need to have transactional service method to have option to rollback for other exceptions and keep "skipping Hibernate interceptor exception when it caught" finishing method successfully.

I tried to add

@Transactional(noRollbackFor = JpaSystemException.class)

But it doesn't work for me. I also tried to noRollBackFor others exception, I tried to throw custom exceptions instead of CallbackException. It didn't fix problem.

What should I do in my situation?

0 Answers
Related