Discussions

EJB design: Transaction problem

  1. Transaction problem (8 messages)

     I have a problem with transactions. I have 2 beans S1 and S2 both individually writing to the database. I have given the transaction attribute as "Required" and type as "Bean" .
     
      I have another session bean M which calls method on the two bean S1 and S2 .I have given the attribute as "Required" and "Bean" . (I even tried "RequiresNew" and "Bean") .But the problem is that even if there is a problem or Exception on one the other gets commited.


    ctx.getUserTransaction().begin();//ctx is sessioncontext

    b1=st1.Method1();
    b2=st2.Mothod2();

    if((b1 || b2)==false)
    ctx.getUserTransaction().rollback();
            else
    ctx.getUserTransaction().commit();

    Please Help as soon as possible

    tony

    Threaded Messages (8)

  2. Transaction problem[ Go to top ]

    Im a little confused. You say you have set the transaction type as "required" however you are using a BEAN_MANAGED transaction via the UserTransaction object. Which is it?

    Secondly, WHY are you using a BEAN_MANAGED transaction? I dont see anything in your logic to show why you would want to do this? CONTAINER_MANAGED transactions are much easier to work with, and much more flexible.

    Dave Wolf
    Internet Applications Division
    Sybase
  3. Transaction problem[ Go to top ]

    Sorry, I am not that good in tranxs , could you please tell me what is the best method to get this work. i.e, to make both roll back if one fails.

      thanking you all in advance

    tony


  4. Transaction problem[ Go to top ]

    May I suggest to use the following setup:

    M - Session Bean (Bean managed)
    S1 - Entity Bean (CMP, TX_REQUIRED)
    S2 - Entity Bean (CMP, TX_REQUIRED)

    In M,
    ctx.getUserTransaction().begin();//ctx is sessioncontext

    b1=s1.Method1();
    b2=s2.Mothod2();

    if((b1 || b2)==false)
    ctx.getUserTransaction().rollback();
            else
    ctx.getUserTransaction().commit();

    Please advice!!

    Jimmy Wong
    HACTL
  5. Transaction problem[ Go to top ]

    I would make all three container managed transactions and mark all 3 as Required. Then to rollback you can call

    _sessionContext.setRollbackOnly().

    Unless there is a VERY strong reason to use bean managed transactions I would avoid it.

    Dave Wolf
    Internet Applications Division
    Sybase
  6. Transaction problem[ Go to top ]

    Dave,

    I agree with you on making them all cmp but thought if this was the case and there was an exception thrown that the transaction was automatically rolled back. why do you need to call _sessionContext.setRollbackOnly().

    Brian
  7. Transaction problem[ Go to top ]

    I believe Dave was referring to CMT (transaction), not CMP (persistence). A related thread can be found here.

    Dave: what are the pros and cons of using BMT vs. CMT?
  8. Transaction problem[ Go to top ]

    Yes I was refering to CMT not CMP. In CMT there are two ways to rollback the tx

    1) Throwing a SYSTEM level exception will rollback the tx. However, this should not be a mechanism to cause a rollback, but rather should be a safety catch. A SYSTEM level exception is much like a GPF in EJB. It should only be sued to denote a major system failure for which you see no other solution. In this way, the container is protecting the stability of the system by rolling back any tx's when it see such an exception. It also will destroy the bean to protect system state.

    2) Voting for a rollback. This is the safeest and most flexible way to request a rollback in a CMT environment.

    My belief is you should always use CMT unless you have a significant driving reason to have BMT. One example where people use BMT is to have a long lived tx, that say spans multiple business methods of a stateful session bean. Even in this case I would suggest better architecures over the use of BMT.

    Dave Wolf
    Internet Applications Division
    Sybase
  9. Transaction problem[ Go to top ]

    Thanks for the wonderful response, But What's the difference between Application Exception and a System Exception. How are these two exceptions going to affect transactions.

              

    tony