I have an application scoped managed bean which main purpose is to serve the rest of the application with less dynamic data such as all available languages and a few more things.
ApplicationController
@ManagedBean(eager=true)
@ApplicationScoped
public class ApplicationController implements Seria开发者_StackOverflow中文版lizable {
private static final long serialVersionUID = 25488214212L;
private List<Language> languages;
private Map<Language, List<LevelDescription>> descriptionsPersonal;
private Map<Language, List<LevelDescription>> descriptionsTechnical;
private List<Integer> levels = new ArrayList<Integer>();
@EJB private LanguageDao languageDao;
@EJB private LevelDescriptionDao levelDescriptionDao;
@EJB private IntraConnectionBean intraBean;
@EJB private ApplicationBean appBean;
public ApplicationController() {
}
@PostConstruct
public void init(){
languages = languageDao.findAll();
descriptionsTechnical = new HashMap<Language, List<LevelDescription>>();
descriptionsPersonal = new HashMap<Language, List<LevelDescription>>();
for(int i = 0; i < 6; i++)
levels.add(i);
for(Language l : languages){
List<LevelDescription> desc = levelDescriptionDao.findAll(l, true);
if(!desc.isEmpty())
descriptionsTechnical.put(l, desc);
desc = levelDescriptionDao.findAll(l, false);
if(!desc.isEmpty())
descriptionsPersonal.put(l, desc);
}
}
public List<Language> getLanguages(){
if(lang)
return languages;
}
public List<LevelDescription> getTechnicalItems(Language lang) {
return descriptionsTechnical.get(lang);
}
public List<LevelDescription> getPersonalItems(Language lang) {
return descriptionsPersonal.get(lang);
}
public List<Integer> getLevels(){
return levels;
}
}
This seems to work fine. For a while. When leaving the application alone for some time, maybe an hour, I get extremly strange behaviour. The get-methods seems to either start returning empty collections, or returning collections with objects that seem proper but doesn't work with posting selectOneMenus. A redeploy makes it works again, which also makes it hard to experiment with since turning on debug mode will make it work again by redeploying.
What time-based event could possibly cause this? It's not a session time out, I've tested setting it to a one minute and letting the session die without causing this problem, it mostly occurs in the morning after the dev-server has been running all night undisturbed. All EJBs are Stateless and I can't imagine them being the problem. Can it be a serialization problem perhaps?
I apologize for being so diffuse, maybe I have somehow missunderstood how application scoped beans work. Any help would be appreciated.
Just found out what my problem was: an incorrectly implemented equals method. The equals method would compare ids of objects (Long), and used == instead of Long.equals. This caused the equality to always be negative when objects lived longer than JPA cache(because their physical address would be the same until the cache got invalidated and a new object was created).
精彩评论