2011-01-27 47 views
1

我想恢復對象狀態的異常。我的代碼是這樣::交易使用deuce stm

public class DeuceTXTest 
{ 
@Atomic 
public void myTransactionMethod(HashMap<String, String> myHashMap) 
     { 
    myHashMap.put("s2", "two"); 
    if(8>5) 
        throw new NullPointerException(); 
} 

    public static void main(String[] args){ 
    HashMap<String, String> hashMap = new HashMap<String, String>(); 
    hashMap.put("s1", "one"); 
    System.out.println("Prior TX :: "+hashMap); 
    DeuceTXTest txTest = new DeuceTXTest(); 
    try { 
    txTest.myTransactionMethod(hashMap); 
    } catch (Exception e) { 
    System.out.println(e); 
    } 
    System.out.println("Post TX :: "+hashMap); 
    } 
} 

我加-javaagent:myDir/deuceAgent.jar作爲VMArgument在Eclipse上運行時。

結果預計::

Prior TX :: {s1=one} 
       java.lang.RuntimeException 
       Post TX :: {s1=one} 

實際結果::

Prior TX :: {s1=one} 
     java.lang.RuntimeException 
     Post TX :: {s2=two, s1=one}. 

另外,請建議我更好的例子平手恢復/對事務回滾或者拋出異常恢復對象的狀態。

+0

使用4個縮進來格式化您的源代碼。 – 2011-01-27 12:16:52

回答

3

Deuce語義是在用戶Exception上提交的,與單個全局鎖相同。 這是這樣做的,因爲Java中的Exception是合法的流控制。

如果要回滾事務,可以引發TransactionException或AbrotTransactionException。

順便說一句,既然你正在使用作爲你的事務的一部分的一個類是rt.jar的一部分(並加載到bootclasspath中),你應該離線儀器rt.jar或至少這個類作爲添加它bootclassloader。

請參閱:http://www.deucestm.org/documentation/getting-started

+0

儘管設計決定考慮對異常進行提交,但從用戶的角度來看,感覺很奇怪。 STMs失敗的原子性丟失,因爲大多數人會在失敗條件下使用Exception。 – 2011-01-28 07:53:17