Question

I am using a Berkeley DB to store information for a web crawler I am building. However none of my database information is being saved between sessions!

When I start the application, count() on every DB returns 0. At the end of the session, before exiting, I print the count() for each DB and it has changed appropriately however it is "reset" when I run the program again... why is this happening?

Also, I am choosing not to use Transactions as I am a single user and I will not be running the program in multi-threaded environments.

Was it helpful?

Solution 3

The answer was to use transactions and call transaction.commit() after put requests. Still not sure how to make it save with transactions disabled.

OTHER TIPS

I just had to enable transactions to make it persistent between runs:

DatabaseConfig databaseConfig = new DatabaseConfig();
databaseConfig.setTransactional(true);

I am working on Berkeley DB for last two weeks. I was also confused with the same question and I have ended up with this: If you close the environment and database before your app stops running, the data is stored into a file that you specify its location when opening the database. Close operation can be achieved when the program is terminating successfully. However, if there is a failure or the program crashes and application cannot be ended under normal conditions, I could not find where to run closing operation yet.

Perhaps this could help:

EnvironmentConfig environmentConfig = new EnvironmentConfig();
environmentConfig.setAllowCreate(true);
File file = new File("<Path_to_Database>");
Environment environment = new Environment(file, environmentConfig);

DatabaseConfig databaseConfig = new DatabaseConfig();
databaseConfig.setAllowCreate(true);
databaseConfig.setDeferredWrite(true);

Database testDatabase = environment.openDatabase(null, "testDB", databaseConfig);

DatabaseEntry keyEntry = new DatabaseEntry();
DatabaseEntry valueEntry = new DatabaseEntry();
DatabaseEntry fetchedEntry = new DatabaseEntry();

IntegerBinding.intToEntry(1, keyEntry);
StringBinding.stringToEntry("Kwakkel", valueEntry);

testDatabase.put(null, keyEntry, valueEntry);


testDatabase.get(null, keyEntry, fetchedEntry, null);
String fetched = StringBinding.entryToString(fetchedEntry);

System.out.println("Fetched value: " + fetched);

testDatabase.sync();
testDatabase.close();

I think you have to set 'DeferredWrite' to 'true'. Then you can use 'sync()' on you database and the data is persisted. Run it and then remove the line with the 'put(...)'-command. Should still work. Well, at least it worked for me... :)

Best regards

Alexander Schell

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top