1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Friday, November 13, 2009

Eclipse Findbugs Plugin & Double Checked Locking

This code sample below is for the creation of a singleton object:-

public static SqlConfiguration getSqlConfig(String fileName) {
if (sqlConfigSingleton == null) {
synchronized(SqlConfiguration.class) {
if (sqlConfigSingleton == null) {
try {
parseXmlConfigFile(fileName);
} catch (JDOMException e) {
log.severe(e.getMessage());
} catch (IOException e) {
log.severe(e.getMessage());
}
sqlConfigSingleton = new SqlConfiguration();
}
}
}

There are many reasons as to why the code is wrong, the most obvious one being that creation of the SqlConfiguration object and write to sqlConfigSingleton can be performed out of order. But this is not what impressed me, what impressed me was the fact that findbugs picked this up:-

Bug: Possible doublecheck on com.sqlstress.test.SqlConfiguration.sqlConfigSingleton
Pattern id: DC_DOUBLECHECK, type: DC, category: MT_CORRECTNESS

This method may contain an instance of double-checked locking. This idiom is not correct according to the semantics of the Java memory model. For more information, see the web page http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html.

Impressive right down to the fact a link is proivded to an artcile that discusses this in detail.

No comments:

 
1. 2.