SessionContext Injection using @Resource annotation

孤者浪人 提交于 2019-12-11 03:54:14

问题


I need to rollback in EJB 3 Stateless SessionBean (CMT, JBoss version 5), for which I am using

sessionContext.setRollbackOnly();

This sessionContext is injected using @Resource annotation. My questions: 1) Is it preferred way to rollback in EJB3?

2) Why Jboss complains on deployment if I use public setter injection

// throws exception on deployment.
    private SessionContext sessionContext;
    @Resource
    public void setSessionContext(SessionContext sessionContext) {
     this.sessionContext = sessionContext;
    }

but following works fine:

@Resource
private SessionContext sessionContext;

Here is the exception in first case:

javax.ejb.SessionContext is an interface, and JAXB can't handle interfaces.
        this problem is related to the following location:
                at javax.ejb.SessionContext
                at public javax.ejb.SessionContext invoice.sap.service.jaxws.SetSctx.arg0
                at invoice.sap.service.jaxws.SetSctx
javax.ejb.SessionContext does not have a no-arg default constructor.
        this problem is related to the following location:
                at javax.ejb.SessionContext

回答1:


I assume the EJB is an @WebService, which is why you're getting JAXB errors. Try:

@Resource
@WebMethod(exclude=true)
public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
}

Alternatively, change the method visibility or add the final modifier (only public non-final non-static methods are webservices methods).




回答2:


1) yes

2) Dunno, perhaps a bug, perhaps deprecated. I've glanced through the EJB 3.1 spec and there I only saw the @Resource SessionContext sessionContext form, while the EJB 3.0 spec also showed the setter injection.



来源:https://stackoverflow.com/questions/5538785/sessioncontext-injection-using-resource-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!