1 Reply Latest reply on Jul 19, 2009 3:27 PM by Massimo Amato

    Transaction drives me crazy

    Massimo Amato Newbie

      Hi,
      let me explain my problem.

      I've a little sample application with a servlet, calling a stateless session bean on jboss 5.0.1

      The method on the session bean just tryes to persist an entity bean into the DB (mySQL)

      All that i wanna do is to be able to manage the transaction by myself.
      I mean if something happens the transaction has to be committed, else, rolled back.

      I've tryed everithing but the results are:
      - if I join the transaction the record is written in any case
      - if I don't join the record is not written also il I commit the transaction

      I've read a lot around and the code is:

      @Stateless(name="WebFacadeBean")
      @TransactionManagement(TransactionManagementType.BEAN)
      public class WebFacadeBean implements WebFacadeBeanRemote, WebFacadeBeanLocal {

      private EntityManager manager;

      // BMT idiom
      @Resource public UserTransaction userTransaction;
      @PersistenceUnit public EntityManagerFactory entityManagerFactory;

      public void method() {
      try {
      manager = entityManagerFactory.createEntityManager();
      userTransaction.begin();

      manager.joinTransaction();

      Ricarica ricarica = new Ricarica();

      manager.merge(ricarica);

      boolean doTheJob = true
      if(!doTheJob)
      throw new Exception("Do rollback");

      manager.flush();

      userTransaction.commit();

      } catch (Exception e) {
      try {
      userTransaction.rollback();
      } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      ....


      PLEASE HELP !!
      Thanks a lot.