• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Providing sessionbeans through init()

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a Servlet that calls many methods of stateless sessionbeans. This Servlet is called very often. So it is really expensive for me to get a link to the sessionbean. I think a solution for me is to provide the sessionbeans by the init()-Method. So the sessionbeans are only generated once.
I think of something like this:



Is this a good solution? Are there any problems in a concurrent situation?

Thx for your advice!!
Bye
Mark
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Mescher:

Is this a good solution?



Not really. This is typical situation where you should go after Design Patterns. What you're looking for is described by the Service Locator pattern.
Take a look

here

And if you don't know about j2ee patterns (or maybe just GOF patterns). Don't think it's a waste of time studying them. It's actually the opposite (I mean, don't studying them is a waste of time
 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the Service Locator Pattern seems to be a good solution for me. One other question: I would initialize the Singleton in the init()-Method of the Servlet. If I deploy my different Servlets in Tomcat, it is possible that each Servlet has its own Instance of the singleton. Do you think this is a problem?
I have read something of workarounds by using a defined classloader. What do you think of this?

Bye

Mark
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Mescher:
If I deploy my different Servlets in Tomcat, it is possible that each Servlet has its own Instance of the singleton. Do you think this is a problem?



By definition, the Singleton has only one instance. So, if you create a Singleton in a correct manner it will never have more than one instance. Therefore, it's impossible for servlets to have their own instance of Singleton.

Also, you don't need to worry about the initialization of a Singleton. Because the Singleton itself is responsible for it's initialization. So, just call the methods you need and if the Singleton has not been initialized yet, it'll do so.

Example. When you call the getInstance() method of a Singleton, you'll have code like this:
 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for your advice.
I have googled for singletions in combination with Servlets. I found that some Servlet-Engines use another Classloader for each servlet. Therefore every Servlet would have a own instance of the Singleton. A solution for this would be to use an own classloader (I have found some advices to this problem). I will test this today and tell you how Tomcat is doing this things...
Bye

Mark
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
maybe you haven't gotten the point yet. The Singleton should NOT be a servlet. The Singleton is POJO (plain old java class). You should just call it's methods from the Servlets. As I said, just build a real Singleton and that's guaranteed it'll not have more than one instance.
So, I don't get why are you googling for this "singleton in combination with servlets". There's no need for this.
Also, don't forget that you will always have only one instance of each servlet (if this helps you in anyway).
What are you trying to do exactly?
[ August 11, 2005: Message edited by: Leandro Melo ]
 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
well I know that there is normally only one Instance of a Singleton. But I want to call methods of the Singleton through different Servlets! And if different Servlets (which are not initialized by me but by Tomcat) want to call the same Singleton, the Servlets must be instantiated by the same classloader (as I understand). And that is not so in every Servlet-Engine, I dont know how Tomcat does this.
Hopefully I'm wrong:-) That would make things a little bit easier
At the end I want to call methods of stateless session Beans (which hide my Business Logic). Till now I generate the home-Interface through JNDI everytime I enter the Servlet. I want to optimize this by the Singleton. It would be nice if a Singleton in a many-Servlets-Tomcat / Jboss - Webapp would be really single:-)
Let me do some tests and than we will see:-) I will post my results here!
[ August 11, 2005: Message edited by: Mark Mescher ]
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark, you should re-read the ServiceLocator and Singleton Design Patterns.
You're making a huge confusion with a very very simple thing.

Originally posted by Mark Mescher:

well I know that there is normally only one Instance of a Singleton. But I want to call methods of the Singleton through different Servlets!



That's the point!!! You can call the methods of the Singleton from different places (from servletA, servletB, servletC, a jsp, a enterprise java bean or a pojo). As long as they're in the same java virtual machine you'll ALWAYS get the same instance of the Singleton.

Suppose you're in ServletA (you'd have code like this).


Suppose you're now in ServletB:



Suppose you're now in ServletC:


Can you see now? The code is the same. When you call the getInstance() method of the singleton you'll be always returned the same instance!
If you call the getInstance() method on the singleton from any part of your application (a servlet, a jsp, a enterprise bean, a java bean or a pojo) you'll will ALWAYS be returned the only instance of the Singleton (as long as they're running on the same java virutal machine, which is propably your case).

Originally posted by Mark Mescher:
And if It would be nice if a Singleton in a many-Servlets-Tomcat / Jboss - Webapp would be really single:-)



It is really single!!! Well, as I said, I think you should re-read these Design Patterns, because from your comments one can tell you that you did not fully understand them.

[ August 11, 2005: Message edited by: Leandro Melo ]
[ August 11, 2005: Message edited by: Leandro Melo ]
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'll give you an extra help here.
I'm sending you the code of a fully working ServiceLocatorSingleton. It works perfectly on jboss 3.2.3 (for the default structure).
Check out the static method getInstance(), this is them method you should call to get the instance from any servlet.
Just insert this class in your package structure and it's ready to use (in the way I described on the previous post).

 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leandro,
now I had the time to test the Singleton Pattern. It works really fine. Thx for your detailed help and the example code.
I have worked before with Singletons in "normal" Apps so i knew the concept of Singletons.
The only problem I had was that I have read of some issues when using Singletons in Servlets in different Servlet-Engines. Look here for detailed informations:
http://www.churchillobjects.com/c/11043f.html
But not important yet...it works fine with Tomcat so I will change my application by using the Singleton-Pattern.
Thx a lot Leandro!!!

Bye

Mark
[ August 12, 2005: Message edited by: Mark Mescher ]
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic