0 Replies Latest reply: Jun 21, 2012 3:23 AM by 945027 RSS

    JPA UserTransaction rollback does not work

    945027
      I try to describe my environment shortly. Technologies: EJB 3.1, JSF, JBoss 7.1.1

      There are Servise-classes (@SessionScoped @Stateful). Servise-classes call Dao classes (@Stateless)

      I want :
      use EntityManager only into @StateLess beans (Dao)
      have short transaction in most cases (like persist, merge)
      have one long transaction for some multistep-methods (methods are in Dao also)
      have actual (up to date, without first-level cache) data

      I have: Pesistense.xml
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:jboss/datasources/MydataSource</jta-data-source>
      <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      </properties>
      </persistence-unit>

      Dao:

      @Stateless
      @TransactionManagement(TransactionManagementType.BEAN)
      public class UserDaoBean implements UserDAO, Serializable {

      @PersistenceContext(name = "MyEntityManager")
      private EntityManager em;

      @Override
      @Transactional
      public void update(User user) throws Exception {
      User tmpUser = getUser(user.getUser());
      //some code, should be rollback, if it is an exception
      em.persist(tmpUser);
      }

      Transaction interceptor
      @Transactional
      @Interceptor
      public class TransactionInterceptor implements Serializable {

      @Resource
      private UserTransaction userTransaction;

      @AroundInvoke
      public Object verifyAccess(InvocationContext context) throws
      Exception {
      Object result = null;

      try {
      userTransaction.begin();
      result = context.proceed();
      userTransaction.commit();
      } catch (Exception e) {
      userTransaction.rollback();
      throw new CustomRuntimeException(e.getMessage());
      }
      return result;
      }
      }

      Problem: If it is throw an Exception into Dao method, part data will save in DB instead of total rollback.

      I think, is need Join Transaction to EM. Or disconnect persists each item to the DB right away (using cache). I've try different ways, but didn't have success.

      Thanks for in advance!