Odoo Help


How to get a new cursor on new api on Thread

Maxim Litnitskiy
on 9/22/14, 5:42 AM 11,555 views

Found from here - http://odoo-new-api-guide-line.readthedocs.org/en/latest/conventions.html:

Using Thread

When using thread you have to create you own cursor and initiate a new environment for each thread. committing is done by committing the cursor:

with Environment.manage(): # class function

               env = Environment(cr, uid, context)

But how to use it?

We have old api code:


        with Environment.manage():

            cr = self.pool.cursor()

            dialer = self.pool['asterisk.dialer'].browse(cr, uid, ids, context=context)[0]


It works.

Now how to do the same with new api?


        with Environment.manage():

            dialer = self.env['asterisk.dialer'].browse([dialer_id])


Gives the following error: OperationalError: Unable to use a closed cursor.


Tried this:

def run_dialer(self, dialer_id):

        with Environment.manage():

            cr, uid, context = self.env.args

            env = Environment(cr, uid, context)

            dialer = env['asterisk.dialer'].browse([dialer_id])

            domain = [('phone', '!=', None)] + [eval(dialer.dialer_domain)[0]]

            contacts = env[dialer.dialer_model].search(domain)


The same:

Exception in thread Thread-7:

Traceback (most recent call last):

  File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner


  File "/usr/lib/python2.7/threading.py", line 761, in run

    self.__target(*self.__args, **self.__kwargs)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/api.py", line 235, in wrapper

    return new_api(self, *args, **kwargs)

  File "/home/max/tmp-work/odoo/myaddons/asterisk_dialer/models/dialer.py", line 98, in run_dialer

    contacts = env[dialer.dialer_model].search(domain)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/api.py", line 235, in wrapper

    return new_api(self, *args, **kwargs)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/api.py", line 464, in new_api

    result = method(self._model, cr, uid, *args, **kwargs)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/models.py", line 1679, in search

    return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/api.py", line 237, in wrapper

    return old_api(self, *args, **kwargs)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/addons/base/res/res_partner.py", line 643, in _search

    count=count, access_rights_uid=access_rights_uid)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/api.py", line 237, in wrapper

    return old_api(self, *args, **kwargs)

  File "/home/max/tmp-work/odoo/odoo.my/openerp/models.py", line 4543, in _search

    res = cr.fetchall()

  File "/home/max/tmp-work/odoo/odoo.my/openerp/sql_db.py", line 157, in wrapper

    raise psycopg2.OperationalError(msg)


FYI this work for me:

with openerp.api.Environment.manage():
    with openerp.registry(self.env.cr.dbname).cursor() as new_cr:
        # Create a new environment with new cursor database
        new_env = api.Environment(new_cr, self.env.uid, self.env.context)
        # with_env replace original env for this method
        self.with_env(new_env).write({'name': 'hello'})  # isolated transaction to commit
        new_env.cr.commit()  # Don't show a invalid-commit in this case
    # You don't need close your cr because is closed when finish "with"
# You don't need clear caches because is cleared when finish "with"

What does `Environment.manage()` ?

on 3/15/16, 12:47 PM
Maxim Litnitskiy
On 9/26/14, 8:56 AM

The following seem to work reliably:

            new_cr = sql_db.db_connect(self.env.cr.dbname).cursor()
            uid, context = self.env.uid, self.env.context
            with api.Environment.manage():
                self.env = api.Environment(new_cr, uid, context)



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

1 follower(s)


Asked: 9/22/14, 5:42 AM
Seen: 11555 times
Last updated: 4/8/16, 11:53 AM