spring-projects / spring-projects/spring-data-rest

Support transactionally sound EventListeners [DATAREST-403]

Open
#780 6 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

status: feedback-provided type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

daniel carter opened DATAREST-403 and commented

I need to publish update notifications over JMS to another system. The RepositoryEventListeners seem the logical place to do this, but these events fire outside of the scope of the container transaction.

Perhaps an onBeforeCommit() is in order? Or is there another extension point for this? It seems like a fairly standard fundamental requirement?

@org.springframework.transaction.annotation.Transactional
public class TestEventListener extends AbstractRepositoryEventListener<Object> {

    private static final Logger LOG = LoggerFactory.getLogger(TestEventListener.class);

    @Override
    protected void onBeforeCreate(Object entity) {
        LOG.info("XXX before create");
    }

    @Override
    protected void onBeforeSave(Object entity) {
        LOG.info("XXX before save");
    }

    @Override
    protected void onAfterCreate(Object entity) {
        LOG.info("XXX after create");
    }

    @Override
    protected void onAfterSave(Object entity) {
        LOG.info("XXX after save");
    }

}
08 15:32:37.119 [http-nio-9000-exec-1] INFO  n.c.v.vcidb.TestEventListener - XXX before create 
08 15:32:37.135 [http-nio-9000-exec-1] TRACE o.s.t.i.TransactionInterceptor - Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]


08 15:32:37.432 [http-nio-9000-exec-1] TRACE o.s.t.i.TransactionInterceptor - Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save] 
08 15:32:37.479 [http-nio-9000-exec-1] INFO  n.c.v.vcidb.TestEventListener - XXX after create 

Affects: 2.1.4 (Dijkstra SR4)

Reference URL: http://stackoverflow.com/questions/25717127/handle-spring-data-rest-application-events-within-the-transaction

4 votes, 9 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.