public static ReservationDao getInstance() throws DBConnectionException {
if (dao == null) {
try {
EntityManager manager = DaoFactory.factory.createEntityManager();
dao = new ReservationDao(manager);
} catch (ServiceException e) {
// Unable to connect to database
throw new DBConnectionException(e);
}
}
return dao;
}
Spotbugs Explanation:
This method contains an unsynchronized lazy initialization of a non-volatile static field. Because the compiler or processor may reorder instructions, threads are not guaranteed to see a completely initialized object, if the method can be called by multiple threads. You can make the field volatile to correct the problem. For more information, see the Java Memory Model website.
Our Analysis:
This method tries to connect with the database, which may raise some problems when called by multiple threads. To make the code robust, volatile keyword should be used.
Ex: ReservationDaoFactory.java[lines 15-18]
Spotbugs Explanation: This method contains an unsynchronized lazy initialization of a non-volatile static field. Because the compiler or processor may reorder instructions, threads are not guaranteed to see a completely initialized object, if the method can be called by multiple threads. You can make the field volatile to correct the problem. For more information, see the Java Memory Model website.
Our Analysis: This method tries to connect with the database, which may raise some problems when called by multiple threads. To make the code robust, volatile keyword should be used.