For a variety of reasons, sometimes it's nice to be able to do something 
like this:

with dbsess.begin(rollback=True):
   do_stuff

Where the expected behavior is that everything in the context runs in a 
transaction, but upon exiting the context we issue a rollback. Sometimes 
this is useful to *guarantee* that actions that take place within the 
context are transactionally read-only.

I had authored and am using this:

def make_dbsess_contextual(sess_factory):
    @contextmanager
    def dbsess(on_success='commit'):
        sess = sess_factory()
        # we could use 'with sess.begin() here but it's
        # easier to have parity
        sess.begin()
        try:
            yield sess
        except:
            sess.rollback()
            sess.close()
            raise
        else:
            if on_success == 'commit':
                sess.commit()
            else:
                sess.rollback()
            sess.close()
    return dbsess

and it's used like this:

factory = ....
contextual_factory = make_dbsess_contextual(factory)

with contextual_factory('rollback') as dbsess:
  do_stuff(dbsess)

however it would be nice if the functionality could be integrated directly.
I also strongly suspect that the way I've approached this is non-optimal.

I looked at the code for how this might work and I have some ideas but I 
thought I'd check here first to see if I'm completely off-base.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to