Should I use EJB-@Singleton (javax.ejb.Singleton) for statistics or monitoring or would it be better to cache the statistics in common @SessionScoped-Bean? To clear my question, here are two scenarios:
Scenario I:
A User begins a Websession and makes database-queries to view statistics or datatables. These queries are fullfilled within its session. So 10.000 Users would make 10.000 equal database-queries.
Scenario II:
A User begins a Websession and retries the datas for the statistics or datatables from a preinitialized @Singleton-Bean. The @Singleton (javax.ejb.Singleton) made the query at the beginning of Server-Startup (@Startup). So 10.000 Users could read from ONE cache (@Singleton) and do not have to query the database. My @Singleton-Bean triggers a Refresh of its cached datas if someone else create/edit/delete datas.
So my questions are:
- Does Scenario II scales better than Scenario I ? I guess, yes. Am I right?
- Are there any other caveats or things to consider?
- I know, Stateless-Beans scales out much more than @stateful or @Singleton. Should I consider to use @Stateless-Bean and cache the queries with something like JPA/Hibernate Caches.
- Should I use @ApplicationScoped (javax.enterprise.context) instead of @Singleton (javax.ejb.Singleton) to make use of proxy? Would it be better?