Stateless and Stateful Enterprise Java Beans

Stateless Session Beans (SLSB) are not tied to one client and there is no guarantee for one client to get the same instance with each method invocation (some containers may create and destroy beans with each method invocation session, this is an implementation-specific decision, but instances are typically pooled – and I don’t mention clustered … Read more

How do CDI and EJB compare? interact?

It is currently indeed a bit confusing as there are now multiple component models in Java EE. They are CDI, EJB3 and JSF Managed Beans. CDI is the new kid on the block. CDI beans feature dependency injection, scoping and an event bus. CDI beans are the most flexible with respect to injection and scoping. … Read more

Where to use EJB 3.1 and CDI?

Yes, you can freely mix both CDI and EJB and achieve some great results. It sounds like you are using @WebService and @Schedule, which are good reasons for adding EJB to the mix. There’s a lot of confusion out there, so here is some general information on EJB and CDI as they relate to each … Read more

JSF managed-bean EJB injection

You are mixing the responsibilities of EJBs and JSF managed beans. The faces-config.xml registers only JSF artifacts, such as managed beans and not EJBs. Your registration in faces-config.xml <managed-bean> <managed-bean-name>personManager</managed-bean-name> <managed-bean-class>ejb.PersonManager</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> does basically exactly the same as @ManagedBean @SessionScoped public class PersonManager { // … } In other words, you’re registering the class … Read more

Handling service layer exception in Java EE frontend method

It’s because you threw a RuntimeException from an EJB. When such RuntimeException is not annotated with @ApplicationException, then the EJB container will wrap it in an javax.ejb.EJBException and rethrow it. This is done so because runtime exceptions are usually only used to indicate bugs in code logic, i.e. programmer’s mistakes and not enduser’s mistakes. You … Read more

EJB 3.1 @LocalBean vs no annotation

The rules are (from memory): Bean has a @LocalBean annotation -> bean has a no-interface view Bean has a @Local annotation -> bean has a local view Bean has a @Remote annotation -> bean has a remote view Bean has no view annotations, but directly implements an interface which has a @Local annotation -> bean … Read more

Should I use @EJB or @Inject

The @EJB is used to inject EJB’s only and is available for quite some time now. @Inject can inject any managed bean and is a part of the new CDI specification (since Java EE 6). In simple cases you can simply change @EJB to @Inject. In more advanced cases (e.g. when you heavily depend on … Read more