"Ian Kelly" <ian.g.ke...@gmail.com> wrote in message news:CALwzid=fzgjpebifx1stdbkh8iwltwggwwptphz1ykyg+05...@mail.gmail.com... > On Tue, Jul 9, 2013 at 1:35 AM, Frank Millman <fr...@chagford.com> wrote: >> When any of them need any database access, whether for reading or for >> updating, they execute the following - >> >> with db_session as conn: >> conn.transaction_active = True # this line must be added if >> updating >> conn.cur.execute(__whatever__) > > I'd probably factor out the transaction_active line into a separate > DbSession method. > > @contextmanager > def updating(self): > with self as conn: > conn.transaction_active = True > yield conn > > Then you can do "with db_session" if you're merely reading, or "with > db_session.updating()" if you're writing, and you don't need to repeat > the transaction_active line all over the place. >
I'll bear it in mind, but I will have to expend some mental energy to understand it first <g>, so it will have to wait until I can find some time. > I would also probably make db_session a factory function instead of a > global. It is not actually a global. When I create a new session, I create a db_session instance and store it as a session attribute. Whenever I create a database object during the session, I pass in the instance as an argument, so they all share the same one. Frank -- http://mail.python.org/mailman/listinfo/python-list