SonarQube vulnerabilty in Java Singleton Class– Static Objects must be avoided, move to local members to avoid global state

huangapple go评论92阅读模式
英文:

SonarQube vulnerabilty in Java Singleton Class-- Static Objects must be avoided, move to local members to avoid global state

问题

以下是要翻译的代码部分:

private static IaddRegionDao iaddRegionDao = null;

public static IaddRegionDao getInstance() {
    if (iaddRegionDao == null) {
        iaddRegionDao = new IaddRegionDaoImpl();
    }
    return iaddRegionDao;
}

关于这段代码的问题,SonarQube 提出了以下错误提示:

静态对象必须避免,将其移到局部成员以避免全局状态

你需要将静态对象 iaddRegionDao 改为局部成员以解决这个问题。

英文:
private static IaddRegionDao iaddRegionDao=null;

public static IaddRegionDao getInstance()
{
	if(iaddRegionDao == null)
	{
		iaddRegionDao = new IaddRegionDaoImpl();
	}
	return iaddRegionDao;
}

My code above is a singleton class for Database Connection. But in SonarQube the code quality check is giving me the following error:

> Static Objects must be avoided, move to local members to avoid global state

Can anyone help me resolve this?

答案1

得分: 0

已标记为误报。有效情景。

英文:

Marked this as a false positive. Valid scenario..

huangapple
  • 本文由 发表于 2020年7月29日 02:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63140832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定