Search code examples
xpagesjavabeans

Xpages: How to access database from CacheBean


I have a cacheBean called PCConfig in which I want to store references to databases, so I can access them in other Java methods.

Here is the relevant part of my cacheBean:

package com.scoular.cache;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Vector;
import org.openntf.domino.utils.Factory;
import org.openntf.domino.xsp.XspOpenLogUtil;
import org.openntf.domino.Database;
import org.openntf.domino.Session;
import org.openntf.domino.View;
import org.openntf.domino.ViewEntry;
import org.openntf.domino.ViewNavigator;

public class PCConfig implements Serializable {

    private static final long serialVersionUID = 1L;

    private static Database PCDataDB;

    // @SuppressWarnings("unchecked")
    private void initConfigData() {
        try {
            loadStatus();
            loadGeoLocations();
            loadModels();
            loadDatabases();
        } catch (Exception e) {
            XspOpenLogUtil.logError(e);
        }
    }

    public PCConfig() {
        initConfigData();
    }

    //Getters   

    public static Database getPCDataDB() {
        return PCDataDB;
    }

    public static void setPCDataDB(Database dataDB) {
        PCDataDB = dataDB;
    }

    public static void loadDatabases() {
        loadPCDataDB();     
    }

    public static void loadPCDataDB() {
        Session session = Factory.getSession();
        PCConfig.PCDataDB = session.getDatabase(thisDB.getServer(),"scoApps\\PC\\PCData.nsf", false);
    }


    }
}

In a different java class I import the PCConfig class and try to use this method getPCDataDB(). I have also tried PCConfig.PCDataDB.

I always get the error null pointer exception.

What am I doing wrong?

public void loadByUnid(String unid) {
    try {
        Document doc = PCConfig.getPCDataDB().getDocumentByUNID(unid);
        if (null == doc) {
            System.out.println("Document not found");
        } else {
            loadValues(doc);
        }
    } catch (Exception e) {
        XspOpenLogUtil.logError(e);
    }
}

Solution

  • As Knut says, storing the database in your static class won't work. Normally you would need to store the server and the database path as separate variables. But since you're using the OpenNTF Domino API, you can take advantage of Database.getApiPath() , which returns a "metaReplicaID" - a combination of servername and replica ID. You can store that and you have a direct reference to where the database resides. You can then use session.getDatabase(metaReplicaID) to retrieve the database when required.