On 8/1/07, Max Ischenko <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm using SAContext to manage my SQLAlchemy database connections. Today
> setup looks like this.
>
> BaseController.__call__:
>
> model.sac.session.clear()
> self.db = model.DatabaseFacade()
>
> And DatabaseFacade.__init__:
>
> self.session = sac.session
> self.meta = sac.get_metadata("blog")
>
> Basically, I create an instance of DatabaseFacade for every web request and
> DatabaseFacade relies on sac.session to manage db conn.
>
> Is it optimal setup? What if make a DatabaseFacade created just once,
> instead of per-request? Would it remain thread-safe?
No. sac.session is the session appropriate to the current thread (and
application for multi-app sites). You'd be saving a session in one
thread and using it in another thread. Worse, the session you clear
in the base controller would be different than the session used by a
DatabaseFacade method.
Are you sure you need a DatabaseFacade class? If there's only one
instance, it's the same as a module. So you can recast your
high-level access methods as functions:
model
====
sac = SAContext()
sac.add_engine(...)
table1 = Table("Table1", sac.metadata, ...)
MyClass(object):
pass
mapper(MyClass, table1)
def get_top_records():
return sac.query(MyClass).filter(...)
def get_last_modify_date():
moddate = table1.c.modify_date
q = select([moddate], order_by=[desc(moddate)], limit=1)
row = q.execute().fetchone()
if row is None:
return None
return row[0]
You can also import sac as Jose suggested. I just prefer to do
everything from the model import.
--
Mike Orr <[EMAIL PROTECTED]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---