KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > txn > TxnFSyncTest


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

8
9 package com.sleepycat.je.txn;
10
11 import junit.framework.Test;
12
13 import com.sleepycat.je.Database;
14 import com.sleepycat.je.DatabaseConfig;
15 import com.sleepycat.je.DatabaseEntry;
16 import com.sleepycat.je.DbInternal;
17 import com.sleepycat.je.Environment;
18 import com.sleepycat.je.EnvironmentConfig;
19 import com.sleepycat.je.LockMode;
20 import com.sleepycat.je.OperationStatus;
21 import com.sleepycat.je.Transaction;
22 import com.sleepycat.je.config.EnvironmentParams;
23 import com.sleepycat.je.dbi.DbEnvPool;
24 import com.sleepycat.je.dbi.EnvironmentImpl;
25 import com.sleepycat.je.test.TxnTestCase;
26 import com.sleepycat.je.util.TestUtils;
27
28 /*
29  * Make sure that transactions sync to disk. Mimic a crash by failing to
30  * close the environment and explicitly flush the log manager. If we haven't
31  * properly written and synced data to disk, we'll have unflushed data and
32  * we won't find the expected data in the log.
33  *
34  * Note that this test is run with the TxnTestCase framework and will
35  * be exercised with app-created and autocommit txns.
36  */

37
38 public class TxnFSyncTest extends TxnTestCase {
39
40     private static final int NUM_RECS = 5;
41
42     private static EnvironmentConfig envConfig = TestUtils.initEnvConfig();
43     static {
44         envConfig.setAllowCreate(true);
45         setupEnvConfig(envConfig);
46     }
47
48     private static void setupEnvConfig(EnvironmentConfig envConfig) {
49         envConfig.setTransactional(true);
50         envConfig.setConfigParam(
51             EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
52     }
53  
54     public static Test suite() {
55         /* Run these tests with user and autocommit txns. */
56         return txnTestSuite(TxnFSyncTest.class,
57                             envConfig,
58                             new String JavaDoc [] {TxnTestCase.TXN_USER,
59                                            TxnTestCase.TXN_AUTO});
60     }
61     
62     public void testFSyncButNoClose()
63         throws Exception JavaDoc {
64
65         try {
66             /* Create a database. */
67             DatabaseConfig dbConfig = new DatabaseConfig();
68             dbConfig.setTransactional(isTransactional);
69             dbConfig.setAllowCreate(true);
70             Transaction txn = txnBegin();
71             Database db = env.openDatabase(txn, "foo", dbConfig);
72
73             /* Insert data. */
74             DatabaseEntry key = new DatabaseEntry();
75             DatabaseEntry data = new DatabaseEntry();
76             for (int i = 0; i < NUM_RECS; i++) {
77                 Integer JavaDoc val = new Integer JavaDoc(i);
78                 key.setData(val.toString().getBytes());
79                 data.setData(val.toString().getBytes());
80
81                 assertEquals(OperationStatus.SUCCESS,
82                              db.putNoOverwrite(txn, key, data));
83             }
84             txnCommit(txn);
85             
86             /*
87              * Now throw away this environment WITHOUT flushing the log
88              * manager. We do need to release the environment file lock
89              * and all file handles so we can recover in this test and
90              * run repeated test cases within this one test program.
91              */

92             EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
93             envImpl.getFileManager().clear(); // release file handles
94
envImpl.getFileManager().close(); // release file lock
95
env = null;
96             DbEnvPool.getInstance().clear();
97
98             /*
99              * Open the environment and database again. The database should
100              * exist.
101              */

102             EnvironmentConfig envConfig2 = TestUtils.initEnvConfig();
103             setupEnvConfig(envConfig2);
104             env = new Environment(envHome, envConfig2);
105             dbConfig.setAllowCreate(false);
106             db = env.openDatabase(null, "foo", dbConfig);
107
108             /* Read all the data. */
109             for (int i = 0; i < NUM_RECS; i++) {
110                 Integer JavaDoc val = new Integer JavaDoc(i);
111                 key.setData(val.toString().getBytes());
112
113                 assertEquals(OperationStatus.SUCCESS,
114                              db.get(null, key, data, LockMode.DEFAULT));
115                 /* add test of data. */
116             }
117             db.close();
118         } catch (Exception JavaDoc e) {
119             e.printStackTrace();
120             throw e;
121         }
122     }
123 }
124
125
Popular Tags