SessionContext Rollback
Hello,
i'm modifying existing EJB code, but i'm completely new in EJB :(
Inside the method addClient - i would like to do partial rollback.
I invoke createClients to add clients to the databse. If one client for some reasons (some business rules) couldn't be added to the databse, then the method createClients have to continue job (and try to add other clients).
If i will use ctx.setRollbackOnly - wouldn't it be rollback for whole transaction?
//EXAMPLE CODE
publicclass SomeBusinessBeanimplements javax.ejb.SessionBean{
private SessionContext ctx
...
public ClientLocal createClients(Collection clients){
for (Iterator iter = exceptionsColl.iterator(); iter.hasNext();){
addClient((Client) iter.next());
}
}
public ClientLocal addClient(Client client){
try{
ebHome.create(client);
}catch (Exception e){
ctx.setRollbackOnly();
}
}
...
}
[1629 byte] By [
Roberts_Va] at [2007-11-27 5:42:31]

# 1
There is no notion of "partial" rollback in J2EE's transaction architecture. Once a transaction is
marked for rollback, none of the work performed within it will commit successfully.
If you need finer control over transactions than just the method-level offered by container-managed
transactions, you can use bean-managed transactions.That allows you to explicitly start and
commit a transaction within the business method itself. You can start and commit multiple transactions
in one business method as well, but not more than one at the same time within the same invocation.
--ken
ksaksa at 2007-7-12 15:21:16 >

# 2
Hello Robert,
Would success/failure for a client affect the addition of another client ? If not, you may run each client addition in a separate txn, and commit / rollback individually.
Hello Ken,
On this topic, would it be advisable to execute the entity bean in its own transaction with the requirednew attribute ? This way, we can rollback the transaction without affecting the master transaction. I understand that this solution is not scalable, and would have performance issues for large number of clients.
- Jithesh
# 3
Yes, defining a separate business method with REQUIRES_NEW is another way to begin a new
transaction.However, in that case the method must be invoked through an ejb reference, even when
called from the same bean.If the method is invoked directly, as in the example in the initial posting,
the EJB container is not involved and therefore does not perform the container-managed
transaction demarcation.
--ken
ksaksa at 2007-7-12 15:21:16 >

# 4
ok, what i understand now is, that by default every method (invocation of the method) runs inside own transaction (method level transaction).
So this means, that if addition of some client fails (method - addClient), then addition of other clients is not affected (method createClients). I'm correct?
# 5
The behavior depends on which transaction attribute is assigned to a business method.
If a method A has tx attribute REQUIRED, then if the caller already has a transaction, that transaction
will be used for the execution of method A.If there is no incoming transaction, a new one will be
started.
ksaksa at 2007-7-12 15:21:16 >
