# Post transaction hook

**URL:** <https://discourse.hibernate.org/t/post-transaction-hook/9525>\
**Category:** Hibernate ORM\
**Created:** [May 16, 2024, 2:34pm UTC](https://discourse.hibernate.org/t/post-transaction-hook/9525 "2024-05-16T14:34:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![agreedSkiing](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/agreedskiing/32/2282_2.png) [@agreedSkiing](https://discourse.hibernate.org/u/agreedSkiing)\
**Post date:** [May 16, 2024, 2:34pm UTC](https://discourse.hibernate.org/t/post-transaction-hook/9525/1 "2024-05-16T14:34:25Z")

</div>

Is there a way to define a post transaction hook instead of using the standard @PostPersist/Update/Delete but to keep the event like created/updated/deleted?

I’m using a redis (don’t know if should mention it now after the license change, but I have done it now 😅) and would like to update its content only when the transaction has been completed, when all the cascaded data has been update/created/deleted.

P.S. I do know that Hibernate search has something similar for their indexing needs with elastic/opensearch.

---

<div class="post-metadata">

**Author:** ![agreedSkiing](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/agreedskiing/32/2282_2.png) [@agreedSkiing](https://discourse.hibernate.org/u/agreedSkiing)\
**Post date:** [May 16, 2024, 3:43pm UTC](https://discourse.hibernate.org/t/post-transaction-hook/9525/2 "2024-05-16T15:43:50Z")

</div>

Just going to answer this myself 🙃, found the documentation with some lucky keywords after writing this [jakarta ee - Check whether a JTA transaction is successfully committed - Stack Overflow](https://stackoverflow.com/questions/32724029/check-whether-a-jta-transaction-is-successfully-committed) and after that found a lot more help in the quarkus code.  
[Using transactions in Quarkus - 3.8 - Quarkus](https://quarkus.io/version/3.8/guides/transaction)  
[Add support for transactional observer event · Issue #2224 · quarkusio/quarkus · GitHub](https://github.com/quarkusio/quarkus/issues/2224) → [Introduce transactional observer support by manovotn · Pull Request #6581 · quarkusio/quarkus · GitHub](https://github.com/quarkusio/quarkus/pull/6581/files)  
[Support event handle after/before transactional commited · Issue #22961 · quarkusio/quarkus · GitHub](https://github.com/quarkusio/quarkus/issues/22961)

Not marking it as a solution yet since I haven’t tried to implement it.

---

<div class="post-metadata">

**Author:** ![agreedSkiing](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/agreedskiing/32/2282_2.png) [@agreedSkiing](https://discourse.hibernate.org/u/agreedSkiing)\
**Post date:** [July 17, 2024, 5:41am UTC](https://discourse.hibernate.org/t/post-transaction-hook/9525/3 "2024-07-17T05:41:51Z")

</div>

So what I needed wasn’t that hard but and was the JTA transaction handeling, but I had a hard time finding an implementation reference for how it works.

1. First i needed to create a jakarta event `private jakarta.enterprise.event.Event<se.agreedSkiing.example.Entity1> dbLogEvent;`
2. Then fire said event from a `@PostPersist/PrePersist`

```auto
    @jakarta.persistence.PostRemove
    public void sendEvents(se.agreedSkiing.example.Entity1 entity) {
        dbLogEvent.fire(entity);
    }

```

1. Create an observer method with the correct transaction phase and wait for a db event to fire.

```auto
    void afterSuccessfulTransactionObservingEntity1LogAsJson(
        @jakarta.enterprise.event.Observes(during = jakarta.enterprise.event.TransactionPhase.AFTER_SUCCESS) Entity1 dto
    ) {
        try {
            var json = new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(dto);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

```
