Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chapter 13. Danger of using single UOW instance. #312

Open

Description

In chapter 13, Unit of work instance is no longer initialized in each api request as it did in chapter 12.
https://github.com/cosmicpython/code/blob/chapter_12_cqrs/src/allocation/entrypoints/flask_app.py

The message bus keeps using a single UOW instance.
This is not a problem if all messages are handled by a single thread.

However, on a multi-threading environment where each api request may be handled by different concurrent thread, there is possibility of losing events before events are collected to the queue in the message bus.

Before a thread execute bellow statement after it handles a message, https://github.com/cosmicpython/code/blob/chapter_13_dependency_injection/src/allocation/service_layer/messagebus.py#L44
if another thread executes with uow: statement, the repository instance in the singleton UOW is replaced to a new instance and all aggregate objects stored in the old instance are lost.
https://github.com/cosmicpython/code/blob/chapter_13_dependency_injection/src/allocation/service_layer/unit_of_work.py#L52

Found this problem while developing a GRPC server which is multi-threaded by default.
I fixed this issue by making message bus to keep a UOW instance generator function instead of the UOW instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /