Beispiel #1
0
    def _check_connection(self):
        """
        Return false if the connection is broken

        :rtype: bool
        """
        # If the connection is not present return False
        if not self._conn:
            return False

        # Check if the connection works by running 'SELECT 1'
        cursor = None
        try:
            cursor = self._conn.cursor()
            cursor.execute(self.CHECK_QUERY)
        except psycopg2.DatabaseError:
            # Connection is broken, so we need to reconnect
            self.close()
            # Raise an error if reconnect is not allowed
            if not self.allow_reconnect:
                raise PostgresConnectionError(
                    "Connection lost, reconnection not allowed")
            return False
        finally:
            if cursor:
                cursor.close()

        return True
Beispiel #2
0
 def connect(self):
     """
     Generic function for Postgres connection (using psycopg2)
     """
     if not self._conn:
         try:
             self._conn = psycopg2.connect(self.conninfo)
         # If psycopg2 fails to connect to the host,
         # raise the appropriate exception
         except psycopg2.DatabaseError as e:
             raise PostgresConnectionError(str(e).strip())
         # Register the connection to the live connections list
         _live_connections.append(self)
     return self._conn
Beispiel #3
0
    def connect(self):
        """
        Generic function for Postgres connection (using psycopg2)
        """
        if self._conn and self._conn.closed:
            # Close the broken connection and let the following code to open
            # it again
            self.close()
            # Raise an error if the connection is broken
            # and reconnect is not allowed
            if not self.allow_reconnect:
                raise PostgresConnectionError(
                    "The connection is broken and reconnection is not allowed")

        if not self._conn:
            try:
                self._conn = psycopg2.connect(self.conninfo)
            # If psycopg2 fails to connect to the host,
            # raise the appropriate exception
            except psycopg2.DatabaseError as e:
                raise PostgresConnectionError(str(e).strip())
            # Register the connection to the live connections list
            _live_connections.append(self)
        return self._conn
Beispiel #4
0
    def connect(self):
        """
        Connect to the PostgreSQL server. It reuses an existing connection.
        """
        if self._conn:
            return self._conn

        self._conn = super(PostgreSQLConnection, self).connect()
        if (self._conn.server_version >= 90000 and
                'application_name' not in self.conn_parameters):
            try:
                cur = self._conn.cursor()
                cur.execute('SET application_name TO barman')
                cur.close()
            # If psycopg2 fails to set the application name,
            # raise the appropriate exception
            except psycopg2.ProgrammingError as e:
                raise PostgresConnectionError(
                    "Cannot set the application name: %s" % str(e).strip())
        return self._conn