2

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.

Allan
  • 17,141
  • 4
  • 52
  • 69
Sam Stern
  • 24,624
  • 13
  • 93
  • 124
  • So if you're not using transactions, what are you using? What statement or process do you have which approximates to `txn.commit()`? – APC Mar 19 '12 at 15:44
  • I don't have anything of that nature (although I did try running Database.sync() at certain times). I thought transactions were optional if Berkeley DB if you disable them... if not why can I disable them? I don't really understand Transactions so that's another reason I'm not using them. Is this my issue? – Sam Stern Mar 19 '12 at 15:49
  • 1
    *I don't really understand Transactions*... If you plan on using a DB, you should probably learn what a transaction is. It is one of the fundamentals of a DB. – Guillaume Polet Mar 19 '12 at 15:59
  • How do you open and how do you close your database? – biziclop Mar 19 '12 at 16:09
  • Post excerpt of your code - please – ring bearer Mar 19 '12 at 16:21

4 Answers4

2

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

DatabaseConfig databaseConfig = new DatabaseConfig();
databaseConfig.setTransactional(true);
Andrés Oviedo
  • 1,388
  • 1
  • 13
  • 28
2

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.

melis
  • 21
  • 1
1

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

  • This was so long ago I no longer have access to the code. I appreciate the answer though, wish I could try it and confirm whether or not it works. – Sam Stern Jul 25 '13 at 17:32
1

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

Sam Stern
  • 24,624
  • 13
  • 93
  • 124