8000 @ApplicationModuleListener and Ignoring Exceptions · Issue #1187 · spring-projects/spring-modulith · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

@ApplicationModuleListener and Ignoring Exceptions #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in 8DBA to your account

Open
SpaceCondor opened this issue May 9, 2025 · 0 comments
Open

@ApplicationModuleListener and Ignoring Exceptions #1187

SpaceCondor opened this issue May 9, 2025 · 0 comments

Comments

@SpaceCondor
Copy link
SpaceCondor commented May 9, 2025

I am trying to have an event listener gracefully ignore DataIntegrityViolationException due to a unique key constraint. I want the event to be successfully completed.

I have the following listener declared:

@Component
class WarehouseManagement {

    @Autowired
    private final WarehouseRepository warehouseRepository;

    @ApplicationModuleListener
    void on(ProductCreated event) {
        try {
            warehouseRepository.addProductToWarehouse(event.getProductId());
        } catch (DataIntegrityViolationException e) {
            logger.warn("Skipping duplicate entry.");
        }
    }
}

The warehouse repository (for reference):

@Service
public class WarehouseProductRepository {

@Autowired
    private final WarehouseProductJpaRepository db; // Implements JpaRepository<WarehouseProduct, Long>
    
    public void addProductToWarehouse(Long productId){
        /** **/
    }
    
}

I've tried annotating the method like so to disable rollbacks and (hopefully) have the method execute successfully:

    @ApplicationModuleListener
    @Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor =  DataIntegrityViolationException.class)

But that doesn't work either, since it yields the following error:

Application exception overridden by commit exception

I've also tried using .saveAndFlush() inside of the JPA repository instead of just save(), but that didn't work either.

Is there a way to accomplish this kind of functionality?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0