4

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?
nimo23
  • 5,170
  • 10
  • 46
  • 75

1 Answers1

2

Yes, Scenario 2 scales better than 1, we're talking efficiency here.

It's better to use the lower tier, meaning persistence tier, to cache entities, simply because it's its job to do so.

Using either depends on what server you are using, are you using a full enterprise server, if so, then better exploit the transactions offered by it, if you're simply using a web container such as tomcat, then better use managed bean.

Herupkhart
  • 499
  • 4
  • 23