Example #1
0
    def borrow(self, dsn):
        self._debug('Borrow connection to %s' % (dsn,))

        # free leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                self._debug('Free leaked connection to %s' % (cnx.dsn,))

        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                self._connections.append((cnx, True))
                self._debug('Existing connection found at index %d' % i)

                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    self._debug('Removing old connection at index %d: %s' % (i, cnx.dsn))
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')

        result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
Example #2
0
    def borrow(self, dsn):
        self._debug('Borrow connection to %r', dsn)

        # free leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                self.__logger.warn('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                self._connections.append((cnx, True))
                self._debug('Existing connection found at index %d', i)

                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    self._debug('Removing old connection at index %d: %r', i, cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')

        try:
            result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
        except psycopg2.Error, e:
            self.__logger.exception('Connection to the database failed')
            raise
Example #3
0
    def borrow(self, dsn):
        self._debug('Borrow connection to %r', dsn)

        # free leaked connections
        for i, (cnx, _, t) in tools.reverse_enumerate(self._connections):
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False, t))
                self.__logger.warn('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used, t) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                if time.time() - t > 3600:
                    cnx.close()
                    try:
                        cnx = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
                        t = time.time()
                    except psycopg2.Error, e:
                        self.__logger.exception('Connection to the database failed')
                        raise
                self._connections.append((cnx, True, t))
                self._debug('Existing connection found at index %d', i)

                return cnx
Example #4
0
    def get_weights(self, item, question):
        """Returns weights of previous answers to the given item.

        :param item: *Item* (i.e. practiced place by a user).
        :type item: :class:`Item`
        :param question: Asked question.
        :type question: :class:`pandas.Series` or :class:`Question`
        """
        correct_weights = [
            ans.is_correct * self.decay ** k for k, ans
            in tools.reverse_enumerate(item.practices)
        ]
        incorrect_weights = [
            (1 - ans.is_correct) * self.decay ** k for k, ans
            in tools.reverse_enumerate(item.practices)
        ]
        return sum(correct_weights), sum(incorrect_weights)
Example #5
0
    def borrow(self, connection_info):
        """
        :param dict connection_info: dict of psql connection keywords
        :rtype: PsycoConnection
        """
        # free dead and leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if cnx.closed:
                self._connections.pop(i)
                self._debug('Removing closed connection at index %d: %r', i, cnx.dsn)
                continue
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                _logger.info('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and cnx._original_dsn == connection_info:
                try:
                    cnx.reset()
                except psycopg2.OperationalError:
                    self._debug('Cannot reset connection at index %d: %r', i, cnx.dsn)
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
                    if not cnx.closed:
                        cnx.close()
                    continue
                self._connections.pop(i)
                self._connections.append((cnx, True))
                self._debug('Borrow existing connection to %r at index %d', cnx.dsn, i)

                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    if not cnx.closed:
                        cnx.close()
                    self._debug('Removing old connection at index %d: %r', i, cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')

        try:
            result = psycopg2.connect(
                connection_factory=PsycoConnection,
                **connection_info)
        except psycopg2.Error:
            _logger.info('Connection to the database failed')
            raise
        result._original_dsn = connection_info
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
Example #6
0
 def close_all(self, dsn=None):
     count = 0
     last = None
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn is None or cnx._original_dsn == dsn:
             cnx.close()
             last = self._connections.pop(i)[0]
             count += 1
     _logger.info('%r: Closed %d connections %s', self, count,
                 (dsn and last and 'to %r' % last.dsn) or '')
Example #7
0
 def close_all(self, dsn=None):
     count = 0
     last = None
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn is None or cnx._original_dsn == dsn:
             cnx.close()
             last = self._connections.pop(i)[0]
             count += 1
     _logger.info('%r: Closed %d connections %s', self, count,
                  (dsn and last and 'to %r' % last.dsn) or '')
Example #8
0
    def borrow(self, dsn):
        self._debug('Borrow connection to %r', dsn)

        # free dead and leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if cnx.closed:
                self._connections.pop(i)
                self._debug('Removing closed connection at index %d: %r', i,
                            cnx.dsn)
                continue
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                _logger.warning('%r: Free leaked connection to %r', self,
                                cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                try:
                    cnx.reset()
                except psycopg2.OperationalError:
                    self._debug('Cannot reset connection at index %d: %r', i,
                                cnx.dsn)
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
                    if not cnx.closed:
                        cnx.close()
                    continue
                self._connections.pop(i)
                self._connections.append((cnx, True))
                self._debug('Existing connection found at index %d', i)

                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    self._debug('Removing old connection at index %d: %r', i,
                                cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')

        try:
            result = psycopg2.connect(dsn=dsn,
                                      connection_factory=PsycoConnection)
        except psycopg2.Error:
            _logger.exception('Connection to the database failed')
            raise
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
Example #9
0
    def borrow(self, dsn):
        self._debug('Borrow connection to %r', dsn)

        # free dead and leaked connections
        time_now = time.time()
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if (cnx.closed or (not cnx.executing and time_now - cnx.init_time > CONNECTION_LIFETIME) or (cnx.executing and CURSOR_TIMEOUT and time_now - cnx.last_execution_time > CURSOR_TIMEOUT)):
                self._connections.pop(i)
                self._debug('Removing closed connection at index %d: %r', i, cnx.dsn)
                if not cnx.closed:
                    cnx.close() # Close unclosed connection.
                continue
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                _logger.warning('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                try:
                    cnx.reset()
                except psycopg2.OperationalError:
                    self._debug('Cannot reset connection at index %d: %r, removing it', i, cnx.dsn)
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
                    if not cnx.closed:
                        cnx.close()
                    continue
                self._connections.append((cnx, True))
                self._debug('Existing connection found at index %d', i)
                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    cnx.close()
                    self._debug('Removing old connection at index %d: %r', i, cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')
        try:
            result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
            result.init_time = time.time()
            result.executing = False
        except psycopg2.Error:
            _logger.exception('Connection to the database failed')
            raise
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
Example #10
0
    def borrow(self, dsn, do_cursor=False):
        self._debug_dsn('Borrow connection to %r', dsn)

        # free leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                self._debug_dsn('Free leaked connection to %r', cnx.dsn)

        result = None
        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                try:
                   if psycopg2.__version__ >= '2.2' :
                        pr = cnx.poll()
                        self._debug("Poll: %d", pr)
                except OperationalError, e:
                    self._debug("Error in poll: %s" % e)
                    continue
                
                if cnx.closed or not cnx.status:
                    # something is wrong with that connection, let it out
                    self._debug("Troubled connection ")
                    continue
                
                self._debug('Existing connection found at index %d', i)
                if do_cursor:
                    try:
                        cur = cnx.cursor(cursor_factory=psycopg1cursor)
                        if psycopg2.__version__ < '2.2' and not cur.isready():
                            continue
                        if cur.closed:
                            continue
                        self._connections.insert(i,(cnx, True))

                        result = (cnx, cur)
                    except OperationalError:
                        continue
                else:
                    self._connections.insert(i,(cnx, True))
                    result = cnx
                break
Example #11
0
    def to_sql(self):
        stack = []
        params = []
        for i, e in reverse_enumerate(self.__exp):
            if self._is_leaf(e, internal=True):
                table = self.__tables.get(i, self.__main_table)
                q, p = self.__leaf_to_sql(e, table)
                params.insert(0, p)
                stack.append(q)
            else:
                if e == '!':
                    stack.append('(NOT (%s))' % (stack.pop(),))
                else:
                    ops = {'&': ' AND ', '|': ' OR '}
                    q1 = stack.pop()
                    q2 = stack.pop()
                    stack.append('(%s %s %s)' % (q1, ops[e], q2,))

        query = ' AND '.join(reversed(stack))
        joins = ' AND '.join(map(lambda j: j[0], self.__joins))
        if joins:
            query = '(%s) AND (%s)' % (joins, query)
        return (query, flatten(params))
Example #12
0
 def close_all(self, dsn):
     _logger.info('%r: Close all connections to %r', self, dsn)
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn_are_equals(cnx.dsn, dsn):
             cnx.close()
             self._connections.pop(i)
Example #13
0
 def close_all(self, dsn):
     self._debug('Close all connections to %r', dsn)
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn_are_equals(cnx.dsn, dsn):
             cnx.close()
             self._connections.pop(i)
Example #14
0
    def borrow(self, connection_info):
        """
        :param dict connection_info: dict of psql connection keywords
        :rtype: PsycoConnection
        """
        # free dead and leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if cnx.closed:
                self._connections.pop(i)
                self._debug('Removing closed connection at index %d: %r', i,
                            cnx.dsn)
                continue
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                _logger.info('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and cnx._original_dsn == connection_info:
                try:
                    cnx.reset()
                except psycopg2.OperationalError:
                    self._debug('Cannot reset connection at index %d: %r', i,
                                cnx.dsn)
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
                    if not cnx.closed:
                        cnx.close()
                    continue
                except InternalError:
                    self._debug(
                        'Cannot reset connection at index %d: %r cause InternalError',
                        i, cnx.dsn)
                    if not cnx.closed:
                        cnx.close()
                    continue
                self._connections.pop(i)
                self._connections.append((cnx, True))
                self._debug('Borrow existing connection to %r at index %d',
                            cnx.dsn, i)

                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    if not cnx.closed:
                        cnx.close()
                    self._debug('Removing old connection at index %d: %r', i,
                                cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')

        try:
            result = psycopg2.connect(connection_factory=PsycoConnection,
                                      **connection_info)
        except psycopg2.Error:
            _logger.info('Connection to the database failed')
            raise
        result._original_dsn = connection_info
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
Example #15
0
 def close_all(self, dsn):
     self._debug('Close all connections to %s' % (dsn,))
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn_are_equals(cnx.dsn, dsn):
             cnx.close()
             self._connections.pop(i)
Example #16
0
 def close_all(self, dsn=None):
     _logger.info('%r: Close all connections to %r', self, dsn)
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn is None or cnx._original_dsn == dsn:
             cnx.close()
             self._connections.pop(i)
Example #17
0
 def close_all(self, dsn):
     _logger.info('%r: Close all connections to %r', self, dsn)
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn_are_equals(cnx.dsn, dsn):
             cnx.close()
             self._connections.pop(i)
Example #18
0
    def borrow(self, dsn):
        self._debug('Borrow connection to %r', dsn)

        # free dead and leaked connections
        time_now = time.time()
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if (cnx.closed
                    or cnx.status == psycopg2.extensions.STATUS_READY and
                (CONNECTION_LIFETIME
                 and time_now - cnx.init_time > CONNECTION_LIFETIME) or
                (CURSOR_TIMEOUT
                 and time_now - cnx.last_execution_time > CURSOR_TIMEOUT)):
                self._connections.pop(i)
                self._debug('Removing closed connection at index %d: %r', i,
                            cnx.dsn)
                if not cnx.closed:
                    cnx.close()  # Close unclosed connection.
                continue
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                _logger.warning('%r: Free leaked connection to %r', self,
                                cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                try:
                    cnx.reset()
                except psycopg2.OperationalError:
                    self._debug(
                        'Cannot reset connection at index %d: %r, removing it',
                        i, cnx.dsn)
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
                    if not cnx.closed:
                        cnx.close()
                    continue
                self._connections.append((cnx, True))
                self._debug('Existing connection found at index %d', i)
                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    self._debug('Removing old connection at index %d: %r', i,
                                cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')
        try:
            result = psycopg2.connect(dsn=dsn,
                                      connection_factory=PsycoConnection)
            result.init_time = time.time()
            result.executing = False
        except psycopg2.Error:
            _logger.exception('Connection to the database failed')
            raise
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result