1 8 9 package com.sleepycat.collections.test; 10 11 import java.io.File ; 12 13 import junit.framework.Test; 14 import junit.framework.TestSuite; 15 16 import com.sleepycat.collections.TransactionRunner; 17 import com.sleepycat.collections.TransactionWorker; 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.DeadlockException; 20 import com.sleepycat.je.Environment; 21 import com.sleepycat.je.EnvironmentConfig; 22 import com.sleepycat.je.XAEnvironment; 23 import com.sleepycat.je.log.LogUtils.XidImpl; 24 import com.sleepycat.util.ExceptionUnwrapper; 25 26 32 public class XACollectionTest extends CollectionTest { 33 34 public static Test suite() 35 throws Exception { 36 37 TestSuite suite = new TestSuite(); 38 39 EnvironmentConfig config = new EnvironmentConfig(); 40 config.setTransactional(true); 41 TestEnv xaTestEnv = new XATestEnv(config); 42 43 for (int j = 0; j < TestStore.ALL.length; j += 1) { 44 for (int k = 0; k < 2; k += 1) { 45 boolean entityBinding = (k != 0); 46 47 suite.addTest(new XACollectionTest 48 (xaTestEnv, TestStore.ALL[j], entityBinding)); 49 } 50 } 51 52 return suite; 53 } 54 55 public XACollectionTest(TestEnv testEnv, 56 TestStore testStore, 57 boolean isEntityBinding) { 58 59 super(testEnv, testStore, isEntityBinding, false ); 60 } 61 62 protected TransactionRunner newTransactionRunner(Environment env) 63 throws DatabaseException { 64 65 return new XARunner((XAEnvironment) env); 66 } 67 68 private static class XATestEnv extends TestEnv { 69 70 private XATestEnv(EnvironmentConfig config) { 71 super("XA", config); 72 } 73 74 protected Environment newEnvironment(File dir, 75 EnvironmentConfig config) 76 throws DatabaseException { 77 78 return new XAEnvironment(dir, config); 79 } 80 } 81 82 private static class XARunner extends TransactionRunner { 83 84 private XAEnvironment xaEnv; 85 private static int sequence; 86 87 private XARunner(XAEnvironment env) { 88 super(env); 89 xaEnv = env; 90 } 91 92 public void run(TransactionWorker worker) 93 throws Exception { 94 95 if (xaEnv.getThreadTransaction() == null) { 96 for (int i = 0;; i += 1) { 97 sequence += 1; 98 XidImpl xid = new XidImpl 99 (1, String.valueOf(sequence).getBytes(), null); 100 try { 101 xaEnv.start(xid, 0); 102 worker.doWork(); 103 xaEnv.prepare(xid); 104 xaEnv.end(xid, 0); 105 xaEnv.commit(xid, false); 106 return; 107 } catch (Exception e) { 108 e = ExceptionUnwrapper.unwrap(e); 109 try { 110 xaEnv.end(xid, 0); 111 xaEnv.rollback(xid); 112 } catch (Exception e2) { 113 e2.printStackTrace(); 114 throw e; 115 } 116 if (i >= getMaxRetries() || 117 !(e instanceof DeadlockException)) { 118 throw e; 119 } 120 } 121 } 122 } else { 123 try { 124 worker.doWork(); 125 } catch (Exception e) { 126 throw ExceptionUnwrapper.unwrap(e); 127 } 128 } 129 } 130 } 131 } 132 | Popular Tags |