Question

I have a little problem with my web service. I exposed my EJB as a web service with annotations.

My other web services are working but this web service doesn't work. In the method I need to do some transactions.

Here is my EJB exposed as web service:

@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:

ut = sessionContext.getUserTransaction( );

Do I add annotations for the UserTransaction or anything else?

I am working on Eclipse and Jboss 6.2 as 7.

Était-ce utile?

La solution 2

I solved this problem. In fact I kept the @Resource and @TransactionManagement annotations. I changed my SessionContext into EJBContext and I also changed my setSessionContext like this:

EJBContext sessionContext;
...

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

And in my method, to get the userTransaction I do:

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

Autres conseils

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. The getUserTransaction() method can only be invoked from a bean with Bean-Managed Transaction.

Keep in mind that the getPfmOverview() business method already executes in a transaction created by the Container.

If you really need manage the transaction programmatically, you can change the bean transaction type with the @TransactionManagement annotation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top