Search code examples
javagwtmapdb

How to retrieve data from mapDB database, without override it every time?


I'm developing a GWT web application very similar to Doodle.com

I need to store in a database some informations about Events and Users. It is a project for a university exam and we have to use mapDB for the persistent datas. The problem we have is that each time the method saveEventsToDB() is called, a new database overwrites the one had been previously created.

This is the saveEventsToDB() method:

public void saveEventsToDB(Event event) {
    DB eventsDB = DBMaker.newFileDB(new File("eventsDB")).closeOnJvmShutdown().make();
    Map<Integer, Event> map = eventsDB.getTreeMap("Events");
    map.clear();
    Set<Integer> keys = map.keySet();
    int id=0;
    for(int key : keys) {
        id++;
    }
    map.put(id+1, event);

    eventsDB.commit();
    eventsDB.close();
}

I'm pretty sure it's caused by this line of code:

DB eventsDB = DBMaker.newFileDB(new File("eventsDB")).closeOnJvmShutdown().make();

But this was in the example code that our professor gave us for mapDB.

mapDB documentation says that newFileDB:

Creates or open database stored in file.

But a new database is created everytime, we saw this using some breakpoints and trying to exctract data from db, only one record was returned each time.

If anyone can help it would be very appreciate. Thanks


Solution

  • Your problem is that you delete the data you stored in DB when you do

    map.clear()
    

    This function delete or the record in the map. You shouldn't call this function.