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

bug: subscriber afterXY functions should be called after the transaction is commited #743

Closed
NoNameProvided opened this issue Aug 7, 2017 · 7 comments

Comments

@NoNameProvided
Copy link
Member

NoNameProvided commented Aug 7, 2017

Description: After trying to update to the latest release we have experienced failing queries which just worked fine before.

Observed behaviour: The reason for this seems to be that TypeORM will wait until the afterXY subscriber calls finish before committing the transaction. (This works fine when no other query has been made in the subscriber, but it causes the transaction to fail and rollback when the subscriber try to execute queries.

Expected behaviour: TypeORM should COMMIT the transaction before calling the subscriber's afterXY functions.

@pleerock
Copy link
Member

TypeORM should COMMIT the transaction before calling the subscriber's afterXY functions.

no. What queries are you executing in your subscribers? You must use entity manager provided by subscribers to execute all queries you do there

@pleerock
Copy link
Member

pleerock commented Sep 8, 2017

If you still have issues or want to discuss something - let me know.

@pleerock pleerock closed this as completed Sep 8, 2017
@VojtaStanek
Copy link

I think I have same issue as mentioned above.

Imagine following use-case: I have an app where users can be awarded a badge. When they recieve I want to send a push-notification to them.
So I create a subscriber with afterInsert and afterUpdate methods. But typeorm notifies me of insert, but at that point when I do a SELECT in the db, it's not yet "connected" with the user (my real use-case is that I have one more entity between user and badge - I do manager.save([somethingBetween, badge])). My subscriber method is called when INSERT query is run, but before additional UPDATE query is called where relationship with the user is added. (In my code it's in one manager.save call but typeorm splits it into multiple queries probably to handle the dependencies).

Proposed solutions:

  • call method on subscriber after transaction ends
  • call afterInsert after the UPDATE query, which adds the relationship

I hope it's understandable.

@pleerock
Copy link
Member

pleerock commented Dec 1, 2017

call afterInsert after the UPDATE query, which adds the relationship

all "after" events are called after all queries are executed in terms of a single save call, please check if you really have this issue and provide more details if you do. Also, its better if you check it in next version since there are important changes in subscriber I made recently there

@yann1ckv
Copy link

yann1ckv commented Nov 1, 2018

Found a workaround for this. You can commit the transaction you are currently in but you then have to immediately open a new transaction. Typeorm will then close an empty transaction I think. You can try it like this:

event.queryRunner.commitTransaction(); // Commit transaction, to make it available in DB
event.queryRunner.startTransaction(); // Start new transaction for TypeOrm to commit after afterInsert

@VinceOPS
Copy link
Contributor

VinceOPS commented Jan 29, 2019

@pleerock Hi! Is there any specific reason for emitting events before the transaction has actually been successfully committed? Would a PR allowing a new type of event (or the same event but with an additional flag) "once commit is done" be acceptable?

Thanks!

EDIT - Ok this has been discussed here and here. I get the "use case", where Events are somewhat extensions to the transaction/query and need to make it fail if they fail. I'd still like to know if a PR would be somewhat acceptable (to only trigger some code execution if the transaction has been committed).

@incompletude
Copy link

news on this? same problem.

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

6 participants