英文:
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..
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论