Before begining, I assume you have sound reasons not to use CMTs.
1 - The EJB2 method run in same transaction of EJB1?
In your example - yes. ut.begin() starts a new transaction. The rule with BMT UserTransactions is that it always suspends the incoming transaction and starts a new one with the call to begin(). For stateless, the transaction should finish at the end of the method. This is true irrespective of whether the incoming transaction is BMT or CMT.
2 - Or everytime that I do ut.begin() a new transaction is started?
Yes, see above.
3 - And, if a new transaction is started, how can I do for run in same transaction of EJB1 (like Required option of CMT)?
You should get a handle to the existing transaction, but should not call begin(). Its the begin() that starts the transaction. And since you havent called begin(), you shouldn't commit() or rollback() either from within EJB2.
This is handled by EJB1 which began the transaction. The commit flow is normal and you neednt signal back to the caller that all is well. However if you have an exception in EJB2, you should convey this back to the caller and he would rollback the transaction.
To mark the transaction for rollback, you would call setRollbackOnly() on the UT object from EJB2.
In EJB1, you would check the status of transaction before committing or rolling back. Use the javax.transaction.Status class for checking transaction status.
EJB2
EJB1
I am sure you can tweak EJB2 to mimic the Required behaviour of CMT transactions. Does this help?
cheers,
ram.
[ August 18, 2007: Message edited by: ramprasad madathil ]
[ August 18, 2007: Message edited by: ramprasad madathil ]