This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes singleton class across Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "singleton class across" Watch "singleton class across" New topic
Author

singleton class across

guru prasanth
Ranch Hand

Joined: Aug 20, 2009
Posts: 96
Hi,'
I have a singleton class which is referred in a jar file.Now the jar file is included in multiple ear file.Let's take an example.
I have singleton class which is present in a singleton.jar and this jar file is copied in 4 different ear/war file.Now my requirement is
this class has to be singleton across multiple appliation.But when iam instatiating across application(.ear) file then it creates a different
singleton class.Is there any otherway other than making these ear files referring to single jar file.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

This doesn't seem to be related to SCEA. Did you mean to post it in BEA/WebLogic? (just guessing based on some of your previous posts). I can move this if you provide a hint on where you would like it moved to.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

You have these options

1. Move the jar up the class loader hierarchy so that it is loaded once.
2. Use an app server that uses a unified classloader factory. This way the first instance of the class will be picked up and the rest will be discarded.

Any reason why this Singleton is creating a problem across multiple apps ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
srivastava ashish
Greenhorn

Joined: Apr 12, 2010
Posts: 28
Is your singleton getInstance() method synchronized??

Refer to Head First Design Patterns -pg 180 ...I think you will get your answer.

Ashish
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

srivastava ashish wrote:Is your singleton getInstance() method synchronized??

Refer to Head First Design Patterns -pg 180 ...I think you will get your answer.

Ashish


That will not solve the problem when the same class is loaded by different class loaders
srivastava ashish
Greenhorn

Joined: Apr 12, 2010
Posts: 28
Thanks for pointing that out deepak..but is there any harm in putting singleton.jar in server classpath rather than individual EARs??. It might just work
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton class across
 
Similar Threads
Sharing JARs across EAR files
deploying to tomcat from ant
Same class from different jar loading problem
isue in singleton
How tro attach source file to single class file in WSAD?