1

I am reviewing my code from since I picked up on JSF. One of the most complex issues has come up once again. The decision between CDI and EJB.

I am using three layers and I wonder which type of annotation to use on each of them: - Backing beans (the Controller as defined in MVC) - The service layer - DAO's

My backing beans are using CDI, as long as I don't need anything from EJB. But I am stuck on those other two. I remember reading about using EJB beans because of the pooling functionalities, which would prevent massive loads of requests (or attacks, if you will). So in short, is there ANY reason to use EJB (Stateless, Stateful, LocalBean et cetera), considering security or anything else (excluded ViewScoped)?

Thanks in advance.

Menno
  • 12,175
  • 14
  • 56
  • 88

2 Answers2

4

You're making a conceptual mistake here. CDI is absolutely not an alternative to EJB. CDI in its default trim doesn't offer transaction management at all. CDI is a bean management and dependency injection API, not a service layer API. In order to manage transactions in CDI, you've to add another API along with a bunch of another annotations.

CDI can replace for example JSF managed bean annotations, but definitely not EJB annotations.

So there's no means of a valid "CDI vs EJB" question. Both have their own completely distinct purposes. Note that you can perfectly inject EJB based service classes in CDI based managed bean classes.

As to security, it's not clear why you mentioned that part in the title and the tags, but the decision between CDI and EJB has nothing to do with security (simply because there's no means of a valid "CDI vs EJB" question in first place).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So basically, only the beans used in views should (or may) be CDI? I find this a very difficult subject to fully understand, I have been reading into both multiple times. – Menno Oct 22 '12 at 18:02
  • Basically, yes. Use managed beans (either JSF or CDI) as controllers and use EJBs as service layer. An alternative to EJB is Spring (which also supports transaction management). Note that you can also annotate an EJB with `@Named` and then reference it directly in views, but that's extreme tight-coupling. See also http://stackoverflow.com/a/12988579. After all, I think the missing key in your story is that you don't understand the benefits of transaction management. I've explained this with a simple example in my answer to your previous question: http://stackoverflow.com/a/13012973. – BalusC Oct 22 '12 at 18:05
  • For the case you wonders about `@Stateless` vs `@Stateful` in EJBs, head to this answer: http://stackoverflow.com/a/8889612. – BalusC Oct 22 '12 at 18:08
  • Very, very nice answers. I am beginning to understand the whole concept of EJB. Seems like my study has been a waste of time, the help you've given me on recent questions has proven to be far more valuable. – Menno Oct 22 '12 at 18:17
0

Yes when you require out-of-box support for transactions, concurrency, pooling etc.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327