KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > DirtyReadTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DirtyReadTest.java,v 1.17 2006/10/30 21:14:41 bostic Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.Arrays JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import com.sleepycat.je.util.StringDbt;
18 import com.sleepycat.je.util.TestUtils;
19
20 /**
21  * Check that the Database and Cursor classes properly use read-uncommitted
22  * when specified.
23  */

24 public class DirtyReadTest extends TestCase {
25     private File JavaDoc envHome;
26     private Environment env;
27
28     public DirtyReadTest() {
29         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
30     }
31
32     public void setUp()
33         throws IOException JavaDoc {
34
35         TestUtils.removeLogFiles("Setup", envHome, false);
36     }
37     
38     public void tearDown()
39         throws Exception JavaDoc {
40
41         TestUtils.removeLogFiles("TearDown", envHome, false);
42     }
43
44     public void testReadUncommitted()
45         throws Throwable JavaDoc {
46
47         Database db = null;
48         Transaction txnA = null;
49         Cursor cursor = null;
50         try {
51             /* Make an environment, a db, insert a few records */
52             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
53             envConfig.setTransactional(true);
54             envConfig.setAllowCreate(true);
55             env = new Environment(envHome, envConfig);
56             
57             /* Now open for real, insert a record */
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             /*
70              * txnA should have a write lock on this record. Now try
71              * to read-uncommitted it.
72              */

73             DatabaseEntry foundKey = new DatabaseEntry();
74             DatabaseEntry foundData = new DatabaseEntry();
75
76             /*
77              * Make sure we get a deadlock exception without read-uncommitted.
78              */

79             try {
80                 db.get(null, key, foundData, LockMode.DEFAULT);
81                 fail("Should deadlock");
82             } catch (DeadlockException e) {
83             }
84
85             /*
86              * Specify read-uncommitted as a lock mode.
87              */

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             /*
105              * Specify read-uncommitted through a read-uncommitted txn.
106              */

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             /*
130              * Specify read-uncommitted through a read-uncommitted cursor
131              */

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             /*
141              * Open through the compatiblity method, should accept dirty
142              * read (but ignores it)
143              */

144         // Database compatDb = new Database(env);
145
// compatDb.open(null, null, "foo", DbConstants.DB_BTREE,
146
// DbConstants.DB_DIRTY_READ, DbConstants.DB_UNKNOWN);
147
// compatDb.close();
148
} catch (Throwable JavaDoc 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