cancel
Showing results for 
Search instead for 
Did you mean: 

Java singleton object in all server processes of a dialog instance

Former Member
0 Kudos

Hi all,

A SAP Web AS Java (Cluster) with one central dialog instance has two (more than one) server processes/nodes.

In process/node 1, a singleton Java object, i.e. StateHolder is being instanciated and filled with arbitrary

other data objects.

This very object is supposed to be also available in node 2 of the central instance. However, if this StateHolder

object is bein accessed in server process 2, i.e. with StateHolder.getSingleton(), that was created in server

process 1, the StateHolder object is being recreated with of course all saved data being lost. By calling

StateHolder.getSingleton() in node 2, not the same StateHolder instance of node one is returned, but a new one.

Is there a possibility to let node 2 access the identical StateHolder singleton instance of node 1, or to copy

this instance into node 2?

Any help would be greatly appreciated.

Kind regards

Andreas

Accepted Solutions (1)

Accepted Solutions (1)

siarhei_pisarenka3
Active Contributor
0 Kudos

Andreas

In our project we solved the problem with help of DB. In other words we replaced our singleton object with a dedicated table in DB. Our singleton was a kind of registry, so we just stored the registry in a table, implemented read/write methods that use JDBC in order to read/write the registry data from DB.

This is one more idea that you can use. By the way what is your singleton's type? What data shall it keep?

BR, Sergei

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi Andreas

Have you solved this issue?

If you did, please explain me how you did it.

Thanks in advanced

Emmanuel Rebolledo

Former Member
0 Kudos

Hi Sergei,

Unfortunately, saving a POJO object in JNDI in server process/node one results in a

com.sap.engine.services.jndi.persistent.exceptions.NameNotFoundException when

looking up in server process/node two:

saving in server process/node one:

Context context = new InitialContext();
StateHolder holder = new StateHolder();
context.rebind("MyStateHolder", holder);

looking up in server process/node two:

Context context = new InitialContext();
StateHolder am = (StateHolder)context.lookup("MyStateHolder");

--> com.sap.engine.services.jndi.persistent.exceptions.NameNotFoundException:

Object not found in lookup of MyStateHolder.

Obviously, there is no replication in JNDI between several

server nodes in a dialog instance when binding an object into it.

It seems that new InitialContext() builds up a new fresh empty context. If so,

it is clear that the object under MyStateHolder cannot be looked up.

But how to avoid that? How to access an evtl. existing replicated JNDI context tree

from different server nodes?

Kind regards

Andreas

Former Member
0 Kudos

Hi Sergei,

Very good point with Serializable Object registered in JNDI! I am going to try that.

I also already tried it with an Stateful and Stateless EJB instead of a Singleton POJO (the StateHolder Java class in my opening post).

But as soon as the server process change takes place, it always creates a new been instance when looking up the state EJB. I also cannot save the bean instance in a ServiceLocator since also the ServiceLocator (Singleton) is recreated in the other

server process of an instance.

Implementing the saving mechanism in the EJB and using the com.sap.bc.proj.jstartup.api Framework to spread the bean instance

(invocations) across all J2EE nodes (dialog instances) to their EJB containers is not fine grained enough. I would need to step down to each instance's server processes/nodes. I think this is not possible with this Framework.

I also thought of using a db table and a temporary file to save the state data. However, I would like to avoid that.

The state data is a Java Object containing a file name and an adtional number of a business entity. The data shall be used as base for a reporting functionality.

Thank you very much for the hint with JNDI! I will post again when I have the results.

Kind regards

Andreas

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi Andreas

One possible solution which came to my mind is using JNDI registry. If you make your singleton object serializable you can register it in JNDI. JNDI spreads across all J2EE nodes. Clients will have to use JNDI lookups in order to access your singleton.

Another thing which is spread across all the J2EE nodes is the EJB container. However, I cannot imagine right now how you can use EJB for singleton implementation.

BR, Sergei