2012-12-04 38 views
0

我們開發了無狀態Web服務實現。我們使用JPA作爲ORM層來完成數據庫操作。在服務方法中,我們使用實體管理器來堅持實體。我們無法處理這些異常「由於:java.sql.SQLIntegrityConstraintViolationException:ORA-00001:唯一約束(TIGOSUSCRIPTIONES.SYS_C0020549)違反了Service方法中的異常。 它直接在客戶端結果中拋出以下異常。JPA中未處理的異常

 Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Transaction rolled back 
     at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178) 
     at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111) 
     at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108) 
     at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78) 
     at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107) 
     at $Proxy30.registerSaleOutcome(Unknown Source) 

     How can I handle this exception? Following is the code we used in service method business logic. 


@TransactionAttribute(TransactionAttributeType.REQUIRED) 
public void recycleOperation(Recycle recycle) throws RecycleFault_Exception{ 
    try { 
    em.persist(recycle); 
    } catch(Exception e){ 
    // not coming to this block 
    log.error("Exception in Data Insertion:"+e.getMessage()); 
    RecycleFault fault = new RecycleFault(); 
    fault.setErrorCode("101"); 
    fault.setMessage("Record is already Existed"); 
    RecycleFault_Exception faultExp = 
     new RecycleFault_Exception("RecycleFault Exception", fault); 
    throw faultExp; 
    } 
} 

你能幫我解決這個問題嗎?

回答

0
@TransactionAttribute(TransactionAttributeType.REQUIRED) 

這會導致該方法被包裝在一個JTA事務中,當事務失敗時,異常將被拋出給調用者。您可能需要在調用者或異常處理程序中處理此操作,或者將方法切換爲使用Bean管理的事務。

您也可以在JPA中調用flush(),這將觸發約束異常,並允許您處理它。但JTA交易仍然會失敗。