Skip to content
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 to your account

[Feature]: Implement afterTransactionCompletion, afterCommitCreate, et al similar to Hibernate counterparts #1175

Closed
rhyek opened this issue Dec 7, 2020 · 3 comments · Fixed by #1213
Labels
enhancement New feature or request

Comments

@rhyek
Copy link
Contributor

rhyek commented Dec 7, 2020

Is your feature request related to a problem? Please describe.
Our project is making use of explicit transactions with explicit use of begin, commit, and rollback methods. We need to register hook handlers for afterFlush, afterCreate, etc, to run some business logic that will notify users and/or integrated systems of such events.
Our problem as of now is that these are run after UoW commit phase and not transaction commit phase so in the case where a commit fails on the database side (deferred constraint checks, concurrency problems, etc), it is important to have a way from those hook handlers to be aware of this and therefore not execute their code OR have separate hooks that are guaranteed to only run on successful transactions. Currently those hooks are executed regardless and this presents a serious problem for our use case.

Describe the solution you'd like
Implement hooks similar to afterTransactionCompleted and events such as POST_COMMIT_INSERT, et al, which are part of the Hibernate ORM (I understand this project is inspired in part by it) so that we can make use of those hooks instead of the current ones. The behavior of the current ones would not be altered.

Describe alternatives you've considered
Our team has concluded that the only way for us to guarantee our business logic is run on successful transactions only is to make use of the CDC (Change Data Capture) functionalities of our preferred database in conjunction with perhaps something like Kafka Connect by way of a Debezium Kafka Source Connector and then additionally a Kafka Consumer that would execute our aforementioned business logic. This seems overkill to us.

Additional context
I had previously opened an issue regarding our findings: #1160. I am willing to write a PR implementing this feature, but would first like to hear from @B4nan if that is something that would be taken into consideration or not.

@rhyek rhyek added the enhancement New feature or request label Dec 7, 2020
@rhyek rhyek changed the title Implement afterTransactionCompletion, afterCommitCreate, et al similar to Hibernate counterparts [Feature]: Implement afterTransactionCompletion, afterCommitCreate, et al similar to Hibernate counterparts Dec 7, 2020
@rhyek
Copy link
Contributor Author

rhyek commented Dec 9, 2020

The test suite referenced in #1160 had a few issues where some tests where not written correctly and I was not awaiting some expects.

With that fixed, I can see that actually more (and some new specific) test cases fail:

image

So as you can see, in the case of commit statement failures, afterCreate and afterFlush are actually being called in the case of explicit transactions with transactional(), and afterCreate (but not afterFlush) in the case of implicit transactions.

After updating to 4.3.4-dev.8 (which includes #1177) 1 test case now succeeds:

image

@kdi-1
Copy link

kdi-1 commented Dec 10, 2020

These hooks exist in other Node.js libraries -
TypeORM - afterTransactionCommit and afterTransactionRollback
Sequelize - afterCommit
It would be great to have something similar in MikroORM

@rhyek
Copy link
Contributor Author

rhyek commented Dec 12, 2020

I am currently working on a PR for this. Will have something very soon.

B4nan pushed a commit that referenced this issue Dec 17, 2020
Current lifecycle hooks cover entity and unit of work events, but
transaction-related ones are missing. Add beforeTransactionStart,
afterTransactionStart, beforeTransactionCommit, afterTransactionCommit,
beforeTransactionRollback, and afterTransactionRollback

Closes #1175
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants