用户事务失败,当调用utx.begin()抛出"java.lang.IllegalStateException: Operation not allowed"



我想使用ejb和jpa控制器,在netbeans中,控制器是自动生成的。。。我试图从会话bean无状态的类(UniversidadServiceEJB)中调用jpa控制器,我调试了项目,UserTransaction和EntityManagerFactory成功创建,但当调用jpa控制程序(UniversityJpaController)中的方法utx.begin时,会引发以下异常:

java.lang.IollegalStateException:不允许操作。

网址:com.sun.enterprise.transaction.UserTransactionImpl.checkUserTransactionMethodAccess(UserTransactionImpl.java:146)网址:com.sun.enterprise.transaction.UserTransactionImpl.begin(UserTransactionImpl.java:162)在控制器处。UniversidadJpaController.create(UniversidadJpaController.java:47)在服务中。UniversidadServiceEJB.create(UniversidadServicesEJB.java:40)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:601)网址:org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)网址:org.glassfish.ejb.security.application.EJBSecurityManager.ioke(EJBSecurityManager.java:1124)网址:com.sun.exb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)网址:com.sun.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)网址:com.sun.ejb.contacters.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)网址:com.sun.ejb.EjbInvocation.prough(EjbInvocation.java:571)网址:org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterreceptor.java:49)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:601)网址:com.sun.exb.containers.enterceptors.AroundInvokeInterceptor.entercept(InterceptorManager.java:861)网址:com.sun.ejb.contacters.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)网址:com.sun.ejb.EjbInvocation.prough(EjbInvocation.java:571)网址:com.sun.exb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)网址:com.sun.exb.containers.enterceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)。。。。。。…

会话Bean类是:

@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);
        } 
    } 
}

jpacontroller类是:

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<Estudiante>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            List<Estudiante> attachedEstudiantes = new ArrayList<Estudiante>();
            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();
            }
        }
    }
}

持久单元是:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <persistence-unit name="ApplicationEJBPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>sqlServer</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

请问出了什么问题?。。非常感谢。。。

通常在EJB环境中,事务由容器管理。它将Bean方法封装在事务中,并在发生异常时自动回滚。这也意味着不允许手动启动/提交/回滚事务,并引发IllegalStateException。

参考:http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction3.html

如上所述,在容器管理事务(CMT)下,如果使用API,您将获得getStatus()异常。

https://issues.jboss.org/browse/JBSEAM-456

但您可以作为一种替代用途:@资源事务同步注册表

如何判断事务是否在JavaEE6拦截器中处于活动状态?

顺便说一句,getStatus()api在glassfish上运行,但在weblogic 12.1.2上没有运行。Weblogic实际上应该在获取状态api上抛出异常。

@资源事务同步注册表

两个容器都可以正常工作。

最新更新