def open_db_connections(self, instance, db_key, db_name=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(instance, db_key, db_name)
        timeout = int(instance.get('command_timeout', self.DEFAULT_COMMAND_TIMEOUT))

        dsn, host, username, password, database, driver = self._get_access_info(instance, db_key, db_name)
        custom_tags = instance.get("tags", [])
        if custom_tags is None:
            custom_tags = []
        service_check_tags = ['host:{}'.format(host), 'db:{}'.format(database)]
        service_check_tags.extend(custom_tags)
        service_check_tags = list(set(service_check_tags))

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

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

            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=service_check_tags)
            if conn_key not in self.connections:
                self.connections[conn_key] = {'conn': rawconn, 'timeout': timeout}
            else:
                try:
                    # explicitly trying to avoid leaks...
                    self.connections[conn_key]['conn'].close()
                except Exception as e:
                    self.log.info("Could not close adodbapi db connection\n{0}".format(e))

                self.connections[conn_key]['conn'] = rawconn
        except Exception:
            cx = "{} - {}".format(host, database)
            message = "Unable to connect to SQL Server for instance {}.".format(cx)
            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=service_check_tags, message=message)

            password = instance.get('password')
            tracebk = traceback.format_exc()
            if password is not None:
                tracebk = tracebk.replace(password, "*" * 6)

            cxn_failure_exp = SQLConnectionError("{} \n {}".format(message, tracebk))
            raise cxn_failure_exp
def open_db_connections ( self , instance , db_key , db_name = None ) : conn_key = self . _conn_key ( instance , db_key , db_name ) timeout = int ( instance . get ( 'command_timeout' , self . DEFAULT_COMMAND_TIMEOUT ) ) dsn , host , username , password , database , driver = self . _get_access_info ( instance , db_key , db_name ) custom_tags = instance . get ( "tags" , [ ] ) if custom_tags is None : custom_tags = [ ] service_check_tags = [ 'host:{}' . format ( host ) , 'db:{}' . format ( database ) ] service_check_tags . extend ( custom_tags ) service_check_tags = list ( set ( service_check_tags ) ) cs = instance . get ( 'connection_string' , '' ) cs += ';' if cs != '' else '' try : if self . _get_connector ( instance ) == 'adodbapi' : cs += self . _conn_string_adodbapi ( db_key , instance = instance , db_name = db_name ) rawconn = adodbapi . connect ( cs , { 'timeout' : timeout , 'autocommit' : True } ) else : cs += self . _conn_string_odbc ( db_key , instance = instance , db_name = db_name ) rawconn = pyodbc . connect ( cs , timeout = timeout ) self . service_check ( self . SERVICE_CHECK_NAME , AgentCheck . OK , tags = service_check_tags ) if conn_key not in self . connections : self . connections [ conn_key ] = { 'conn' : rawconn , 'timeout' : timeout } else : try : self . connections [ conn_key ] [ 'conn' ] . close ( ) except Exception as e : self . log . info ( "Could not close adodbapi db connection\n{0}" . format ( e ) ) self . connections [ conn_key ] [ 'conn' ] = rawconn except Exception : cx = "{} - {}" . format ( host , database ) message = "Unable to connect to SQL Server for instance {}." . format ( cx ) self . service_check ( self . SERVICE_CHECK_NAME , AgentCheck . CRITICAL , tags = service_check_tags , message = message ) password = instance . get ( 'password' ) tracebk = traceback . format_exc ( ) if password is not None : tracebk = tracebk . replace ( password , "*" * 6 ) cxn_failure_exp = SQLConnectionError ( "{} \n {}" . format ( message , tracebk ) ) raise cxn_failure_exp
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.