I have a @RequestScoped CDI bean that I want to turn into an EJB to get declarative transactions. (I'm on EJB 3.1, Java EE 6)
Currently, I am passing state between subroutines, under the assumption that the instance is only used in a single request. If I add @Stateless
now that assumption would change.
For example, I want to do something like
@Stateless
@Named
@RequestScoped
public class Foo {
private String var1; // can't use instance vars in @Stateless?
private String var2;
public void transactionForRequest() {
var1 = value;
var2 = value;
....
subroutine();
}
}
I assume the above doesn't work- is that correct?
I am contemplating two alternatives:
- Use @Stateful instead of @Stateless, along with @Named and @RequestScoped.
- Keep @Stateless and use
EJBContext.getContextData
map to replace instance variables.
Which is better? And is there some other alternative I'm not thinking of? (Besides wait for Java EE 7 or switch to Spring. :-))
@Stateful
may be an over-skill. Have you considered using normal Stateless bean and@ConversationScoped
managed bean to pass states? – Mr.J4mes@Stateless
then@Inject
a CDI bean? Could this CDI bean be@RequestScoped
? – wrschneider@Stateless
and use@ConversationScoped
bean to pass variables from pages to pages. Check out this article about creating wizard. – Mr.J4mes@SessionScoped
bean with Stateless bean may still be easier to manage than Stateful bean. – Mr.J4mes