Search code examples
javasessionxpagesmanaged-beanxpages-ssjs

Xpages: managedBean is constantly "losing" global Domino Session object


I'm in the process of building a managedBean for an Xpages application. Currently the bean is registered in faces-config at view scope because I need it to re-initialize on every page load (see below). The bean's constructor initializes several class variables whose values are referenced all over the entire class's code. One of those variables is a Domino session object, another example is the current document datasource:

private Session session;
private DominoDocument ds;

Both are initialized within the constructor as

session=DominoUtils.getCurrentSession();
ds=(DominoDocument) resolveVariable(dsName);

(resolveVariable is an internal helper method; don't think I need to explain that here)

Apart from the constructor and the various helper methods there are also some other public methods within the same class that are called on button clicks. Most of those additional methods make use of the same document datasource, as well as the current session object.

What's happening is that my additional methods can access and use the global datasource object (ds) but if they try to access the global session object an error is thrown; further down the stack trace I find what seems to cause the error:

NotesException: Object has been removed or recycled

There's no recycling at all in my code right now, the session object must get lost somewhere on the way.

As a workaround I started passing the session object from SSJS into each method as in

public void testMethod(Session s){ System.out.println("my name is " + s.getEffectiveUserName()); }

Which is working fine. But why is it that the bean object keeps forgetting the global session while it can remember all the other objects and variables?

BTW: I tried to bind my managedBean to a session scope but that doesn't help at all. And yes, I even restarted the entire server after making that change...


Solution

  • All Domino objects coming from the runtime are recycled at the end of every request. If you want to access any of them, you should re-fetch them from the environment when they're needed and not store references within your mean (you could use transient refs, but you wouldn't gain much). So the quick fix is to replace each use of the session in your class with that DominoUtils.getCurrentSession() call.