我想使用ejb和jpa控制器,在netbeans中控制器是自动生成的......我尝试从类(UniversidadServiceEJB)调用jpa控制器,这是一个会话bean无状态,我调试了项目以及UserTransaction和EntityManagerFactory是成功创建但是当调用jpa控制器(UniversityJpaController)中的方法utx.begin时抛出此异常:
java.lang.IllegalStateException:不允许操作。
at com.sun.enterprise.transaction.UserTransactionImpl.checkUserTransactionMethodAccess(UserTransactionImpl.java:146) at com.sun.enterprise.transaction.UserTransactionImpl.begin(UserTransactionImpl.java:162) 在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) 在java.lang.reflect.Method.invoke(Method.java:601) 在org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 在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) 在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) 在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) 在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) ... ... ...
Session 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>
请问有什么问题? 非常感谢你......
答案 0 :(得分:1)
通常在EJB环境中,事务由容器管理。当发生异常时,它将Bean方法包装在具有自动回滚的事务中。这也意味着不允许手动启动/提交/回滚事务并抛出IllegalStateException。
参考:http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction3.html
答案 1 :(得分:0)
如上所述,在容器管理事务(CMT)下,如果使用API,则会获得getStatus()异常。
https://issues.jboss.org/browse/JBSEAM-456
但你可以作为替代用途: @Resource TransactionSynchronizationRegistry
How to tell if a transaction is active in a Java EE 6 interceptor?
顺便说一下 - getStatus()api会对glassfish造成打击,但它不会在weblogic 12.1.2上爆炸。 Weblogic实际上应该在get status api上抛出异常。
在 @Resource TransactionSynchronizationRegistry
在两个容器上都能正常工作。