def test_table_lock(self): "Test table lock" pool = Pool() Model = pool.get('test.modelsql.lock') transaction = Transaction() with transaction.new_transaction(): Model.lock() with transaction.new_transaction(): with self.assertRaises(backend.DatabaseOperationalError): Model.lock()
def test_lock(self): "Test lock" pool = Pool() Model = pool.get('test.modelsql.lock') transaction = Transaction() record_id = Model.create([{}])[0].id transaction.commit() with transaction.new_transaction(): record = Model(record_id) record.lock() with transaction.new_transaction(): record = Model(record_id) with self.assertRaises(backend.DatabaseOperationalError): record.lock()
def test_memory_cache_transactions(self): "Test MemoryCache with concurrent transactions" transaction1 = Transaction().start(DB_NAME, USER) self.addCleanup(transaction1.stop) cache.set('foo', 'bar') self.assertEqual(cache.get('foo'), 'bar') transaction2 = transaction1.new_transaction() self.addCleanup(transaction2.stop) cache.clear() self.assertEqual(cache.get('foo'), None) cache.set('foo', 'baz') self.assertEqual(cache.get('foo'), 'baz') Transaction().set_current_transaction(transaction1) self.addCleanup(transaction1.stop) self.assertEqual(cache.get('foo'), 'bar') transaction2.commit() for n in range(10): if cache.get('foo') is None: break self.wait_cache_sync() self.assertEqual(cache.get('foo'), None)
def test_memory_cache_old_transaction(self): "Test old transaction does not fill cache" transaction1 = Transaction().start(DB_NAME, USER) self.addCleanup(transaction1.stop) # Clear cache from new transaction transaction2 = transaction1.new_transaction() self.addCleanup(transaction2.stop) cache.clear() transaction2.commit() # Set value from old transaction Transaction().set_current_transaction(transaction1) self.addCleanup(transaction1.stop) cache.set('foo', 'baz') # New transaction has still empty cache transaction3 = transaction1.new_transaction() self.addCleanup(transaction3.stop) self.assertEqual(cache.get('foo'), None)
def transition_upgrade(self): pool = Pool() Module = pool.get('ir.module') Lang = pool.get('ir.lang') transaction = Transaction() with transaction.new_transaction(): modules = Module.search([ ('state', 'in', ['to upgrade', 'to remove', 'to activate']), ]) update = [m.name for m in modules] langs = Lang.search([ ('translatable', '=', True), ]) lang = [x.code for x in langs] if update: pool.init(update=update, lang=lang) Cache.refresh_pool(transaction) return 'done'
def run(cls, db_name): transaction = Transaction() logger.info('cron started for "%s"', db_name) now = datetime.datetime.now() retry = config.getint('database', 'retry') with transaction.start(db_name, 0, context={'_skip_warnings': True}): pool = Pool() Error = pool.get('ir.error') cls.lock() crons = cls.search([ 'OR', ('next_call', '<=', now), ('next_call', '=', None), ]) for cron in crons: name = '<Cron %s@%s %s>' % (cron.id, db_name, cron.method) logger.info("%s started", name) for count in range(retry, -1, -1): if count != retry: time.sleep(0.02 * (retry - count)) try: with processing(name), \ transaction.new_transaction() as cron_trans: cron.run_once() cron_trans.commit() except Exception as e: if (isinstance(e, backend.DatabaseOperationalError) and count): continue if isinstance(e, (UserError, UserWarning)): Error.log(cron, e) logger.info('%s failed', name) else: logger.critical('%s failed', name, exc_info=True) cron.next_call = cron.compute_next_call(now) cron.save() break while transaction.tasks: task_id = transaction.tasks.pop() run_task(db_name, task_id) logger.info('cron finished for "%s"', db_name)
def test_memory_cache_transactions(self): "Test MemoryCache with concurrent transactions" transaction1 = Transaction().start(DB_NAME, USER) self.addCleanup(transaction1.stop) cache.set('foo', 'bar') self.assertEqual(cache.get('foo'), 'bar') transaction2 = transaction1.new_transaction() self.addCleanup(transaction2.stop) cache.clear() self.assertEqual(cache.get('foo'), None) cache.set('foo', 'baz') self.assertEqual(cache.get('foo'), 'baz') with Transaction().set_current_transaction(transaction1): self.assertEqual(cache.get('foo'), 'bar') commit_time = dt.datetime.now() transaction2.commit() self.wait_cache_sync(after=commit_time) self.assertEqual(cache.get('foo'), 'baz')