1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.Arrays ; 14 15 import junit.framework.TestCase; 16 17 import com.sleepycat.je.util.StringDbt; 18 import com.sleepycat.je.util.TestUtils; 19 20 24 public class DirtyReadTest extends TestCase { 25 private File envHome; 26 private Environment env; 27 28 public DirtyReadTest() { 29 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 30 } 31 32 public void setUp() 33 throws IOException { 34 35 TestUtils.removeLogFiles("Setup", envHome, false); 36 } 37 38 public void tearDown() 39 throws Exception { 40 41 TestUtils.removeLogFiles("TearDown", envHome, false); 42 } 43 44 public void testReadUncommitted() 45 throws Throwable { 46 47 Database db = null; 48 Transaction txnA = null; 49 Cursor cursor = null; 50 try { 51 52 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 53 envConfig.setTransactional(true); 54 envConfig.setAllowCreate(true); 55 env = new Environment(envHome, envConfig); 56 57 58 DatabaseConfig dbConfig = new DatabaseConfig(); 59 dbConfig.setTransactional(true); 60 dbConfig.setAllowCreate(true); 61 db = env.openDatabase(null, "foo", dbConfig); 62 63 StringDbt key = new StringDbt("key1"); 64 StringDbt data = new StringDbt("data1"); 65 txnA = env.beginTransaction(null, TransactionConfig.DEFAULT); 66 OperationStatus status = db.put(txnA, key, data); 67 assertEquals(OperationStatus.SUCCESS, status); 68 69 73 DatabaseEntry foundKey = new DatabaseEntry(); 74 DatabaseEntry foundData = new DatabaseEntry(); 75 76 79 try { 80 db.get(null, key, foundData, LockMode.DEFAULT); 81 fail("Should deadlock"); 82 } catch (DeadlockException e) { 83 } 84 85 88 status = db.get(null, key, foundData, LockMode.READ_UNCOMMITTED); 89 assertEquals(OperationStatus.SUCCESS, status); 90 assertTrue(Arrays.equals(data.getData(), foundData.getData())); 91 92 status = db.getSearchBoth 93 (null, key, data, LockMode.READ_UNCOMMITTED); 94 assertEquals(OperationStatus.SUCCESS, status); 95 96 cursor = db.openCursor(null, CursorConfig.DEFAULT); 97 status = cursor.getFirst(foundKey, foundData, 98 LockMode.READ_UNCOMMITTED); 99 assertEquals(OperationStatus.SUCCESS, status); 100 assertTrue(Arrays.equals(key.getData(), foundKey.getData())); 101 assertTrue(Arrays.equals(data.getData(), foundData.getData())); 102 cursor.close(); 103 104 107 TransactionConfig txnConfig = new TransactionConfig(); 108 txnConfig.setReadUncommitted(true); 109 Transaction readUncommittedTxn = 110 env.beginTransaction(null, txnConfig); 111 112 status = db.get 113 (readUncommittedTxn, key, foundData, LockMode.DEFAULT); 114 assertEquals(OperationStatus.SUCCESS, status); 115 assertTrue(Arrays.equals(data.getData(), foundData.getData())); 116 117 status = db.getSearchBoth 118 (readUncommittedTxn, key, data,LockMode.DEFAULT); 119 assertEquals(OperationStatus.SUCCESS, status); 120 121 cursor = db.openCursor(readUncommittedTxn, CursorConfig.DEFAULT); 122 status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT); 123 assertEquals(OperationStatus.SUCCESS, status); 124 assertTrue(Arrays.equals(key.getData(), foundKey.getData())); 125 assertTrue(Arrays.equals(data.getData(), foundData.getData())); 126 cursor.close(); 127 readUncommittedTxn.abort(); 128 129 132 CursorConfig cursorConfig = new CursorConfig(); 133 cursorConfig.setReadUncommitted(true); 134 cursor = db.openCursor(null, cursorConfig); 135 status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT); 136 assertEquals(OperationStatus.SUCCESS, status); 137 assertTrue(Arrays.equals(key.getData(), foundKey.getData())); 138 assertTrue(Arrays.equals(data.getData(), foundData.getData())); 139 140 144 } catch (Throwable t) { 149 t.printStackTrace(); 150 throw t; 151 } finally { 152 if (cursor != null) { 153 cursor.close(); 154 } 155 156 if (txnA != null) { 157 txnA.abort(); 158 } 159 160 if (db != null) { 161 db.close(); 162 } 163 env.close(); 164 } 165 } 166 } 167 | Popular Tags |