[openstack-dev] [tricircle] DAL implementation

joehuang joehuang at huawei.com
Fri Jul 31 01:05:12 UTC 2015


Hi, Vega,

Multiple DB access will be a use case for one session , especially for DB data insertion, multiple table will be involved. To embed the session in Context, it’s ok to start a session if the session is empty, but how to decide when to commit the data, end a session?

Best Regards
Chaoyi Huang ( Joe Huang )

From: Vega Cai [mailto:luckyvega.g at gmail.com]
Sent: Thursday, July 30, 2015 3:03 PM
To: openstack-dev at lists.openstack.org
Subject: [openstack-dev] [tricircle] DAL implementation

Hi folks,

In my current implementation, there are a core module and a models module. Core module handles all the database stuff, start a session, issue sql operation, then end a session. Models module invokes methods in core module to access database, as showed below:

model.py
def get_site(site_id):
    core.get_resource(Site, site_id)

core.py
def get_resource(model, pk_value):
    # code to access database

To add context, I am going to implement like this:

model.py
def get_site(context, site_id):
    policy_check(context)
    core.get_resource(Site, site_id)

core.py
def get_resource(model, pk_value):
    # code to access database

So there is no need to embed session into context.

One advantage of embedding session into context is that you can combine more than one method calls in one session, like:

model.py
def complex_operation(context):
    policy_check(context)
    with context.session.begin():
        core.operation1(context)
        core.operation2(context)

But this approach moves session handling from core module to models module and core module just provides some utility methods.

I'm not sure which one is better.

BR
Zhiyuan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20150731/ea65be1f/attachment.html>


More information about the OpenStack-dev mailing list