1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 13 import junit.framework.TestCase; 14 15 import com.sleepycat.je.util.TestUtils; 16 17 public class DbHandleLockTest extends TestCase { 18 private File envHome; 19 private Environment env; 20 21 public DbHandleLockTest() { 22 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 23 } 24 25 public void setUp() 26 throws Exception { 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 { 37 38 try { 39 40 env.close(); 41 } catch (DatabaseException e) { 42 43 } 44 45 TestUtils.removeLogFiles("TearDown", envHome, false); 46 } 47 48 public void testOpenHandle() 49 throws Throwable { 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 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 75 insertData(10, db); 76 db.close(); 77 } catch (Throwable t) { 78 t.printStackTrace(); 79 throw t; 80 } 81 } 82 83 public void testSR12068() 84 throws Throwable { 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 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 T) { 112 T.printStackTrace(); 113 throw T; 114 } 115 } 116 117 private void insertData(int numRecs, Database db) 118 throws Throwable { 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 |