Odoo Help


What is the cr.commit() risk, how to safe db write

on 8/22/14, 12:30 PM 8,628 views


I have a cycle like this.

for i in ids:

 self.pool.get('product.product').create(cr, uid, vals, context = context)


Now in empty system this is not cause any matter, but what is a risk, for this kind of execution? Think of live system with user activites, and so on.

If has what is a different's between 6.1, 7.0, 8.0 versions?





Odoo auto commits the changes you make at the end of the transaction, why are you explicitly calling cr.commit() yourself?

odoo.ma by IBS Group
on 8/23/14, 10:30 AM

Hi. Thanks for the reply. So I need the data sometimes as well, because I make a large calculation what is take's a lot of time. Sometimes this calculation cannot reach the end of transaction, because some exception raised, but some semi finish data is important for me, and this case I lost all of the calculated data. (I working on some diff. resource planner, what is need to recalculate the production, according the daily capacity and shift happening. ) Now I working on an automatic factory builder, what is create all of the things according few input data. (repacking routings, bill of material, create the sem finish parts, work instruction, according few templates) So in this case sometimes I create 200.000 lines In mrp.routing and I lost all, if some raw data is wrong. (template not exist, wrong mrp code and so on ) That's why I need to write manually the results what is ok.

on 8/23/14, 11:49 AM

I understand, in that case I don't think there is a risk associated with this, you are simply committing your changes immediately and it's just one line of code, so it shouldn't be a problem.

odoo.ma by IBS Group
on 8/27/14, 12:20 PM

As long as you're sure that the batches of code you're committing wont cause a problem in case there is a failure, then it shouldn't be a problem. Just make sure to manage your transactions as the other member suggested in the answer to your question.

odoo.ma by IBS Group
on 8/27/14, 12:27 PM

Med Said BARA

--Med Said BARA--

| 5 5 7
--Med Said BARA--
Med Said BARA
On 8/23/14, 12:20 PM

You should NEVER call cr.commit() yourself, UNLESS you have created your own database cursor explicitly! And the situations where you need to do that are exceptional!

And by the way if you did create your own cursor, then you need to handle error cases and proper rollback, as well as properly close the cursor when you're done with it.

From: https://doc.odoo.com/contribute/15_guidelines/coding_guidelines_framework/#never-commit-the-transaction

Ok. What is the situation whit wizards? This can be commit own self?

on 8/23/14, 12:23 PM

can you be more explicit?

Med Said BARA
on 8/23/14, 12:41 PM

Hi Med. Can you explain me how to create an own database cursor, to take safe my activities?

on 8/23/14, 12:45 PM

It's in the above response;"And by the way if you did create your own cursor, then you need to handle error cases and proper rollback, as well as properly close the cursor when you're done with it." Keep in mind the "Transaction Isolation". To read: http://www.postgresql.org/docs/9.2/static/transaction-iso.html http://fossies.org/dox/odoo-7.saas-5/classopenerp_1_1sql__db_1_1Cursor.html

Med Said BARA
on 8/23/14, 2:32 PM

A good example: https://github.com/yeahliu/workflow_info/blob/master/controller.py

Med Said BARA
on 8/23/14, 3:46 PM

Thx Med. i will try it soon as possible.

on 8/24/14, 2:13 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 8/22/14, 12:30 PM
Seen: 8628 times
Last updated: 11/24/16, 6:41 PM