[英]EJB as Web service with transaction inside
我的网络服务有一点问题。 我将EJB作为带有注释的Web服务公开。
我的其他Web服务正在运行,但是此Web服务不起作用。 在该方法中,我需要进行一些事务。
这是暴露为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( );
}
}
当我在客户端调用Web服务(通过ws import自动生成)时,在此行得到NullPointerException
:
ut = sessionContext.getUserTransaction( );
是否为UserTransaction
或其他内容添加注释?
我正在使用Eclipse和Jboss 6.2进行7。
默认情况下,会话Bean的事务类型为CMT ,这意味着Container是唯一可以管理事务的容器。 只能从具有Bean管理事务的Bean中调用getUserTransaction()
方法。
请记住, getPfmOverview()
业务方法已经在容器创建的事务中执行。
如果确实需要以编程方式管理事务,则可以使用@TransactionManagement
批注更改Bean事务类型。
我解决了这个问题。 实际上,我保留了@Resource
和@TransactionManagement
批注。 我将SessionContext
更改为EJBContext
,也将setSessionContext
更改为如下形式:
EJBContext sessionContext;
...
@Resource
private void setSessionContext( EJBContext sctx )
{
this.sessionContext = sctx;
}
在我的方法中,要执行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.