[Cubicweb] ValidationError and implicit rollbacks

Adrien Di Mascio adrien.dimascio at logilab.fr
Thu Sep 30 15:57:21 CEST 2010

Hi there,

In CW 3.9, if a ValidationError is raised, the transaction is implicitly 
rollbacked. As far as I understand, this is done to prevent any later 
commit() (in the same transaction) from letting the database in an 
inconsistent state.

I've just discovered a strange side-effect which is that a given eid can 
be assigned twice in the same HTTP request processing. The following 
simple set of instructions illustrates this behaviour :

     def test_validation_error_behaviour(self):
         create = self.request().create_entity
         user1 = create('CWUser', login=u'foo', upassword=u'foo')
             user2 = create('CWUser', login=u'foo', upassword=u'foo')
         except ValidationError:
             # a ValidationError is raised because logins are unique
         user3 = create('CWUser', login=u'bar', upassword=u'bar')
         # The following assertion fails and user1 doesn't event exist
         # anymore
         self.assertNotEqual(user1.eid, user3.eid)

For the record, I've discovered this because I implemented a custom 
EditController that catches ValidationError and is able to treat some of 
them as part of the normal execution flow under certain conditions.

This post is here to inform some of you who, just like me, might have 
skipped this new "feature". I wonder however if I hadn't prefered simply
a non-commitable transaction (i.e. waiting for an explicit rollback()) 
rather than an implicit one.

What do you think ?

Adrien Di Mascio - LOGILAB, Paris (France).
Formations - http://www.logilab.fr/formations
Développements - http://www.logilab.fr/services
Gestion de connaissances - http://www.cubicweb.org/

More information about the Cubicweb mailing list