Using SQLAlchemy 0.9.7 against a PostgreSQL 9.2 database.  

with e as an Engine:


i = 1
with e.begin() as conn:
    x = conn.execute('select * from test01')
    print x.fetchall()
    with conn.contextual_connect() as conn2:
        with conn2.begin():
            i += 1
            conn2.execute('insert into marius.test01 values (%s)' % str(i))
            x = conn2.execute('select * from test01')
            print x.fetchall()
    x = conn.execute('select * from test01')
    print x.fetchall()
    raise Exception("HELLO")


This will actually commit a row to the table test, rather than commit 
nothing.

Should the transaction not be carried over to the branched connection? 
 This is not PostgreSQL specific.


Something like this solves this potential issue:

class Connection(sqlalchemy.engine.Connectable):
    """Override of :class:`sqlalchemy.engine.Connection` to allow allow 
"nested" connections (non-forking).

    This is achieved by using a stack increment decrement.
    """

    def __init__(self, engine, connection=None, close_with_result=False, 
_branch=False, _execution_options=None,
                 _dispatch=None, _has_events=None, transaction=None):
        ...
        self.__transaction = transaction
        ...
        
    def _branch(self):
            """Return a new Connection which references this Connection's
            engine and connection; but does not have close_with_result 
enabled,
            and also whose close() method does nothing.

            This is used to execute "sub" statements within a single 
execution,
            usually an INSERT statement.
            """

        return self.engine._connection_cls(
            self.engine,
            self._Connection__connection,
            _branch=True,
            _has_events=self._has_events,
            _dispatch=self.dispatch,
            transaction=self.__transaction)

What is the standard way to deal with this case?  Subclassing Connection to 
implement this?

-- 
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