Transaction's new design and its use in a pyramid web app

55 views
Skip to first unread message

Fabien Castarède

unread,
May 28, 2016, 5:33:17 AM5/28/16
to tryton-dev
In a Pyramid web app that use SQLAlchemy I need to work also with a Tryton DB. With Tryton 3.8 I had implemented a Zope Data Manager for Tryton backend that I joined to the current transaction of my Pyramid app. Now with Tryton 4.0 it no longer works due to the new design of Tryton transaction. Here is my initial Zope Data Manager for Tryton backend :

from trytond.transaction import Transaction as TrytonTransaction

class TrytonDataManager(object):
    transaction_manager = transaction.manager

    def __init__(self, db, user, context={}):
        TrytonTransaction().start(db, user, context=context)

    def abort(self, transaction):
        pass

    def tpc_begin(self, transaction):
        pass

    def commit(self, transaction):
        pass

    def tpc_vote(self, transaction):
        pass

    def tpc_abort(self, transaction):
        pass

    def tpc_finish(self, transaction):
        cursor = TrytonTransaction().cursor
        cursor.commit()

And I have this "cleanup" hook for the Pyramid requests :

def _cleanup(request):
    TrytonTransaction().stop()
request.add_finished_callback(_cleanup)

I don't know how update my code to manage the new design of the Tryton transaction. Especially how to stop transaction in the cleanup hook.

Any idea ?

Nicolas Évrard

unread,
May 28, 2016, 5:50:37 AM5/28/16
to tryton-dev
* Fabien Castarède [2016-05-28 11:27 +0200]:
>In a Pyramid web app that use SQLAlchemy I need to work also with a Tryton
>DB. With Tryton 3.8 I had implemented a Zope Data Manager for Tryton
>backend that I joined to the current transaction of my Pyramid app. Now
>with Tryton 4.0 it no longer works due to the new design of Tryton
>transaction.

It's funny that you did since we use the design of the Zope Data
Manager to add the support of the two phase commit protocol to Tryton.

Here's an example of a data manager to send emails:
http://hg.tryton.org/trytond/file/tip/trytond/sendmail.py

Basically what you have to do once you have created your datamanager
is to join it to the Tryton transaction (line 22). Afterwards, the
Tryton transaction should take care of everything for you.

In our case we decided that the Tryton transaction would be the one
orchestrating the others.

>def _cleanup(request):
> TrytonTransaction().stop()
>request.add_finished_callback(_cleanup)
>
>I don't know how update my code to manage the new design of the Tryton
>transaction. Especially how to stop transaction in the cleanup hook.

The Tryton Transactions are now context managers, the stop method has
been removed.

I don't know much about Pyramid so I don't know how you have to change
your code but you could use some inspiration from flask-tryton:

http://hg.b2ck.com/flask-tryton/

--
Nicolas Évrard - B2CK SPRL
E-mail/Jabber: nicolas...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Fabien Castarède

unread,
May 28, 2016, 3:19:47 PM5/28/16
to tryton-dev


Le samedi 28 mai 2016 11:50:37 UTC+2, Nicolas Évrard a écrit :
* Fabien Castarède  [2016-05-28 11:27 +0200]:
>In a Pyramid web app that use SQLAlchemy I need to work also with a Tryton
>DB. With Tryton 3.8 I had implemented a Zope Data Manager for Tryton
>backend that I joined to the current transaction of my Pyramid app. Now
>with Tryton 4.0 it no longer works due to the new design of Tryton
>transaction.

It's funny that you did since we use the design of the Zope Data
Manager to add the support of the two phase commit protocol to Tryton.

Here's an example of a data manager to send emails:
        http://hg.tryton.org/trytond/file/tip/trytond/sendmail.py

Basically what you have to do once you have created your datamanager
is to join it to the Tryton transaction (line 22). Afterwards, the
Tryton transaction should take care of everything for you.

In our case we decided that the Tryton transaction would be the one
orchestrating the others.

Actually I would join the Tryton transaction to the transaction of my Pyramid app, it's because of that that I have implemented a Zope Data Manager for the Tryton transaction.

Fabien Castarède

unread,
May 28, 2016, 3:19:47 PM5/28/16
to tryton-dev
Finally I have update my data manager like this :

from trytond.transaction import Transaction as TrytonTransaction

class TrytonDataManager(object):
    transaction_manager = transaction.manager

    def __init__(self, db, user, context={}):
        TrytonTransaction().start(db, user, context=context)

    def abort(self, transaction):
        pass

    def tpc_begin(self, transaction):
        pass

    def commit(self, transaction):
        pass

    def tpc_vote(self, transaction):
        pass

    def tpc_abort(self, transaction):
        pass

    def tpc_finish(self, transaction):
        with TrytonTransaction() as tryton_trans:
            tryton_trans.commit()

And I removed the cleanup hook since it is no longer needed.

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages