1 8 9 package com.sleepycat.je.txn; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.je.Database; 17 import com.sleepycat.je.DatabaseConfig; 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.EnvironmentConfig; 20 import com.sleepycat.je.Transaction; 21 import com.sleepycat.je.XAEnvironment; 22 import com.sleepycat.je.log.FileManager; 23 import com.sleepycat.je.log.LogUtils.XidImpl; 24 import com.sleepycat.je.util.StringDbt; 25 import com.sleepycat.je.util.TestUtils; 26 27 30 public class TwoPCTest extends TestCase { 31 private File envHome; 32 private XAEnvironment env; 33 private Database db; 34 35 public TwoPCTest() 36 throws DatabaseException { 37 38 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 39 } 40 41 public void setUp() 42 throws IOException , DatabaseException { 43 44 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 45 46 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 47 envConfig.setTransactional(true); 48 envConfig.setAllowCreate(true); 49 env = new XAEnvironment(envHome, envConfig); 50 51 DatabaseConfig dbConfig = new DatabaseConfig(); 52 dbConfig.setTransactional(true); 53 dbConfig.setAllowCreate(true); 54 db = env.openDatabase(null, "foo", dbConfig); 55 } 56 57 public void tearDown() 58 throws IOException , DatabaseException { 59 60 db.close(); 61 env.close(); 62 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 63 } 64 65 68 public void testBasic2PC() 69 throws Throwable { 70 71 try { 72 Transaction txn = env.beginTransaction(null, null); 73 XidImpl xid = new XidImpl(1, "TwoPCTest1".getBytes(), null); 74 env.setXATransaction(xid, txn); 75 76 StringDbt key = new StringDbt("key"); 77 StringDbt data = new StringDbt("data"); 78 db.put(txn, key, data); 79 80 env.prepare(xid); 81 env.commit(xid, false); 82 } catch (Exception E) { 83 System.out.println("caught " + E); 84 } 85 } 86 87 90 public void testTwicePreparedTransaction() 91 throws Throwable { 92 93 Transaction txn = env.beginTransaction(null, null); 94 XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null); 95 env.setXATransaction(xid, txn); 96 StringDbt key = new StringDbt("key"); 97 StringDbt data = new StringDbt("data"); 98 db.put(txn, key, data); 99 100 try { 101 env.prepare(xid); 102 env.prepare(xid); 103 fail("should not be able to prepare twice"); 104 } catch (Exception E) { 105 env.commit(xid, false); 106 } 107 } 108 109 112 public void testRollbackNonExistent() 113 throws Throwable { 114 115 Transaction txn = env.beginTransaction(null, null); 116 StringDbt key = new StringDbt("key"); 117 StringDbt data = new StringDbt("data"); 118 db.put(txn, key, data); 119 XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null); 120 121 try { 122 env.rollback(xid); 123 fail("should not be able to call rollback on an unknown xid"); 124 } catch (Exception E) { 125 } 126 txn.abort(); 127 } 128 129 132 public void testCommitNonExistent() 133 throws Throwable { 134 135 Transaction txn = env.beginTransaction(null, null); 136 StringDbt key = new StringDbt("key"); 137 StringDbt data = new StringDbt("data"); 138 db.put(txn, key, data); 139 XidImpl xid = new XidImpl(1, "TwoPCTest2".getBytes(), null); 140 141 try { 142 env.commit(xid, false); 143 fail("should not be able to call commit on an unknown xid"); 144 } catch (Exception E) { 145 } 146 txn.abort(); 147 } 148 } 149 | Popular Tags |