title: Use JTA transaction with EJB and JPA
body: <p>Hello everybody i'm looking for some help with the next problem:
  i have a jpa/stateless ejb's proyect that works perfectly, it just does simple queries and persist operations, but now i need to execute a set of persist operations, if any of them fails, i must perform a rollback, so i found JTA can do the job but using this piece of source code:</p>
  
  <pre><code>@Stateless
  public class ProjectBean implements IProject {
  
  @Resource
  javax.transaction.UserTransaction utx;
  @PersistenceContext(unitName = "JPADB")
  private EntityManager entityManager;
    ...
   //more code
  
  //this is part of a method
  try{
  utx.begin();
  entityManager.joinTransaction();
      for(Project p:projectResultList){
                  entityManager.persist(p);
              }
              utx.commit();
          }catch(Exception e){
              e.printStackTrace();
              if(utx != null)
                  try {
                      utx.rollback();
                  } catch (IllegalStateException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  } catch (SecurityException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  } catch (SystemException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                  //tx.rollback();
          }
  </code></pre>
  
  <p>simply doesn't work, and this is how the persistence.xml looks like:</p>
  
  <pre><code>&lt;persistence-unit name="JPADB"&gt;
  &lt;jta-data-source&gt;java:jboss/datasources/OracleBic&lt;/jta-data-source&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.show_sql" value ="true" /&gt;
          &lt;property name="hibernate.dialect"  value="org.hibernate.dialect.Oracle10gDialect" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>really hope anyone can give me a tip or advice, i'm a newbie with jpa/jta concepts and i tried lots of codes i found in the web but i always obtain different errors (Wrong tx on thread: expected TransactionImple usertransaction begin,Cannot use an EntityTransaction while using JTA). thanks in advance.</p>