how to handle sqlalchemy exception with pyramid?

69 views
Skip to first unread message

Di Dio Adrien

unread,
Aug 5, 2011, 9:43:05 AM8/5/11
to pylons-...@googlegroups.com
I use the pyramid_routesalchemy scaffold, Is it possible to handle an sqlalchemy excpetion in a view.
If I understand, the transaction is commited after the request . 
def trackMail(request):
    try:
        dbsession = DBSession()
        test=Test("test")
        dbsession.add(test)       
    except exc.SQLAlchemyError:  
    #do something
    return Response("ok")

Di Dio Adrien

unread,
Aug 5, 2011, 12:53:38 PM8/5/11
to pylons-...@googlegroups.com
Sorry my first message was incomplete (accidental sending):


I solved my problem with the use of a finished callback:

def commit_callback(request):
    '''commit or abort the transaction associated with request'''
    if request.exception is not None:
        transaction.abort()
    else:
        try:
            transaction.commit()
        except exc.SQLAlchemyError as e:
            print e
            transaction.abort()

Is it the right way for handling sqlalchemy exception in a callable view?

Thanks.

Michael Merickel

unread,
Aug 5, 2011, 6:05:43 PM8/5/11
to pylons-...@googlegroups.com
The real exceptions occur when the data is flushed to the database, not the actual commit.

If you do a dbsession.flush() in your view code and it doesn't error then you can be fairly certain that the transaction's commit() will not fail.


--

Michael
Reply all
Reply to author
Forward
0 new messages