def transaction(self, _filter=None, default=None, yield_resource=False):
        """
        transaction(_filter=None, default=None)

        Claims a resource from the pool for use in a thread-safe,
        reentrant manner (as part of a with statement). Resources are
        created as needed when all members of the pool are claimed or
        the pool is empty.

        :param _filter: a filter that can be used to select a member
            of the pool
        :type _filter: callable
        :param default: a value that will be used instead of calling
            :meth:`create_resource` if a new resource needs to be created
        :param yield_resource: set to True to yield the Resource object
            itself
        :type yield_resource: boolean
        """
        resource = self.acquire(_filter=_filter, default=default)
        try:
            if yield_resource:
                yield resource
            else:
                yield resource.object
            if resource.errored:
                self.delete_resource(resource)
        except BadResource:
            self.delete_resource(resource)
            raise
        finally:
            self.release(resource)