Search code examples
web-servicesjbossnullpointerexceptionjaxbtransactional

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.

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.


Solution

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