1

I doing a project with GWT and use mapDB for DB. if I create a new object i can read it, but when close the project and restart I lost all data, I see that it never creates a file to save.

this is my code:

Method for create and add new object Student(e)

@Override
    public void addStudent(String mail, String password) throws IllegalArgumentException {
        Studente u = new Studente("G", "A", mail, password, "22-08-1999", 000001);
        loadDB();
        map.put(String.valueOf(map.size() + 1), u);
        db.commit();
        db.close();
    }

Method for read data from DB

@Override
    public void getStudents() {
        loadDB();
        Studente[] studenti = new Studente[map.size()];
        int j = 0;
        for( String i: map.getKeys()){
            studenti[j] = map.get(i);
            j++;
        }
        db.close();
    }

Method loadDB

private void loadDB(){
        this.db = getDb("studenti.db");
        this.map = this.db.hashMap("studentiMap").counterEnable().keySerializer(Serializer.STRING).valueSerializer(new SerializerStudente()).createOrOpen();
    }

Method getDB

private DB getDb(String nomeDB) {
        ServletContext context = this.getServletContext();
        synchronized (context) {
            DB db = (DB)context.getAttribute("DB");
            if(db == null){
                db = DBMaker.fileDB(nomeDB).make();
                context.setAttribute("DB", db);
            }
        return db;
        }
    }

Thanks you so much

P.s. Sorry for my english

Mr Alsi
  • 37
  • 6

0 Answers0