public void testOpenAndClose() throws DatabaseException { Database priDb = openDatabase(false, "testDB", false); /* Open two secondaries as regular databases and as secondaries. */ Database secDbDetached = openDatabase(true, "testSecDB", false); SecondaryDatabase secDb = openSecondary(priDb, true, "testSecDB", false, false); Database secDb2Detached = openDatabase(true, "testSecDB2", false); SecondaryDatabase secDb2 = openSecondary(priDb, true, "testSecDB2", false, false); assertEquals( priDb.getSecondaryDatabases(), Arrays.asList(new SecondaryDatabase[] {secDb, secDb2})); Transaction txn = txnBegin(); /* Check that primary writes to both secondaries. */ checkSecondaryUpdate(txn, priDb, 1, secDbDetached, true, secDb2Detached, true); /* New txn before closing database. */ txnCommit(txn); txn = txnBegin(); /* Close 2nd secondary. */ secDb2.close(); assertEquals(priDb.getSecondaryDatabases(), Arrays.asList(new SecondaryDatabase[] {secDb})); /* Check that primary writes to 1st secondary only. */ checkSecondaryUpdate(txn, priDb, 2, secDbDetached, true, secDb2Detached, false); /* New txn before closing database. */ txnCommit(txn); txn = txnBegin(); /* Close 1st secondary. */ secDb.close(); assertEquals(0, priDb.getSecondaryDatabases().size()); /* Check that primary writes to no secondaries. */ checkSecondaryUpdate(txn, priDb, 3, secDbDetached, false, secDb2Detached, false); /* Open the two secondaries again. */ secDb = openSecondary(priDb, true, "testSecDB", false, false); secDb2 = openSecondary(priDb, true, "testSecDB2", false, false); assertEquals( priDb.getSecondaryDatabases(), Arrays.asList(new SecondaryDatabase[] {secDb, secDb2})); /* Check that primary writes to both secondaries. */ checkSecondaryUpdate(txn, priDb, 4, secDbDetached, true, secDb2Detached, true); /* Close the primary first to disassociate secondaries. */ txnCommit(txn); priDb.close(); assertNull(secDb.getPrimaryDatabase()); assertNull(secDb2.getPrimaryDatabase()); secDb2.close(); secDb.close(); secDb2Detached.close(); secDbDetached.close(); }
private SecondaryDatabase openSecondary( Database priDb, boolean allowDuplicates, String dbName, boolean allowPopulate, boolean readOnly) throws DatabaseException { List secListBefore = priDb.getSecondaryDatabases(); SecondaryConfig dbConfig = new SecondaryConfig(); dbConfig.setTransactional(isTransactional); dbConfig.setAllowCreate(true); dbConfig.setSortedDuplicates(allowDuplicates); dbConfig.setReadOnly(readOnly); dbConfig.setAllowPopulate(allowPopulate); if (!readOnly) { if (useMultiKey) { dbConfig.setMultiKeyCreator(new SimpleMultiKeyCreator(new MyKeyCreator())); } else { dbConfig.setKeyCreator(new MyKeyCreator()); } } Transaction txn = txnBegin(); SecondaryDatabase secDb; try { secDb = env.openSecondaryDatabase(txn, dbName, priDb, dbConfig); } finally { txnCommit(txn); } assertNotNull(secDb); /* Check configuration. */ assertSame(priDb, secDb.getPrimaryDatabase()); SecondaryConfig config2 = secDb.getSecondaryConfig(); assertEquals(allowPopulate, config2.getAllowPopulate()); assertEquals(dbConfig.getKeyCreator(), config2.getKeyCreator()); /* Make sure the new secondary is added to the primary's list. */ List secListAfter = priDb.getSecondaryDatabases(); assertTrue(secListAfter.remove(secDb)); assertEquals(secListBefore, secListAfter); return secDb; }