On Jan 30, 2012, at 5:49 AM, Joril wrote:

> Hi everyone!
> I'm trying to do a two-phase commit using SQLalchemy 0.6.8 with
> Postgresql 8.3.4, but I think I'm missing something...
> The workflow goes like this:
> 
> session = sessionmaker(engine)(autocommit=True)
> tx = session.connection().begin_twophase(xid) # Doesn't issue any SQL
> session.begin() # Issues a BEGIN that marks transaction boundary
> session.add(obj1)
> session.flush()
> tx.prepare()


> then from another session
> 
> session = sessionmaker(engine)(autocommit=True)
> session.connection().commit_prepared(xid, recover=True) # recover=True
> because otherwise it complains that you can't issue a COMMIT PREPARED
> from inside a transaction
> 
> This doesn't raise any error, but doesn't write anything to the table
> either... O_o What am I missing?

What's happening above is the session is not in a transaction due to 
autocommit, so when you call session.connection(), that connection is thrown 
away as far as the Session is concerned.   If you wanted to do it the 
"external" way like that, you need to set session.bind = connection, then the 
Session will do everything in terms of that connection.  You could skip the 
autocommit/begin/flush and just do a single session.commit(), which from the tx 
point of view would not be the actual commit.

Session has a public API for this, though it doesn't at the moment accept the 
XID, but you can grab the one it generates:

session = sessionmaker(engine)  
session.prepare()
session.add(obj1)
session.flush()

# we need some public API here, here's a workaround
xid = session.transaction._connections[session.connection()][1].xid

either way should work, but turning on SQL echo is the best way to see what 
is/is not happening.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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.

Reply via email to