On Mon, Jan 11, 2010 at 4:07 PM, Manlio Perillo
<[email protected]> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi.
>
> I'm updating some of my code to SQLALchemy 0.6, and I have noted a
> problem with the sessionmaker function.
>
> The problem is a compatibility one: old versions use the transactional
> parameter, new ones the autocommit parameter.
>
> Usually, to handle these problems I use the try/except method:
>
> try:
> return orm.sessionmaker(bind=bind, autocommit=autocommit)
> except TypeError:
> # COMPAT: SQLAlchemy 0.4.x, deprecated in 0.5.x
> transactional = not autocommit
> return orm.sessionmaker(bind=bind, transactional=transactional)
>
>
> However this does not work, since error is raise only ewhen the actual
> Session instance is created.
>
>
> As far as can understand, the sessionmaker function supports keyword
> arguments since user can specify a custom session class to use.
>
> Can error handling be improved?
How about:
try:
orm.create_session(autocommit=autocommit)
except TypeError:
# COMPAT: SQLAlchemy 0.4.x, deprecated in 0.5.x
transactional = not autocommit
return orm.sessionmaker(bind=bind, transactional=transactional)
else:
return orm.sessionmaker(bind=bind, autocommit=autocommit)
Creating and disposing a session via create_session() in this way
isn't particularly expensive and won't initiate any database
connections or activity.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en.