KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ReadCommittedTest.java,v 1.5 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
14 import junit.framework.TestCase;
15
16 import com.sleepycat.bind.tuple.IntegerBinding;
17 import com.sleepycat.je.util.TestUtils;
18
19 /**
20  * Tests the read-committed (degree 2) isolation level.
21  */

22 public class ReadCommittedTest extends TestCase {
23
24     private File JavaDoc envHome;
25     private Environment env;
26     private Database db;
27
28     public ReadCommittedTest() {
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         if (env != null) {
42             try {
43                 env.close();
44             } catch (Exception JavaDoc e) {
45                 System.out.println("tearDown: " + e);
46             }
47         }
48
49         env = null;
50         db = null;
51
52         TestUtils.removeLogFiles("TearDown", envHome, false);
53     }
54
55     private void open()
56         throws DatabaseException {
57
58         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
59         /* Control over isolation level is required by this test. */
60         TestUtils.clearIsolationLevel(envConfig);
61
62         envConfig.setTransactional(true);
63         envConfig.setAllowCreate(true);
64         env = new Environment(envHome, envConfig);
65
66         DatabaseConfig dbConfig = new DatabaseConfig();
67         dbConfig.setTransactional(true);
68         dbConfig.setAllowCreate(true);
69         dbConfig.setExclusiveCreate(true);
70         db = env.openDatabase(null, "foo", dbConfig);
71
72         DatabaseEntry key = new DatabaseEntry();
73         DatabaseEntry data = new DatabaseEntry();
74
75         for (int i = 100; i <= 200; i += 100) {
76             for (int j = 1; j <= 5; j += 1) {
77                 IntegerBinding.intToEntry(i + j, key);
78                 IntegerBinding.intToEntry(0, data);
79                 db.put(null, key, data);
80             }
81         }
82     }
83
84     private void close()
85         throws DatabaseException {
86
87         db.close();
88         db = null;
89         env.close();
90         env = null;
91     }
92
93     public void testIllegalConfig()
94         throws DatabaseException {
95
96         open();
97
98         CursorConfig cursConfig;
99         TransactionConfig txnConfig;
100
101         /* Disallow transaction ReadCommitted and Serializable. */
102         txnConfig = new TransactionConfig();
103         txnConfig.setReadCommitted(true);
104         txnConfig.setSerializableIsolation(true);
105         try {
106             env.beginTransaction(null, txnConfig);
107             fail();
108         } catch (IllegalArgumentException JavaDoc expected) {}
109
110         /* Disallow transaction ReadCommitted and ReadUncommitted. */
111         txnConfig = new TransactionConfig();
112         txnConfig.setReadCommitted(true);
113         txnConfig.setReadUncommitted(true);
114         try {
115             env.beginTransaction(null, txnConfig);
116             fail();
117         } catch (IllegalArgumentException JavaDoc expected) {}
118
119         /* Disallow cursor ReadCommitted and ReadUncommitted. */
120         cursConfig = new CursorConfig();
121         cursConfig.setReadCommitted(true);
122         cursConfig.setReadUncommitted(true);
123         Transaction txn = env.beginTransaction(null, null);
124         try {
125             db.openCursor(txn, cursConfig);
126             fail();
127         } catch (IllegalArgumentException JavaDoc expected) {}
128         txn.abort();
129
130         close();
131     }
132
133     public void testWithTransactionConfig()
134         throws DatabaseException {
135
136         open();
137
138         TransactionConfig config = new TransactionConfig();
139         config.setReadCommitted(true);
140         Transaction txn = env.beginTransaction(null, config);
141         Cursor cursor = db.openCursor(txn, null);
142
143         checkReadCommitted(cursor, 100, true);
144
145         cursor.close();
146         txn.commit();
147         close();
148     }
149
150     public void testWithCursorConfig()
151         throws DatabaseException {
152
153         open();
154
155         Transaction txn = env.beginTransaction(null, null);
156         CursorConfig config = new CursorConfig();
157         config.setReadCommitted(true);
158         Cursor cursor = db.openCursor(txn, config);
159         Cursor degree3Cursor = db.openCursor(txn, null);
160
161         checkReadCommitted(cursor, 100, true);
162         checkReadCommitted(degree3Cursor, 200, false);
163
164         degree3Cursor.close();
165         cursor.close();
166         txn.commit();
167         close();
168     }
169
170     public void testWithLockMode()
171         throws DatabaseException {
172
173         open();
174
175         Transaction txn = env.beginTransaction(null, null);
176
177         checkReadCommitted(txn, LockMode.READ_COMMITTED, 100, true);
178         checkReadCommitted(txn, null, 200, false);
179
180         txn.commit();
181         close();
182     }
183
184     /**
185      * Checks that the given cursor provides the given
186      * expectReadLocksAreReleased behavior.
187      */

188     private void checkReadCommitted(Cursor cursor,
189                                     int startKey,
190                                     boolean expectReadLocksAreReleased)
191         throws DatabaseException {
192
193         LockStats baseStats = env.getLockStats(null);
194         DatabaseEntry key = new DatabaseEntry();
195         DatabaseEntry data = new DatabaseEntry();
196
197         checkNReadLocks(baseStats, 0);
198         for (int i = 1; i <= 5; i += 1) {
199             IntegerBinding.intToEntry(startKey + i, key);
200             OperationStatus status = cursor.getSearchKey(key, data, null);
201             assertEquals(OperationStatus.SUCCESS, status);
202             if (expectReadLocksAreReleased) {
203                 /* Read locks are released as the cursor moves. */
204                 checkNReadLocks(baseStats, 1);
205             } else {
206                 /* Read locks are not released. */
207                 checkNReadLocks(baseStats, i);
208             }
209         }
210
211         checkNWriteLocks(baseStats, 0);
212         for (int i = 1; i <= 5; i += 1) {
213             IntegerBinding.intToEntry(startKey + i, key);
214             IntegerBinding.intToEntry(0, data);
215             cursor.put(key, data);
216             /* Write locks are not released. */
217             checkNWriteLocks(baseStats, i);
218         }
219
220         if (expectReadLocksAreReleased) {
221             /* The last read lock was released by the put() call above. */
222             checkNReadLocks(baseStats, 0);
223         }
224     }
225
226     /**
227      * Checks that the given lock mode provides the given
228      * expectReadLocksAreReleased behavior.
229      */

230     private void checkReadCommitted(Transaction txn,
231                                     LockMode lockMode,
232                                     int startKey,
233                                     boolean expectReadLocksAreReleased)
234         throws DatabaseException {
235
236         LockStats baseStats = env.getLockStats(null);
237         DatabaseEntry key = new DatabaseEntry();
238         DatabaseEntry data = new DatabaseEntry();
239
240         checkNReadLocks(baseStats, 0);
241         for (int i = 1; i <= 5; i += 1) {
242             IntegerBinding.intToEntry(startKey + i, key);
243             OperationStatus status = db.get(txn, key, data, lockMode);
244             assertEquals(OperationStatus.SUCCESS, status);
245             if (expectReadLocksAreReleased) {
246                 /* Read locks are released when the cursor is closed. */
247                 checkNReadLocks(baseStats, 0);
248             } else {
249                 /* Read locks are not released. */
250                 checkNReadLocks(baseStats, i);
251             }
252         }
253
254         checkNWriteLocks(baseStats, 0);
255         for (int i = 1; i <= 5; i += 1) {
256             IntegerBinding.intToEntry(startKey + i, key);
257             IntegerBinding.intToEntry(0, data);
258             db.put(txn, key, data);
259             /* Write locks are not released. */
260             checkNWriteLocks(baseStats, i);
261         }
262
263         if (expectReadLocksAreReleased) {
264             /* The last read lock was released by the put() call above. */
265             checkNReadLocks(baseStats, 0);
266         }
267     }
268
269     private void checkNReadLocks(LockStats baseStats, int nReadLocksExpected)
270         throws DatabaseException {
271
272         LockStats stats = env.getLockStats(null);
273         assertEquals
274             ("Read locks -- ",
275              nReadLocksExpected,
276              stats.getNReadLocks() - baseStats.getNReadLocks());
277     }
278
279     private void checkNWriteLocks(LockStats baseStats, int nWriteLocksExpected)
280         throws DatabaseException {
281
282         LockStats stats = env.getLockStats(null);
283         assertEquals
284             ("Write locks -- ",
285              nWriteLocksExpected,
286              stats.getNWriteLocks() - baseStats.getNWriteLocks());
287     }
288 }
289
Popular Tags