I have a map (let's call it original map), which is initially null. During service deployment and every hour after that, I need to refresh this map or basically reassing it.
Here is how I do it. In the refresh, I create a new map, and return an unmodifiableMap view of that new map, to my original map, now while this reassignment happens, that is the reference for original map gets changed, will it impact any other thread currently accessing the original map? Thing to note is during the service deployment, the original map is assigned a value in the similar fashion, basically the same refresh strategy is used.
private static Map<String, PricingPriceList> plInfoByName;
TransactionData.plInfoByName = plInfo.get(0);
Here plInfoByName is my original map, and plInfo contains a list of unmodifable maps. Here is how plInfo list is populated
Map<String, PricingPriceList> plInfoByName = new HashMap<String, PricingPriceList>();
Map<String, PricingPriceList> plInfoById = new HashMap<String, PricingPriceList>();
try {
stmt = dbConn.createStatement();
stmt.setFetchSize(10000);
rs = stmt.executeQuery(query);
PricingPriceList plDetails = null;
while (rs.next()) {
plDetails = new PricingPriceList();
//populate plDetails attributes
plInfoByName.put(rs.getString(0), plDetails);
plInfoById.put(rs.getString(1), plDetails);
}
} catch (Exception e) {
LOGGER.ERROR("Error executing refreshPlInfo. Affected in-memory objects: plInfoByName, plInfoById.", e);
} finally {
try {
if (stmt != null && !stmt.isClosed()) {
stmt.close();
}
if (rs != null && !rs.isClosed()) {
rs.close();
}
} catch (SQLException e) {
LOGGER.ERROR("refreshPlInfo failed to close SQL statement or resultset.", e);
}
}
// Return unmodifiable version
List<Map<String, PricingPriceList>> plInfo = new ArrayList<Map<String, PricingPriceList>>();
plInfo.add(Collections.unmodifiableMap(plInfoByName));
plInfo.add(Collections.unmodifiableMap(plInfoById));
return plInfo;
So when I do this, will it impact any thread reading TransactionData.plInfoByName? Or is it thread safe cause it's an unModifiableMap that is stored in it.
TransactionData.plInfoByName = plInfo.get(0);