[英]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.