I don't know exactly what your needs are, but here's the example from the docs:

    engine1 = create_engine('postgresql://db1')
    engine2 = create_engine('postgresql://db2')

    Session = sessionmaker(twophase=True)

    # bind User operations to engine 1, Account operations to engine 2
    Session.configure(binds={User:engine1, Account:engine2})

    session = Session()

    # .... work with accounts and users

    # commit.  session will issue a flush to all DBs, and a prepare
step to all DBs,
    # before committing both transactions
    session.commit()

So in this case there are 2 engines, pointing at 2 different DBs.
Operations against the "User" class will use engine1, and operations
against the Account class will use engine2. session.commit() will use
two-phase operations to ensure that both get committed together.

I think the magic happens within the Session.get_bind() method:

  
http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#sqlalchemy.orm.session.Session.get_bind

  
http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#sqlalchemy.orm.session.Session.params.binds

Would that work in your situation?

Simon

On Wed, Feb 26, 2014 at 12:50 PM, Chris Withers <[email protected]> wrote:
> Hi Simon,
>
> The problem is that I want to use multiple sessions, I think.
>
> How would I have connections to multiple databases with differing schemas
> using a single Session?
>
> cheers,
>
> Chris
>
>
> On 25/02/2014 18:14, Simon King wrote:
>>
>> I've never used two-phase commit, but can you get away with just a
>> single session, like the example at:
>>
>>
>> http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#enabling-two-phase-commit
>>
>> Simon
>>
>> On Tue, Feb 25, 2014 at 6:07 PM, Chris Withers <[email protected]>
>> wrote:
>>>
>>> No takers? :'(
>>>
>>>
>>> On 19/02/2014 18:43, Chris Withers wrote:
>>>>
>>>>
>>>> Hi All,
>>>>
>>>> My other usual question now ;-)
>>>>
>>>> I have multiple databases that I'm connecting to from my application. I
>>>> need to commit or rollback a single transaction across all of them.
>>>>
>>>> So, two phase commit, right?
>>>>
>>>> Okay, but how do I tie the sessions together? What and how do I call
>>>> commit?
>>>>
>>>> Is there anything better than zope.transaction? (which does address this
>>>> need...)
>>>>
>>>> Chris
>>>>
>>>
>>> --
>>> Simplistix - Content Management, Batch Processing & Python Consulting
>>>              - http://www.simplistix.co.uk
>>>
>>> --
>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/sqlalchemy.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>             - http://www.simplistix.co.uk
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to