Java – xpages: managedbeans often “lose” global domino session objects
I'm building a managedbean for the xpages application Currently, the bean is registered in the view - scoped faces - config because I need it to be reinitialized when each page loads (see below) The bean constructor initializes several class variables whose values are referenced throughout the class code One variable is the domino session object. Another example is the current document data source:
private Session session; private DominoDocument ds;
Both are initialized in the constructor to
session=DominoUtils.getCurrentSession(); ds=(DominoDocument) resolveVariable(dsName);
(resolvevariable is an internal helper method; don't think I need to explain it here)
In addition to constructors and various helper methods, there are other public methods in the same class that are called when a button is clicked Most of these additional methods use the same document data source and the current session object
What happens is that my other methods can access and use the global data source object (DS), but they will raise an error if they try to access the global session object; Further down the stack trace, I found that it seemed to cause an error:
NotesException: Object has been removed or recycled
My code is not recycled at all now, and the session object must be lost on the way
As a workaround, I started passing session objects from ssjs into each method
Public void testmethod (session s) {system.out.println ("my name is" s.geteffectiveusername());}
Which one works normally But why do bean objects remember all other objects and variables and forget the global session?
BTW: I tried to bind my managedbean to session scope, but it didn't help at all Yes, I even restarted the whole server after making the change
Solution
All domino objects from the runtime are recycled at the end of each request If you want to access any of them, you should retrieve them from the environment when needed, rather than storing references in your meaning (you can use transient references, but you won't get too much) Therefore, the quick fix is to use domino utils The getcurrentsession () call replaces each usage of the session in the class