title: UserTransaction failed when call utx.begin() throws "java.lang.IllegalStateException: Operation not allowed"
body: <p>i want to use ejb and jpa controller, in netbeans the controller is generated automatically... I try to call the jpa controller from class (UniversidadServiceEJB) that is a session bean stateless, I debugged the project and the UserTransaction and EntityManagerFactory is created successfully but when call the method utx.begin in the jpa controller (UniversityJpaController) throws this exception:</p>
  
  <p>java.lang.IllegalStateException: Operation not allowed.</p>
  
  <p>at com.sun.enterprise.transaction.UserTransactionImpl.checkUserTransactionMethodAccess(UserTransactionImpl.java:146)
      at com.sun.enterprise.transaction.UserTransactionImpl.begin(UserTransactionImpl.java:162)
      at controller.UniversidadJpaController.create(UniversidadJpaController.java:47)
      at services.UniversidadServiceEJB.create(UniversidadServiceEJB.java:40)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
      at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
      at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
      at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
      at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:49)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  ...
  ...
  ...</p>
  
  <p>The Session Bean class is:</p>
  
  <pre><code>@Stateless(name="UniversidadJpa")
  @Remote(IGestionUniversidad.class)
  public class UniversidadServiceEJB {
  
      @Resource
      private UserTransaction utx;
      @PersistenceUnit(unitName="ApplicationEJBPU")
      EntityManagerFactory emf;
  
  
      public void create(Universidad universidad)  throws Exception {        
          try {
              UniversidadJpaController universidadController = new UniversidadJpaController(utx,emf);
              universidadController.create(universidad);
          } catch (RollbackFailureException ex) {
              Logger.getLogger(UniversidadServiceEJB.class.getName()).log(Level.SEVERE, null, ex);
          } catch (Exception ex) {
              Logger.getLogger(UniversidadServiceEJB.class.getName()).log(Level.SEVERE, null, ex);
          } 
  
      } 
  
  }
  </code></pre>
  
  <p>And the jpacontroller class is:</p>
  
  <pre><code>public class UniversidadJpaController implements Serializable {
  
      public UniversidadJpaController(UserTransaction utx, EntityManagerFactory emf) {
          this.utx = utx;
          this.emf = emf;
      }
      private UserTransaction utx = null;
      private EntityManagerFactory emf = null;
  
      public EntityManager getEntityManager() {
          return emf.createEntityManager();
      }
  
      public void create(Universidad universidad) throws RollbackFailureException, Exception {
          if (universidad.getEstudiantes() == null) {
              universidad.setEstudiantes(new ArrayList&lt;Estudiante&gt;());
          }
          EntityManager em = null;
          try {
              utx.begin();
              em = getEntityManager();
              List&lt;Estudiante&gt; attachedEstudiantes = new ArrayList&lt;Estudiante&gt;();
              for (Estudiante estudiantesEstudianteToAttach : universidad.getEstudiantes()) {
                  estudiantesEstudianteToAttach = em.getReference(estudiantesEstudianteToAttach.getClass(), estudiantesEstudianteToAttach.getId());
                  attachedEstudiantes.add(estudiantesEstudianteToAttach);
              }
              universidad.setEstudiantes(attachedEstudiantes);
              em.persist(universidad);
              for (Estudiante estudiantesEstudiante : universidad.getEstudiantes()) {
                  Universidad oldUniversidadOfEstudiantesEstudiante = estudiantesEstudiante.getUniversidad();
                  estudiantesEstudiante.setUniversidad(universidad);
                  estudiantesEstudiante = em.merge(estudiantesEstudiante);
                  if (oldUniversidadOfEstudiantesEstudiante != null) {
                      oldUniversidadOfEstudiantesEstudiante.getEstudiantes().remove(estudiantesEstudiante);
                      oldUniversidadOfEstudiantesEstudiante = em.merge(oldUniversidadOfEstudiantesEstudiante);
                  }
              }
              utx.commit();
          } catch (Exception ex) {
  //            try {
  //                utx.rollback();
  //            } catch (Exception re) {
  //                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
  //            }
              throw ex;
          } finally {
              if (em != null) {
                  em.close();
              }
          }
      }
  }
  </code></pre>
  
  <p>And the Persistence Unit is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
    &lt;persistence-unit name="ApplicationEJBPU" transaction-type="JTA"&gt;
      &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;sqlServer&lt;/jta-data-source&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="eclipselink.ddl-generation" value="create-tables"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Please what is the problem?..
  Thank you very much...</p>