Avoid OpenERP Outreach Mistakes

I would like to manage the OpenERP user activity by installing the audittrail module.
After creating some rules (determine which user, which object and what kind of activity (create, update ..) will be controlled). I am updating the product to see how it works.
When I tried to update the product, I received a system error. When I see the log, I get [2010-08-31 12: 53: 35,042] The cursor is not explicitly closed
[2010-08-31 12: 53: 35,043] The cursor was created in / home / pilgrim / working / sources / addons / audittrail / audittrail.py:204
Here is the line that causes the error cr = pooler.get_db (db) .cursor ()
Looking at sql_db.py, I get a comment

def __del__(self):
    if not self.__closed:
        # Oops. 'self' has not been closed explicitly.
        # The cursor will be deleted by the garbage collector,
        # but the database connection is not put back into the connection
        # pool, preventing some operation on the database like dropping it.
        # This can also lead to a server overload.
        msg = "Cursor not closed explicitly\n"  \
              "Cursor was created at %s:%s" % self.__caller
        log(msg, netsvc.LOG_WARNING)
        self.close()

Python, , ?
, ?

+3
3

t , , . , , , .

cr = sqldb.db_connect(dbname).cursor()
.........
cr.close()
cr = None

audittrail.py, . , .

, , , . .

+4

, . .

def a():  
  try:
    print 'before return '
    return 1
  finally:
    print 'in finally'

a()

before return 
in finally
1

. . ( audittrail.py)

def do_something_with_db(db):
   // open cusror again
   cr = db.cursor()
   // do somethign
   // close cursor internally
   cr.close()
def execute(db)
   // 1, open connection and open cursor
   cr = db.cursor
   try:
        //2, do something with db, seeing that this method will open cursor again
       return do_something_with_db(db)
   finally:
       cr.close()

, do_something_with_db ( ), . , : cr

Before
**do_something_with_db(db)**
after
**do_something_with_db(cr)**

.

@Don Kirkby: , ...

+2

OpenERP , PyDev Eclipse? , . , , , log_fct(). ( , 207, ?) , :

def log_fct(self, db, uid, passwd, object, method, fct_src, *args):
    logged_uids = []
    pool = pooler.get_pool(db)
    cr = pooler.get_db(db).cursor() # line 207 in version 5.0.12

    # ...

    if method in ('create'):

        # ...

        cr.close()
        return res_id

    # ...

    cr.close()

, return, cr.close(), . . , - :

    logger = netsvc.Logger()
    logger.notifyChannel('audittrail', netsvc.LOG_INFO, 'something happened')

Update: , . , , . try... finally , , . , :

def log_fct(self, db, uid, passwd, object, method, fct_src, *args):
    logged_uids = []
    pool = pooler.get_pool(db)
    cr = pooler.get_db(db).cursor() # line 207 in version 5.0.12
    try:

        # ...

        if method in ('create'):

            # ...

            return res_id

        # ...

    finally:
        cr.close()
+1

Source: https://habr.com/ru/post/1762408/


All Articles