0

I'm trying to understand how Java objects behave in a Domino environment and am wondering what happens when you use singletons (a singleton is a Java object that's constructed in such a way that only one instance will ever exist within the JVM).

Say I have a singleton in database 1, use getInstance() on it. The object is created in the JVM and from there on each time I use the getInstance() method the same object is returned.

Now I copy that class over to database 2 and use getInstance() in that database too. Which object is being returned? The one that I originally created in database 1 or the new one from database 2?

Thimo Jansen
  • 485
  • 2
  • 15
  • What do you mean `database 1 or 2`? – Maxim Shoustin Oct 21 '12 at 19:16
  • Fess, in a IBM/Lotus Domino environment the design of an application (including Java code) can be included in its document based database (NSF). This in contrast to putting code in JAR's on the filesystem – Thimo Jansen Oct 21 '12 at 19:24
  • Fess, this questions concerns XPages so with database Thimo means an application running on a Lotus Domino or IBM XWork server. – Per Henrik Lausten Oct 21 '12 at 19:24
  • 1
    I would expect, since sessions can't be shared between databases as well, you will end up with 2 instances of the singleton. One for database 1 and anothre for the second database. How do you copy the class? Just copy the java file and the faces.config settings or some other way? – jjtbsomhorst Oct 22 '12 at 07:31
  • 1
    I haven't done it myself (yet), but creating a 'server scope' can be done using a Singleton pattern on a Domino server. The XSP Starter Kit (available on OpenNTF) contains sample code for a server bean. See this slideshow http://www.slideshare.net/NotesSensei/the-xsp-starter-kit on how to get started with the starter kit. – Mark Leusink Oct 22 '12 at 22:33

1 Answers1

2

I've done a test and as jjtbsomhorst suggests, there indeed will be two separate singletons.

XPage SingletonTest.xsp:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:text escape="true" id="computedField1" value="#{javascript:nl.tjit.SingletonTest.INSTANCE.getWhoami()}"></xp:text>
</xp:view>

Java class:

package nl.tjit;
    public enum SingletonTest {
    INSTANCE;

    private final String whoami = "I am your father";

    public String getWhoami() {
        return whoami;
    }
}

The XPage and Java class copied to another database with a slightly modified whoami string produce a different output. So the singletons are not shared between databases.

Thimo Jansen
  • 485
  • 2
  • 15
  • Thanks for the answer. This is indeed what I expected. Each application uses the same JVM ( as far as I know) but they use their own memory block ( or how do you call it in real java language ;)) – jjtbsomhorst Oct 22 '12 at 12:10
  • 2
    It looks like this is handled by the class loader and each XPages app has its own class loader. As explained by Phillip Riand in this SO answer: http://stackoverflow.com/a/5352674/524998 – Thimo Jansen Oct 23 '12 at 06:14
  • 2
    @ThimoJansen: exactly. furthermore, you can have more than one class loader in your application. we have tried cache logic with singletons and it failed because of many cache instances. – Frantisek Kossuth Oct 23 '12 at 13:02