On Saturday, 17 December 2011 17:22:26 UTC, Michael Bayer wrote:
>
>
> On Dec 17, 2011, at 9:04 AM, Andronikos Nedos wrote:
>
> Hi all,
>
> We make heavy use of SqlAchemy in our app and use a custom 2-phase commit 
> protocol over HTTP to communicate
> with mobile devices. 
>
> While all read,write,deletes happen in a first HTTP request, a subsequent 
> HTTP request needs to find the transaction and either commit or abort. i.e.,
>
> request 1, start tx
> read,write,deletes
>
> request 2, find tx
> commit or abort tx
>
> There's no requirement per se for sesion objects to be migrated from one 
> thread to another one, but is it safe to save/pickle the SessionTransaction
> object, until another request in another thread decides that all work done 
> should be committed or aborted ? 
>
>
> Yikes, not at all- SessionTransaction holds onto DBAPI connections and 
> they don't move.   Not like this wouldn't be an interesting feature,  it 
> would involve custom __getstate__()/__setstate__(), and then a lot of 
> tests.     But while I've been aware of this pattern in theory, in 16 years 
> of web programming I've never heard of anyone actually doing it.  
>
> But we do support two phase transactions and XID so the mechanics are 
> there to achieve this effect more manually.
>
> If you are using 2-phase to resurrect transactions into a new process, you 
> need to use Connection directly along with begin_twophase:
>
>
> http://www.sqlalchemy.org/docs/core/connections.html#sqlalchemy.engine.base.Connection.begin_twophase
>
> where you'll note you can pass the xid.  The xid is the thing you actually 
> need to be carrying along between requests.
>

Thanks Michael. 

I guess calling our protocol two-phase commit can be quite misleading, as 
it's not using any of the TPC database machinery, so 
it's less complicated than it appears to be. In reality, it only really 
needs to know the XID of the transaction, so it can commit or abort the 
transaction in another process.
 

>
> Then you bind your Session to that connection using the techniques at 
> http://www.sqlalchemy.org/docs/orm/session.html#joining-a-session-into-an-external-transaction
> .
>

So, if I understand correctly, I need to maintain the Connection object 
between requests and on the 2nd request bind a session to the existing 
Connection object and then 
session.commit() or session.abort() ? The question now is how can I persist 
the connection object between requests, is in-memory the only option here ?

Thanks for your help,
Andronikos

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/Qm_KtbuE2GsJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to