(self, db_key, db_name=None, is_default=True, key_prefix=None)
We open the db connections explicitly, so we can ensure they are open
        before we use them, and are closable, once we are finished. Open db
        connections keep locks on the db, presenting issues such as the SQL
        Server Agent being unable to stop.
We open the db connections explicitly, so we can ensure they are open
        before we use them, and are closable, once we are finished. Open db
        connections keep locks on the db, presenting issues such as the SQL
        Server Agent being unable to stop.
We open the db connections explicitly so we can ensure they are open before we use them and are closable once we are finished . Open db connections keep locks on the db presenting issues such as the SQL Server Agent being unable to stop .
def open_db_connections(self, db_key, db_name=None, is_default=True, key_prefix=None):
        """
        We open the db connections explicitly, so we can ensure they are open
        before we use them, and are closable, once we are finished. Open db
        connections keep locks on the db, presenting issues such as the SQL
        Server Agent being unable to stop.
        """
        conn_key = self._conn_key(db_key, db_name, key_prefix)

        _, host, _, _, database, _ = self._get_access_info(db_key, db_name)

        cs = self.instance.get('connection_string', '')
        cs += ';' if cs != '' else ''

        self._connection_options_validation(db_key, db_name)

        try:
            if self.connector == 'adodbapi':
                cs += self._conn_string_adodbapi(db_key, db_name=db_name)
                # autocommit: true disables implicit transaction
                rawconn = adodbapi.connect(cs, {'timeout': self.timeout, 'autocommit': True})
            else:
                cs += self._conn_string_odbc(db_key, db_name=db_name)
                rawconn = pyodbc.connect(cs, timeout=self.timeout, autocommit=True)
                rawconn.timeout = self.timeout

            self.service_check_handler(AgentCheck.OK, host, database, is_default=is_default)
            if conn_key not in self._conns:
                self._conns[conn_key] = rawconn
            else:
                try:
                    # explicitly trying to avoid leaks...
                    self._conns[conn_key].close()
                except Exception as e:
                    self.log.info("Could not close adodbapi db connection\n%s", e)

                self._conns[conn_key] = rawconn
            self._setup_new_connection(rawconn)
        except Exception as e:
            cx = "{} - {}".format(host, database)

            if is_default:
                message = "Unable to connect to SQL Server for instance {}: {}".format(cx, repr(e))
            else:
                message = "Unable to connect to Database: {} for instance {}: {}".format(database, host, repr(e))
            password = self.instance.get('password')
            if password is not None:
                message = message.replace(password, "*" * 6)

            self.service_check_handler(AgentCheck.CRITICAL, host, database, message, is_default=is_default)

            raise_from(SQLConnectionError(message), None)
def open_db_connections ( self , db_key , db_name = None , is_default = True , key_prefix = None ) : conn_key = self . _conn_key ( db_key , db_name , key_prefix ) _ , host , _ , _ , database , _ = self . _get_access_info ( db_key , db_name ) cs = self . instance . get ( 'connection_string' , '' ) cs += ';' if cs != '' else '' self . _connection_options_validation ( db_key , db_name ) try : if self . connector == 'adodbapi' : cs += self . _conn_string_adodbapi ( db_key , db_name = db_name ) # autocommit: true disables implicit transaction rawconn = adodbapi . connect ( cs , { 'timeout' : self . timeout , 'autocommit' : True } ) else : cs += self . _conn_string_odbc ( db_key , db_name = db_name ) rawconn = pyodbc . connect ( cs , timeout = self . timeout , autocommit = True ) rawconn . timeout = self . timeout self . service_check_handler ( AgentCheck . OK , host , database , is_default = is_default ) if conn_key not in self . _conns : self . _conns [ conn_key ] = rawconn else : try : # explicitly trying to avoid leaks... self . _conns [ conn_key ] . close ( ) except Exception as e : self . log . info ( "Could not close adodbapi db connection\n%s" , e ) self . _conns [ conn_key ] = rawconn self . _setup_new_connection ( rawconn ) except Exception as e : cx = "{} - {}" . format ( host , database ) if is_default : message = "Unable to connect to SQL Server for instance {}: {}" . format ( cx , repr ( e ) ) else : message = "Unable to connect to Database: {} for instance {}: {}" . format ( database , host , repr ( e ) ) password = self . instance . get ( 'password' ) if password is not None : message = message . replace ( password , "*" * 6 ) self . service_check_handler ( AgentCheck . CRITICAL , host , database , message , is_default = is_default ) raise_from ( SQLConnectionError ( message ) , None )