KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.sleepycat.je.util.TestUtils;
16
17 public class DbHandleLockTest extends TestCase {
18     private File JavaDoc envHome;
19     private Environment env;
20
21     public DbHandleLockTest() {
22         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
23     }
24
25     public void setUp()
26         throws Exception JavaDoc {
27
28         TestUtils.removeLogFiles("Setup", envHome, false);
29         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
30         envConfig.setTransactional(true);
31         envConfig.setAllowCreate(true);
32         env = new Environment(envHome, envConfig);
33     }
34     
35     public void tearDown()
36         throws Exception JavaDoc {
37
38         try {
39             /* Close in case we hit an exception and didn't close */
40             env.close();
41         } catch (DatabaseException e) {
42             /* Ok if already closed */
43         }
44
45         TestUtils.removeLogFiles("TearDown", envHome, false);
46     }
47
48     public void testOpenHandle()
49         throws Throwable JavaDoc {
50
51         try {
52             Transaction txnA =
53         env.beginTransaction(null, TransactionConfig.DEFAULT);
54             DatabaseConfig dbConfig = new DatabaseConfig();
55             dbConfig.setTransactional(true);
56             dbConfig.setAllowCreate(true);
57             Database db = env.openDatabase(txnA, "foo", dbConfig);
58
59             /*
60          * At this point, we expect a write lock on the NameLN (the handle
61          * lock).
62              */

63             LockStats lockStat = env.getLockStats(null);
64             assertEquals(1, lockStat.getNTotalLocks());
65             assertEquals(1, lockStat.getNWriteLocks());
66             assertEquals(0, lockStat.getNReadLocks());
67
68             txnA.commit();
69             lockStat = env.getLockStats(null);
70             assertEquals(1, lockStat.getNTotalLocks());
71             assertEquals(0, lockStat.getNWriteLocks());
72             assertEquals(1, lockStat.getNReadLocks());
73
74             /* Updating the root from another txn should be possible. */
75             insertData(10, db);
76             db.close();
77         } catch (Throwable JavaDoc t) {
78             t.printStackTrace();
79             throw t;
80         }
81     }
82
83     public void testSR12068()
84     throws Throwable JavaDoc {
85
86     try {
87             Transaction txnA =
88         env.beginTransaction(null, TransactionConfig.DEFAULT);
89
90             DatabaseConfig dbConfig = new DatabaseConfig();
91             dbConfig.setTransactional(true);
92             dbConfig.setAllowCreate(true);
93             Database db = env.openDatabase(txnA, "foo", dbConfig);
94         db.close();
95
96             dbConfig.setExclusiveCreate(true);
97         try {
98         db = env.openDatabase(txnA, "foo", dbConfig);
99         fail("should throw database exeception");
100         } catch (DatabaseException DE) {
101         /* expected Database already exists. */
102         }
103             dbConfig.setAllowCreate(false);
104             dbConfig.setExclusiveCreate(false);
105         db = env.openDatabase(txnA, "foo", dbConfig);
106         db.close();
107         txnA.commit();
108         txnA = env.beginTransaction(null, TransactionConfig.DEFAULT);
109         env.removeDatabase(txnA, "foo");
110         txnA.commit();
111     } catch (Throwable JavaDoc T) {
112         T.printStackTrace();
113         throw T;
114     }
115     }
116
117     private void insertData(int numRecs, Database db)
118         throws Throwable JavaDoc {
119
120         for (int i = 0; i < numRecs; i++) {
121             DatabaseEntry key = new DatabaseEntry(TestUtils.getTestArray(i));
122             DatabaseEntry data = new DatabaseEntry(TestUtils.getTestArray(i));
123             assertEquals(OperationStatus.SUCCESS,
124              db.put(null, key, data));
125         }
126     }
127 }
128
Popular Tags