On Tue, Feb 12, 2008 at 07:34:46AM -0800, Pavel Skvazh wrote:
> This may sound like a really obvious, not really Pylons related
> question but still, I'm pretty sure there are people who just doesn't
> really pay attention to this or just new guys like me.

Right, we eat new guys for breakfast. :) I rather assume that most of us
are in many ways 'new guys' so don't worry.

> How do you guys make sort of init in your Pylons model/__init__.py to
> set up DB connection and global variables?

I assume you read
http://wiki.pylonshq.com/display/pylonscookbook/SQLAlchemy+0.4+for+people+in+a+hurry
already. Otherwise: do it. :)

> Let's say we've got a pretty common __init__.py file like this:
> 
> from sqlalchemy import *
> from sqlalchemy.exceptions import *
> 
> meta = MetaData()
> toCharFormat = u'dd.mm.yyyy'
> 
> DB_LOCATION = config.get('sqlalchemy_conf.db_location', '')
> 
> Session = sessionmaker(.....)
> 
> def init():
>       if session.has_key('SA_sess'):
>               pass
>       else:
>               session['SA_sess'] = web_user_sess
> 
> import classifiers
> import register
> ....
> 
> # Setting up WebUser common session
> engine = create_engine(.....')
> web_user_sess = Session(bind=engine)
> 
> ..................
> 
> The main question is: Is the content of this file run only on
> application start or on every request to any controller?

The model/__init__.py (if you import it from your lib/base.py) is
imported in a global context of your application. The "Session" object
in there is a "contextual session"
(http://www.sqlalchemy.org/docs/04/session.html#unitofwork_contextual)
which means you get a session object per thread. If you try to access
the session object (even if you may have removed it at the end of your
HTTP request) then you get the very same session object back for the
thread. So it's a consistent thread-local object. At least that's how I
understood it.

So your models are not defined at every request. Neither is the database
connection re-established at every request. Each thread has access to
the database backend through the session.

> If every time - what's the best practice for specifying some init
> function that will set up the connection?

The "in a hurry" article is the best description.

Cheers
 Christoph
-- 
[EMAIL PROTECTED]  www.workaround.org   JID: [EMAIL PROTECTED]
gpg key: 79CC6586         fingerprint: 9B26F48E6F2B0A3F7E33E6B7095E77C579CC6586

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

  • Model init Pavel Skvazh
    • Re: Model init Christoph Haas

Reply via email to