views:

145

answers:

2
    @Stateless    @LocalBean
    public class MySLSB {

            @Resource
            SessionContext ctx;
            @PersistenceContext(unitName = "myPU")
            EntityManager em;

            public void T1() {
                em.persist(new MyEntity(1L)); //T1 created!
/* wrong call to plain java object               
 T2();
 */     
//corrected by lookup its business object first 
ctx.getBusinessObject(MySLSB.class).T2();   
     ctx.setRollbackOnly();
            }

            @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
            public void T2() {
                em.persist(new MyEntity(2L)); //T2 created!
            }
        }

The client calls T1(), at first T2 as a new Transaction should be committed, but T1 will be rolled back.

Expected result:

T1: insert into myentity set id=1;

T2: insert into myentity set id=2;

T2: commit;

T1: rollback;

-> The row with id=2 is created in DB.

Actual result:

insert into myentity set id=1;

insert into myentity set id=2;

rollback;

-> Nothing is created in DB.

What's the problem? Thanks a lot!

+1  A: 

The problem is solved. i made a naive mistake.

The call to T2() should lookup its business object, direct call to T2() IS merely to its plain java object.

i updated the code on the question above, making everything worked just as expected.

sof
A: 

Also it might be easier to declare self reference @EJB MySLSB me; and to call me.T2(); instead of using ctx.getBusinessObject(MySLSB.class). But the sense is the same.

igis