The Artima Developer Community
Sponsored Link

Java Answers Forum
The Singleton Pattern

7 replies on 1 page. Most recent reply: Apr 16, 2002 5:04 PM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Chhavi Arya

Posts: 4
Nickname: chhavi
Registered: Apr, 2002

The Singleton Pattern Posted: Apr 11, 2002 11:38 PM
Reply to this message Reply
Advertisement
I was going thru "Thinking in Patterns with Java", by Bruce Eckel...

Wanted to ask a question about "singleton" pattern...

Can this design be deployed on clustered environment ?


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: The Singleton Pattern Posted: Apr 12, 2002 12:29 AM
Reply to this message Reply
What does it have to do with clustering? You want a single object's constituent parts spread across multiple machines? You can use the singleton pattern regardless of the underlying physical architecture of your system. All the singleton pattern says is that only one instance of a particular class can be instantiated (within the context of the JVM on which it is running, really).

Chhavi Arya

Posts: 4
Nickname: chhavi
Registered: Apr, 2002

Re: The Singleton Pattern Posted: Apr 14, 2002 8:09 PM
Reply to this message Reply
Thank you Matt for your reply.

I inherited some code which is based on Singleton approach, it is basically limiting the number of sessions at any point of time... (firstly i don't understand why it should be done this way... anyhow I have to fix it with least possible changes..)

It has a static vector collecting SessionIDs. Whenever a new session is created, sessioID is added in the vector. It also has unbinder, removing the sessionID from vector on expiry.

This code works perfectly fine on a single server, but the vector has different data on clustered environment.

On my first inspection of code, added Cloneable & Serializable interfaces to these classes. Still it did not work... there is one instance of vector on each clone..with different values.

When I posted my query I was guessing that maybe STATIC declarations was a problem in clustered environment.

I will try it again today.

Would it be alright if I send you the code for your expert advise on your email ?

Thanks

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: The Singleton Pattern Posted: Apr 14, 2002 10:06 PM
Reply to this message Reply
Well, I can't claim to be an expert, but it sounds interesting.

I think generally that static should not be used too indiscriminantly. It may work at first, but as you have discovered, it makes for inflexible code.

There is a difference between having a singleton and using static, however. Although, if you have a singleton, I suppose you could arrange it so that it works with static or non-static methods (when the class is first accessed, it creates the singleton, saves a reference, then all the static methods can refer to it).

I'm not sure what you mean by clustering. Do you mean that you have several application servers running the same code? Also, what is the effect you are trying to get? Do you want each node to have its own collection (Vector) of sessions, or do you want them to share the collection?

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: The Singleton Pattern Posted: Apr 15, 2002 12:15 AM
Reply to this message Reply
What bothers me in the question is the usage of the static key-word with the usage of deployment on multiple server.
Are you using EJB's ?
If that's the case, then the answer may reside somewhere else!
The usage of static variables is something one should avoid as it looking for troubles. If you want to use the Singelton Pattern in EJB's you will definitivelly have to glue the EJB to the LDAP (JNDI) server to store the EJB referenced into the LDAP Server !

rgds,

Thomas,

Chhavi Arya

Posts: 4
Nickname: chhavi
Registered: Apr, 2002

Re: The Singleton Pattern Posted: Apr 15, 2002 9:58 PM
Reply to this message Reply
Currently, it is NOT using EJBs... and I'm trying to push the use of EJBs.. your comments would definitely help me later :) Thanks Thomas.

Looks like STATIC declaration is creating problems...

Chhavi Arya

Posts: 4
Nickname: chhavi
Registered: Apr, 2002

Re: The Singleton Pattern Posted: Apr 15, 2002 11:27 PM
Reply to this message Reply
The NetworkDispatcher (ND) product is used to provide clustering...WebSphere Cluster.. by running an instance of the deployed application on each node in the cluster.

So yes, we have multiple Application servers runing same code.
Basically want to share the collection among the clones. ??

Thank you

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: The Singleton Pattern Posted: Apr 16, 2002 5:04 PM
Reply to this message Reply
From your postings:
On my first inspection of code, added Cloneable & Serializable interfaces to these classes. Still it did not work... there is one instance of vector on each clone..with different values
The NetworkDispatcher (ND) product is used to provide clustering...WebSphere Cluster.. by running an instance of the deployed application on each node in the cluster.

So you have more than one instance on each node. Which means there are more than one JVMs each running its own Session Manager. Thats the reason for copies of vector inspite of declaring it static. For a single instance, static vector works fine (which you already know). When more than one instance is running, you need a dedicated session cluster from which every other node gets the session. Perhaps this is what is being overlooked.

Flat View: This topic has 7 replies on 1 page
Topic: JavaServlets and sessions. Need a little help!! Previous Topic   Next Topic Topic: having problem with getting a date!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use