Search code examples
xpagesjavabeanslotus-domino

Xpages JavaBean static Notes Object cause error saving


In this JavaBean, I wrote some object as (static). But when I reach the logDoc.save() method, it returns error. Removing all (static), all run fine. The error in console was:

HTTP JVM: lotus.domino.local.Document method: save, row: -1: 
NotesException: Notes error: You are not authorized to perform that operation

or

HTTP JVM:lotus.domino.local.NotesBase method: CheckObject, row: -1: 
NotesException: Object has been removed or recycled

depending on ACL settings for Anonymous.

Anyone can explain me whot's the problem with static objects here?

    public class AnyRest extends CustomServiceBean implements Serializable  {

    private static final long serialVersionUID = 3548995909847938225L;

    private (static) Database logDB=null;
    private (static) Document logDoc=null;
    private (static) RichTextItem logBody=null;

    private  (static) void log(String text) {
        if (logDB==null) logDB= ExtLibUtil.getCurrentDatabase();
        try {
            if (logDoc==null) {
                logDoc=logDB.createDocument();
                logDoc.replaceItemValue("form","log");
                logBody=logDoc.createRichTextItem("Body");
            }
            logBody.appendText(text);
            logBody.addNewLine();
            logDoc.save();
        } catch(NotesException ne) {
            logStackTrace(ne);
        }
    }


    private (static) void destroyLog() {

        try {
            if (logDoc!=null) {
                logDoc.save();
                logDoc.recycle();
                logDoc=null;
            }
            if (logDB!=null) {
                logDB.recycle();
                logDB=null;
            }

        } catch (Exception e) {
            logStackTrace(e);
        }
    }

    public String doPost(String strObject){
        //do useful things
        log("useful post data")
    }
}

Solution

  • Domino objects are not serializable.

    You should use Domino objects within a class method only.

    Store in class fields data like:

    • Domino server's name,

    • path to database,

    • view's name,

    • document's id or a list of documents' ids,

    • document item's values,

    This way you can easily get the Domino objects again when you need them.
    But, never store Domino objects like session, database, view and document in class fields.
    (Unless you instantiate the class at every request new and with it the fields - but this is definitely not the case when you use "static".)