def process_request(self, request): """Begin a transaction if one doesn't already exist.""" try: commands.begin() except OperationFailure as err: message = utils.get_error_message(err) if messages.TRANSACTION_EXISTS_ERROR not in message: raise err
def make_shell_context(auto_transact=True): from modularodm import Q from framework.auth import User, Auth from framework.mongo import database from website.app import init_app from website.project.model import Node from website import models # all models from website import settings import requests from framework.transactions import commands from framework.transactions import context as tcontext app = init_app() def commit(): commands.commit() print("Transaction committed.") if auto_transact: commands.begin() print("New transaction opened.") def rollback(): commands.rollback() print("Transaction rolled back.") if auto_transact: commands.begin() print("New transaction opened.") context = { "transaction": tcontext.TokuTransaction, "start_transaction": commands.begin, "commit": commit, "rollback": rollback, "app": app, "db": database, "User": User, "Auth": Auth, "Node": Node, "Q": Q, "models": models, "run_tests": test, "rget": requests.get, "rpost": requests.post, "rdelete": requests.delete, "rput": requests.put, "settings": settings, } try: # Add a fake factory for generating fake names, emails, etc. from faker import Factory fake = Factory.create() context["fake"] = fake except ImportError: pass if auto_transact: commands.begin() return context
def test_before_request_transaction_active(self): commands.begin() transactions_before = database.command('showLiveTransactions') handlers.transaction_before_request() transactions = database.command('showLiveTransactions') assert_equal(len(transactions['transactions']), 1) assert_not_equal( transactions_before['transactions'][0]['txnid'], transactions['transactions'][0]['txnid'], )
def __enter__(self): try: commands.begin(self.database) self.pending = True except OperationFailure as error: message = utils.get_error_message(error) if messages.TRANSACTION_EXISTS_ERROR not in message: raise logger.warn('Transaction already in progress') return self
def make_shell_context(auto_transact=True): from modularodm import Q from framework.auth import User, Auth from framework.mongo import database from website.app import init_app from website.project.model import Node from website import models # all models from website import settings import requests from framework.transactions import commands from framework.transactions import context as tcontext app = init_app() def commit(): commands.commit() print('Transaction committed.') if auto_transact: commands.begin() print('New transaction opened.') def rollback(): commands.rollback() print('Transaction rolled back.') if auto_transact: commands.begin() print('New transaction opened.') context = { 'transaction': tcontext.TokuTransaction, 'start_transaction': commands.begin, 'commit': commit, 'rollback': rollback, 'app': app, 'db': database, 'User': User, 'Auth': Auth, 'Node': Node, 'Q': Q, 'models': models, 'run_tests': test, 'rget': requests.get, 'rpost': requests.post, 'rdelete': requests.delete, 'rput': requests.put, 'settings': settings, } try: # Add a fake factory for generating fake names, emails, etc. from faker import Factory fake = Factory.create() context['fake'] = fake except ImportError: pass if auto_transact: commands.begin() return context
def test_after_request_uncaught_exception(self): commands.begin() key = 'test_after_request_uncaught_exception' database['txn'].insert({'_id': key}) response = make_response('ack!', 500) handlers.transaction_after_request(response) transactions = database.command('showLiveTransactions') assert_equal(len(transactions['transactions']), 0) assert_equal( database['txn'].find({'_id': key}).count(), 0, )
def transaction_before_request(): """Setup transaction before handling the request. """ if view_has_annotation(NO_AUTO_TRANSACTION_ATTR): return None try: commands.rollback() logger.error('Transaction already in progress; rolling back.') except OperationFailure as error: message = utils.get_error_message(error) if messages.NO_TRANSACTION_ERROR not in message: raise commands.begin()
def test_after_request_uncaught_exception(self): commands.begin() key = 'test_after_request_uncaught_exception' database['txn'].insert({'_id': key}) response = make_response('ack!', 500) handlers.transaction_after_request(response) transactions = database.command('showLiveTransactions') assert_equal(len(transactions['transactions']), 0) assert_equal( database['txn'].find({ '_id': key }).count(), 0, )
def test_after_request_lock_error(self): commands.begin() key = 'test_after_request_lock_error' database['txn'].insert({'_id': key}) with app.test_request_context(content_type='application/json'): response = make_response('bob', 200) with mock.patch('framework.transactions.commands.commit') as mock_commit: mock_commit.side_effect = OperationFailure(messages.LOCK_ERROR) handlers.transaction_after_request(response) transactions = database.command('showLiveTransactions') assert_equal(len(transactions['transactions']), 0) assert_equal( database['txn'].find({'_id': key}).count(), 0, )
def test_after_request_lock_error(self): commands.begin() key = 'test_after_request_lock_error' database['txn'].insert({'_id': key}) with app.test_request_context(content_type='application/json'): response = make_response('bob', 200) with mock.patch( 'framework.transactions.commands.commit') as mock_commit: mock_commit.side_effect = OperationFailure(messages.LOCK_ERROR) handlers.transaction_after_request(response) transactions = database.command('showLiveTransactions') assert_equal(len(transactions['transactions']), 0) assert_equal( database['txn'].find({ '_id': key }).count(), 0, )
def rollback(): commands.rollback() print('Transaction rolled back.') if auto_transact: commands.begin() print('New transaction opened.')
def commit(): commands.commit() print('Transaction committed.') if auto_transact: commands.begin() print('New transaction opened.')