Vulnerability scan report

Viewed 26
public MQTTClient(MQTTConnectionInfo conn, ConnectionMgr conmgr) throws Exception {
        try {
            clientId = conn.getClientId();
            mqttClient = ClientFactory.getConnection(conn, conmgr);
            username =conn.getUsername();
            log.info("-- inside MqttClient == While Connecting : "+username.toString() + mqttClient.getState());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        
    }

This resource is not properly closed and released in all situations. how to close getConnection()

1 Answers

You could use a finally block which is executed after the try or the catch:

        try {
            mqttClient = ClientFactory.getConnection(conn, conmgr);
            ...
        } catch (Exception e) {
            ...
        } finally {
            mqttClient.close();
        }

Or if the client class implements java.lang.AutoClosable you could use a try-with-resources statement:

        try (MQTTClient mqttClient = ClientFactory.getConnection(conn, conmgr)) {
            
            mqttClient.doSomething()
        } catch (Exception e) {
            ...
        }
Related