public void setRollbackOnly() throws IllegalStateException {
   try {
     UserTransaction userTransaction = getUserTransaction();
     userTransaction.setRollbackOnly();
   } catch (SystemException e) {
     throw new KernelRuntimeException(e);
   }
 }
 @Override
 public void markRollbackOnly() {
   try {
     userTransaction.setRollbackOnly();
   } catch (SystemException e) {
     throw new TransactionException("Unable to mark transaction for rollback only", e);
   }
 }
 public void setRollbackOnly() {
   try {
     if (userTransaction.getStatus() == STATUS_ACTIVE) {
       userTransaction.setRollbackOnly();
     }
   } catch (final Exception e) {
     logger.log("ESSR0017", new Object[] {e.getMessage()}, e);
   }
 }
  public void openAccount(Account acct, Boolean rollback)
      throws RemoteException, RollbackException {

    try {
      DataSource ds =
          (DataSource)
              javax.rmi.PortableRemoteObject.narrow(
                  jndiContext.lookup("java:comp/env/database"), DataSource.class);
      Connection con = ds.getConnection();

      try {
        UserTransaction ut = ejbContext.getUserTransaction();
        /*[1] Begin the transaction */
        ut.begin();

        /*[2] Update the table */
        PreparedStatement stmt =
            con.prepareStatement(
                "insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
        try {
          stmt.setString(1, acct.getSsn());
          stmt.setString(2, acct.getFirstName());
          stmt.setString(3, acct.getLastName());
          stmt.setInt(4, acct.getBalance());
          stmt.executeUpdate();
        } finally {
          stmt.close();
        }

        /*[3] Commit or Rollback the transaction */
        if (rollback.booleanValue()) ut.setRollbackOnly();

        /*[4] Commit or Rollback the transaction */
        ut.commit();
      } finally {
        con.close();
      }
    } catch (RollbackException re) {
      throw re;
    } catch (Exception e) {
      e.printStackTrace();
      throw new RemoteException("[Bean] " + e.getClass().getName() + " : " + e.getMessage());
    }
  }
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    out.println("RestoFX Server STARTING");

    if (emf == null) {
      throw new RuntimeException("JPA Persistence Unit is not properly setup!");
    }

    EntityManager em = emf.createEntityManager();

    try {
      utx.begin();
      em.joinTransaction();
      if (em.createQuery("select c from Category c").getResultList().isEmpty()) {
        List<Category> categories = createCategories();
        for (Category category : categories) {
          em.persist(category);
        }
        utx.commit();
      }
    } catch (Exception ex) {
      try {
        utx.setRollbackOnly();
      } catch (Exception rollbackEx) {
        // Impossible d'annuler les changements, vous devriez logguer une erreur,
        // voir envoyer un email à l'exploitant de l'application.
      }
      throw new RuntimeException(ex.getMessage(), ex);
    } finally {
      if (em != null) {
        em.close();
      }
    }

    out.println("RestoFX Server STARTED");
  }
  @Test
  @FailureExpectedWithNewMetamodel(
      message =
          "ForeignKeyHelper.java:140 fails with org.hibernate.cfg.NotYetImplementedException: "
              + "No support yet for referenced join columns unless they correspond with columns bound for an attribute binding.")
  public void testPersistAndLoadUnderJta() throws Exception {
    Item item;
    SessionFactory sessionFactory = buildSessionFactory();
    try {
      UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");
      ut.begin();
      try {
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        item = new Item("anItem", "An item owned by someone");
        session.persist(item);
        session.getTransaction().commit();
        session.close();
      } catch (Exception e) {
        ut.setRollbackOnly();
        throw e;
      } finally {
        if (ut.getStatus() == Status.STATUS_ACTIVE) ut.commit();
        else ut.rollback();
      }

      ut = (UserTransaction) ctx.lookup("UserTransaction");
      ut.begin();
      try {
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        Item found = (Item) session.load(Item.class, item.getId());
        Statistics stats = session.getSessionFactory().getStatistics();
        log.info(stats.toString());
        assertEquals(item.getDescription(), found.getDescription());
        assertEquals(0, stats.getSecondLevelCacheMissCount());
        assertEquals(1, stats.getSecondLevelCacheHitCount());
        session.delete(found);
        session.getTransaction().commit();
        session.close();
      } catch (Exception e) {
        ut.setRollbackOnly();
        throw e;
      } finally {
        if (ut.getStatus() == Status.STATUS_ACTIVE) ut.commit();
        else ut.rollback();
      }

      ut = (UserTransaction) ctx.lookup("UserTransaction");
      ut.begin();
      try {
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        assertNull(session.get(Item.class, item.getId()));
        session.getTransaction().commit();
        session.close();
      } catch (Exception e) {
        ut.setRollbackOnly();
        throw e;
      } finally {
        if (ut.getStatus() == Status.STATUS_ACTIVE) ut.commit();
        else ut.rollback();
      }
    } finally {
      if (sessionFactory != null) sessionFactory.close();
    }
  }