예제 #1
0
    async def begin(self, read_only: bool = False) -> ITransaction:
        """Starts a new transaction.
        """
        # already has txn registered, as long as connection is closed, it
        # is safe
        txn: typing.Optional[ITransaction] = task_vars.txn.get()
        if (txn is not None and txn.manager == self and txn.status
                in (Status.ABORTED, Status.COMMITTED, Status.CONFLICT)):
            # re-use txn if possible
            txn.status = Status.ACTIVE
            if (txn._db_conn is not None
                    and getattr(txn._db_conn, '_in_use', None) is None):
                try:
                    await self._close_txn(txn)
                except Exception:
                    logger.warn('Unable to close spurious connection',
                                exc_info=True)
        else:
            txn = Transaction(self, read_only=read_only)

        try:
            txn.user = get_authenticated_user_id()
        except RequestNotFound:
            pass

        await txn.tpc_begin()

        # make sure to explicitly set!
        task_vars.txn.set(txn)

        return txn
예제 #2
0
    async def begin(self, request=None):
        """Starts a new transaction.
        """

        if request is None:
            try:
                request = get_current_request()
            except RequestNotFound:
                pass

        user = None

        txn = None

        # already has txn registered, as long as connection is closed, it
        # is safe
        if (getattr(request, '_txn', None) is not None and request._txn.status
                in (Status.ABORTED, Status.COMMITTED, Status.CONFLICT)):
            # re-use txn if possible
            txn = request._txn
            txn.status = Status.ACTIVE
            if txn._db_conn is not None:
                try:
                    await self._close_txn(txn)
                except Exception:
                    logger.warn('Unable to close spurious connection',
                                exc_info=True)
        else:
            txn = Transaction(self, request=request)

        self._last_txn = txn

        if request is not None:
            # register tm and txn with request
            request._tm = self
            request._txn = txn
            user = get_authenticated_user_id(request)

        if user is not None:
            txn.user = user

        db_conn = self._last_db_conn = await self._storage.open()
        txn._query_count_start = _get_conn_query_count(db_conn)
        await txn.tpc_begin(db_conn)

        return txn
    async def begin(self, request=None):
        """Starts a new transaction.
        """

        db_conn = self._last_db_conn = await self._storage.open()

        if request is None:
            try:
                request = get_current_request()
            except RequestNotFound:
                pass

        user = None

        txn = None
        # already has txn registered, as long as connection is closed, it
        # is safe
        if (getattr(request, '_txn', None) is not None
                and request._txn._db_conn is None
                and request._txn.status in (Status.ABORTED, Status.COMMITTED)):
            # re-use txn if possible
            txn = request._txn
            txn.status = Status.ACTIVE
        # XXX do we want to auto clean up here? Or throw an error?
        # This will break tests that are starting multiple transactions
        # else:
        #     await self._close_txn(request._txn)
        else:
            txn = Transaction(self, request=request)

        self._last_txn = txn

        if request is not None:
            # register tm and txn with request
            request._tm = self
            request._txn = txn
            user = get_authenticated_user_id(request)

        if user is not None:
            txn.user = user

        await txn.tpc_begin(db_conn)

        return txn
예제 #4
0
    async def begin(self, request=None):
        """Starts a new transaction.
        """

        if request is None:
            try:
                request = get_current_request()
            except RequestNotFound:
                pass

        user = None

        txn = None

        # already has txn registered, as long as connection is closed, it
        # is safe
        if (getattr(request, '_txn', None) is not None and
                request._txn.status in (Status.ABORTED, Status.COMMITTED, Status.CONFLICT)):
            # re-use txn if possible
            txn = request._txn
            txn.status = Status.ACTIVE
            if (txn._db_conn is not None and
                    getattr(txn._db_conn, '_in_use', None) is None):
                try:
                    await self._close_txn(txn)
                except Exception:
                    logger.warn('Unable to close spurious connection', exc_info=True)
        else:
            txn = Transaction(self, request=request)

        self._last_txn = txn

        if request is not None:
            # register tm and txn with request
            request._tm = self
            request._txn = txn
            user = get_authenticated_user_id(request)

        if user is not None:
            txn.user = user

        await txn.tpc_begin()

        return txn