odavid / typeorm-transactional-cls-hooked

A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS
MIT License
522 stars 86 forks source link

Error when using repository methods outside of a request context #73

Closed DavideCarvalho closed 3 years ago

DavideCarvalho commented 3 years ago

On my open source project, we have a in memory queue, where we have some functions regards gamification stuff. So, those functions run asynchronously

Before version 0.1.17, everything worked fine, but now we are constantly getting the error below

image

As far as I can tell, it seems that when not bound to a http request context, the manager is null, and then everything stops working.

image

odavid commented 3 years ago

Hi @DavideCarvalho,

Can you follow: https://github.com/odavid/typeorm-transactional-cls-hooked#logging--debug and post the log?

Also, when do you patch the Repository and when do you initialize the context?

It is weird, since the Repository.manager get/set are doing the same thing they did before

https://github.com/odavid/typeorm-transactional-cls-hooked/blob/master/src/patch-typeorm-repository.ts#L9

odavid commented 3 years ago

Hi @DavideCarvalho - I could not reproduce the issue, however, I think I found a case within the code that can explain such behavior. After #61 was merged, I noticed that the manager can be null after a transaction was ended, but the context is still active. This seems to be a very rare case, but I restored the original behavior, so I hope it solves the issue

I would be glad if you let me know after upgrading to v0.1.18, if it still happens.

DavideCarvalho commented 3 years ago

Hey @odavid, you were really fast replying! Thanks!

Renovate bot already PR my repo, will accept and have a quick test, I'll give you a feedback after it

DavideCarvalho commented 3 years ago

Everything is working fine, ty very much!