简体   繁体   English

从托管bean访问ejb时出现异常

[英]Exception when access to ejb from managed bean

I'can't access to my @EJB from my managed bean (used from a jsf page) because of this exception 由于此异常,我无法从托管bean(从jsf页面使用)访问我的@EJB

javax.ejb.EJBTransactionRequiredException: Transaction is required for invocation: org.jboss.invocation.InterceptorContext@353e531e javax.ejb.EJBTransactionRequiredException:调用需要事务:​​org.jboss.invocation.InterceptorContext@353e531e

The jsf part (pages faces-config and web.xml) is in a different package as the beans part but two are in an EAR deployed. jsf部分(pages faces-config和web.xml)与bean部分位于不同的包中,但两个部署在EAR中。 This application is deployed on jboss 7 该应用程序部署在jboss 7上

EJB (business): EJB(业务):

@Stateful
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class DataAccessBusinessImpl implements DataAccessBusiness {

Managed Bean: 托管Bean:

public class ConfigurationBean implements Serializable {
    @EJB
    DataAccessBusiness dab;

Faces-config.xml: faces-config.xml中:

<managed-bean>
<managed-bean-name>configurationBean</managed-bean-name>
<managed-bean-class>ch.morphean.videoaid.lb.managedBean.ConfigurationBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>

web.xml: web.xml中:

<display-name>videoaid-site</display-name>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

Thank's for your help 谢谢你的帮助

According to the EJB specification for TransactionAttributeType.MANDATORY : 根据TransactionAttributeType.MANDATORY的EJB规范:

If a client invokes the enterprise bean's method while the client is associated with a transaction context, the container invokes the enterprise bean's method in the client's transaction context. 如果客户端在客户端与事务上下文关联时调用企业bean的方法,则容器将在客户端的事务上下文中调用企业bean的方法。 If there is no existing transaction, an exception is thrown. 如果没有现有事务,则抛出异常。

As your "client" (the managed bean) has no transaction started, the EJB container throws an exception because you have annotated your session bean with TransactionAttributeType.MANDATORY . 由于您的“客户端”(托管bean)没有启动事务,EJB容器会抛出异常,因为您已使用TransactionAttributeType.MANDATORY注释了会话Bean。

Instead of using MANDATORY as value to the @TransactionAttribute, use REQUIRED as this will force the container to start a transaction if there isn't an existing one. 不使用MANDATORY作为@TransactionAttribute的值,而是使用REQUIRED因为这将强制容器启动事务(如果没有现有事务)。 The specification about TransactionAttributeType.REQUIRED says the following: 有关TransactionAttributeType.REQUIRED的规范说明如下:

If a client invokes the enterprise bean's method while the client is associated with a transaction context, the container invokes the enterprise bean's method in the client's transaction context. 如果客户端在客户端与事务上下文关联时调用企业bean的方法,则容器将在客户端的事务上下文中调用企业bean的方法。

If the client invokes the enterprise bean's method while the client is not associated with a transaction context, the container automatically starts a new transaction before delegating a method call to the enterprise bean method. 如果客户端在客户端未与事务上下文关联时调用企业bean的方法,则容器会在将方法调用委托给enterprise bean方法之前自动启动新事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM