• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton

 
Gus Mus
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion we cannot use static inside EJB because of Threading problem.
I saw at Petstore blueprint that the Service Locator is Singleton (so it's not Thread safe).
If this service locator is used inside EJB to look for another EJB then we break the rule
that we cannot use static inside EJB.
What is your opinion ?
 
Brian Alexander Lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have static members on ejbs. Of course you'll run into a problem if your ejb runs in multiple containers.
The threading problem can be solved by synchronizing sets and/or gets.
With something like a service locator it's probably okay that you have one static instance for each ejb container (or classloader) and there will probably only be one load that needs to be synchronized. Afterwards the data will be read only so gets don't need to be sync'd.
This is all conjecture as I haven't gone into the pet store that deeply.
 
Tom Marrs
Author
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've used ServiceLocator, and while it works OK, I have concerns about its Singleton implementation.
1) It's possible to have more than one ClassLoader per JVM, and the only way to guarantee uniqueness in a JVM for a class is the following combination: package + class name + Class Loader. When instantiating or referencing a Singleton there's no way to know which Class Loader created it. The only way to be sure would be to use the Boot Strap Class Loader, but this is inaccessible to developers. So, you could end up with one Singleton per class loader in the JVM.
2) While synchronization makes the Singleton thread-safe, it could cause a bottleneck if multiple threads are blocked as they wait for access to this resource.
While all of this bothers me, I don't have a solution, either. If I find out more, I'll post it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic