简体   繁体   English

EJB作为内部事务的Web服务

[英]EJB as Web service with transaction inside

I have a little problem with my web service. 我的网络服务有一点问题。 I exposed my EJB as a web service with annotations. 我将EJB作为带有注释的Web服务公开。

My other web services are working but this web service doesn't work. 我的其他Web服务正在运行,但是此Web服务不起作用。 In the method I need to do some transactions. 在该方法中,我需要进行一些事务。

Here is my EJB exposed as web service: 这是暴露为Web服务的EJB:

@Stateless 
@WebService( endpointInterface="blabla.PfmOverview",serviceName="PfmOverviewWS",name="PfmOverviewWS" )
@XmlJavaTypeAdapter(value=DateAdapter.class, type=Date.class)

public class PfmOverviewBean implements PfmOverview 
{

    SessionContext sessionContext;


  public void setSessionContext(SessionContext sessionContext)
  {
    this.sessionContext = sessionContext;
  }

public PfmOverviewDto getPfmOverview(  YUserProfile userProfile, BigDecimal portfolioId,@XmlJavaTypeAdapter(value=DateAdapter.class, type=Date.class) Date computeDate ) throws Exception
{

    YServerCtx serverCtx = new YServerCtx( userProfile );
    UserTransaction ut = null;

    try
    {
        ut = sessionContext.getUserTransaction( );
        ut.begin( );
        PfmOverviewDto dto = new PfmOverviewBL( serverCtx ).getPfmOverviewDataPf( portfolioId, computeDate );
        ut.rollback( );

        return dto;
    }
    catch( Throwable t )
    {
        if( ut != null )
            ut.rollback( );
        SLog.error( t );
        throw ( t instanceof Exception ) ? (Exception) t : new Exception( t );
    }
    finally
    {
        serverCtx.disconnect( );
    }       
}

When I call my web service in the client side (generated automatically with ws import), I get a NullPointerException at this line: 当我在客户端调用Web服务(通过ws import自动生成)时,在此行得到NullPointerException

ut = sessionContext.getUserTransaction( );

Do I add annotations for the UserTransaction or anything else? 是否为UserTransaction或其他内容添加注释?

I am working on Eclipse and Jboss 6.2 as 7. 我正在使用Eclipse和Jboss 6.2进行7。

By default the transaction type of the session beans is CMT , which means that the Container is the only one that can manage the transaction. 默认情况下,会话Bean的事务类型为CMT ,这意味着Container是唯一可以管理事务的容器。 The getUserTransaction() method can only be invoked from a bean with Bean-Managed Transaction. 只能从具有Bean管理事务的Bean中调用getUserTransaction()方法。

Keep in mind that the getPfmOverview() business method already executes in a transaction created by the Container. 请记住, getPfmOverview()业务方法已经在容器创建的事务中执行。

If you really need manage the transaction programmatically, you can change the bean transaction type with the @TransactionManagement annotation. 如果确实需要以编程方式管理事务,则可以使用@TransactionManagement批注更改Bean事务类型。

I solved this problem. 我解决了这个问题。 In fact I kept the @Resource and @TransactionManagement annotations. 实际上,我保留了@Resource@TransactionManagement批注。 I changed my SessionContext into EJBContext and I also changed my setSessionContext like this: 我将SessionContext更改为EJBContext ,也将setSessionContext更改为如下形式:

EJBContext sessionContext;
...

@Resource
 private void setSessionContext( EJBContext sctx )
{
    this.sessionContext = sctx;
}

And in my method, to get the userTransaction I do: 在我的方法中,要执行userTransaction ,请执行以下操作:

UserTransaction ut = null;
try
    {
    ut = sessionContext.getUserTransaction( );
        ut.begin( );

//here I'm calling DB access etc: result = blabla....

        ut.rollback( );
        return result;
    }
    catch( Throwable t )
    {
        if( ut != null )
                ut.rollback( );
        SLog.error( t );
        throw ( t instanceof Exception ) ? (Exception) t : new Exception( t );
    }

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

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