My question is about the need to define a UserTransaction in a JSF Bean if multiple EJB methods are called. This is my general scenario:
//jsf bean...
@EJB ejb1;
...
public String process(businessobject) {
ejb1.op1(businessobject);
ejb1.op2(businessobject);
....
}
both ejbs methods manipulate the same complex jpa entity bean object (including flush and detachment). I recognized in the database that some of the @oneToMany relations form my entity bean where duplicated when ejb1.op1() is called before ejb1.op2(). I understand that both ejbs start a new transaction. And to me anything looks ok so far. But the JSF code only works correctly if I add a UserTransaction to my jsf method like this:
//jsf bean...
@Resource UserTransaction tx;
@EJB ejb1;
...
public String process(businessobject) {
try {
tx.begin();
ejb1.op1(businessobject);
ejb1.op2(businessobject);
finaly {
tx.commit();
}....
}
I did not expect that it is necessary to encapsulate both ejb calls into one usertransaction. Why is this necessary?