-
Notifications
You must be signed in to change notification settings - Fork 1
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
database transactions and @transactional decorator #89
Comments
Possible related issue |
Design goals
Design proposals:1. Session as parameter in transactional methodclass MyGreatService(Service):
@transactional
def get_foo(self, foo_id, db_session):
# ...
db_session.query(Foo).get(foo_id)
# ...
# usage
# db_session argument is set by @transactional decorator
my_great_service.get_foo(3)
# or when we already are inside transactional method, we can use existing db session
my_great_service.get_foo(3, db_session=existing_db_session)
Pros:
Cons:
2. Sessions stored in sessionHolder object, current session accessible through sessionProxyThere are 2 crucial objects in this design:
Pros:
Cons:
|
After discussion in telegram the winning proposal is the first one Session as parameter in transactional method. The main reason is that first proposal meets all 4 design goals and is somehow cleaner solution while cons of second proposal (Sessions stored in sessionHolder object, current session accessible through sessionProxy) are significant. |
Call inside @transactional method to another @transactional method from other Service class instance lead in some cases to error.
Following code will result in exception (
sqlalchemy.orm.exc.DetachedInstanceError
) raised at linefoo.bar_parent_name = bar.parent.name
when calling methodFooService.create_foo()
If you want, I can provide real-life use-case similar to this example. This behavior limit options to better split code into logic and reusable units.
Maybe we should open discussion about @transactional decorator design and implementation. Briefly said, nowadays @transactional decorator set db session to
_orm
property of Service class instance. I am not sure if it is ideal design. Maybe we could get inspiration from Java Spring framework where 2 types of classes are used - Services and Daos - and db session is not directly bound to class instances as in our case.The text was updated successfully, but these errors were encountered: