1

I am developing a web application on JAVA EE 6 with SEAM 3, using the full profile on glassfish 3, JSF on the frontend. My main question here is, each user logged in has an inbox, pretty much like facebook, which is constantly updating through ajax, Now my question is. So far I have a CDI @SessionScoped @Named bean that stores the messages, therefore I'm bloating the user session by keeping all his messages here, these get synchronized eventually. I could also call the EJB service that retrieves the inbox for every user each time the ajax request is called and hope it will use the cache to load the messages from there instead of flooding my db with queries per ajax request. My question is, should i do this:

In my cdi bean:

public List<Message> getInbox() {
  return inbox; //where inbox is a field that holds all the messages
}

or this:

public List<Message> getInbox() {
  return messagingService.findInbox(userdId); //stub to stateless EJB fetching the inbox
}

In other words, should I store the inbox in the session or just fetch it everytime? (Consider i will be using ajax polling and have something like 10 requests per minute per user)

arg20
  • 4,893
  • 1
  • 50
  • 74

1 Answers1

4

The JPA cache is applicable to application-wide audience, while the HTTP session is only applicable to the client session. In other words, the data in session scope is duplicated for every single client. It's not really a cache, it's only memory hogging. Use the session scope for real session scoped data only, such as the logged-in user, its preferences, etcetera.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Well yes, but each client has his inbox. Why duplicated? I know the httpsession is no a cache, cache's are meant to be transparent. My question is whether i should fetch it once and store it, then update it eventually or not store anything and fetch everytime – arg20 Mar 16 '12 at 14:10
  • 1
    Duplicated in both JPA cache and HTTP session. – BalusC Mar 16 '12 at 14:25
  • 1
    JPA can manage its cache self if free memory is low. Like LRU - Keeps X number of recently used objects in the cache. Take a look here: http://en.wikibooks.org/wiki/Java_Persistence/Caching – edze Mar 16 '12 at 14:29
  • @BalusC when I have JSF or CDI beans inspected in my debugger, the getters are called a bunch of times, do I really have no penalty in calling the EJB inside the getter instead of storing its result in some field? – arg20 Mar 17 '12 at 20:16
  • Getters should not do any business logic. There they are not for. http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – BalusC Mar 17 '12 at 20:56