[sqlalchemy] error handling for sessionmaker function
-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? Thanks Manlio -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAktLvVYACgkQscQJ24LbaUSrRQCfab1w/JR+KUNdAo188hEn4NgK Rf8AoIJR/iGu0xHGTUv09Z3A6sjeydFg =w5ts -END PGP SIGNATURE- -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] error handling for sessionmaker function
On Mon, Jan 11, 2010 at 4:07 PM, Manlio Perillo manlio.peri...@gmail.com 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 sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] error handling for sessionmaker function
On Jan 11, 2010, at 7:07 PM, Manlio Perillo 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? i think instead of putting try/excepts all over the place in an attempt to achieve compat with 0.4 thorugh 0.6, just look at sqlalchemy.__version__ to determine the correct API. 0.4 also has severe performance issues so I'd urge everyone to drop it if they haven't already. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.