How to handle two-phase commit using SQLAlchemy

I am 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 looks like this:

session = sessionmaker(engine)(autocommit=True) tx = session.connection().begin_twophase(xid) # Doesn't issue any SQL session.begin() 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 does not cause any errors, but does not write anything to the table ... O_o What am I missing?

I even tried to lock the application after prepare() and release COMMIT PREPARED 'xid' from pgadmin, but nothing is written anyway.

+4
source share
1 answer

I managed to get it working, here's how:

 session = sessionmaker(engine)(twophase=True) session.add(obj1) session.prepare() # Find transaction id for k, v in s.transaction._connections.iteritems(): if isinstance(k, Connection): return v[1].xid 

then from another session

 session = sessionmaker(engine)(twophase=True) session.connection().commit_prepared(xid, recover=True) 
+1
source

Source: https://habr.com/ru/post/1393393/


All Articles