Search code examples
javaspring-bootspotbugs

build failed Error: java: USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES how to fix spot bug reported issue


I am facing Error: java: USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES and build gets failed for setter method. it is reported by spotbugs. How to fix this please help as not getting solution. Below is the class MySuperServiceConfig.

@Component
@ConfigurationProperties("mysuperservice")
@PropertySource("classpath:data.properties")
public class MySuperServiceConfig {
  private String username;
  private String password;
  private List<String> schemadata;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public List<String> getSchemadata() {
    return schemadata;
  }

  public void setSchemadata(List<String> schemadata) {
    this.schemadata = schemadata;
  }
}

Below is the log

[INFO] --- spotbugs-maven-plugin:3.1.12.2:check (default) @ aif-handler ---
[INFO] BugInstance size is 3
[INFO] Error size is 0
[INFO] Total bugs: 3
[ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setPassword(String) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:[line 29] USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES
[ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setSchemadata(List) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:[line 37] USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES
[ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setUsername(String) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:[line 21] USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES

Solution

  • The spotbug documentation has some information on this error

    http://fb-contrib.sourceforge.net/bugdescriptions.html

    USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES

    This method writes to a field of this class. Since this class is seen as a Singleton this can produce race conditions, or cause non-visible changes to other threads, because the field isn't accessed synchronously.

    As the Configuration Property class is marked with Component annotation, Spotbug would know that this is a Singleton Bean. The Configuration bean is not autowiring any of the fields - username / password / schemaData, but it has a getter and setter methods. So, it is appearing like these instance properties of the bean or dependancies could be changed multiple times by the code. In such a scenario, there could be race conditions as the methods are not synchronized. Hence, the fix should be to remove this Component annotation as this is a Configuration Property mapper class.

    To have this autowired, you can -

    1. use @EnableConfigurationProperties(MySuperServiceConfig.class) on SpringBootApplication class or another Configuration class
    2. Mark this as a @Configuration class