2015-11-18 92 views
0

我要回滾上的工作絕對沒例外我的交易。但是現在我不想回滾我所做的所有動作。 例如,我的應用程序的請求被處理,並在多個數據庫上的幾個數據庫操作完成。如果發生異常時我想回滾我的數據庫之一的一切行動,只有2第二個數據庫上的操作。我怎樣才能做到這一點?我總是最後滾動bback整個交易...JTA如何不回滾任何東西?

我試過到目前爲止:

@TransactionAttribute(TransactionAttributeType.REQUIRED) 
public <Param> GenericResponseMsg executeRequest (Param myParam) { 
    entityManager1.persist(someEntity); // rollback 
    ... 
} 

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 
public void addFailedRequestToDatabase() { 
    entityManager2.persist(otherEntity); // do not rollback 
    ... 
} 

我也試圖與

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) 

其註釋類,但這會導致以下例外:

Internal Exception: java.sql.SQLException: Connection can not be used while enlisted in another transaction 
Error Code: 0 

任何想法?我莫名其妙地卡住,不知道該怎麼辦了......

編輯: 這是你要的工作流程:

@Stateless 
@Path("my/path") 
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) 
public class MyRessource { 

    @EJB 
    private MyEJB myEjb; 

    @POST 
    @Path("method/Path") 
    @Consumes({MediaType.APPLICATION_JSON}) 
    @Produces({MediaType.APPLICATION_JSON}) 
    public GenericResponseMsg doSomeStuff(Param param) throws Exception { 
     try { 
      return myEjb.executeRequest(param); 
     } catch(Throwable throwable) { 
      myEjb.addFailedRequestToDatabase(); 
      throw throwable; 
     } 
    } 
} 
+1

與REQUIRES_NEW測試應該工作,如果你正確地調用方法。這個測試如何看待更完整的流程?所有方法如何以及在哪裏被調用? – Gimby

+0

看來,這是OpenEJB的一個問題。我直接在我的野蠻服務器上嘗試了我的代碼,並按預期工作。該錯誤只發生在openejb中 – Goldfish

回答

0

@TransactionAttribute是支持EJB按照規範。 @交易支持即將到達tomee 7(目前在@asf投票中)。

旁註:tomee和OpenEJB的獨立代碼是100%JTA支持相同。