• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton for LDAP DirContext  RSS feed

 
Rithanya Laxmi
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to create a singleton for DirContext for LDAP configuration, hence i have used the initialize on demand class holder idiom as shown below:-





Now the problem is if i close the DirContext.close(), when the next request comes the singleton wont work as the dir context is already closed, hence
it will create a new dir context for each requests. Which breaks the singleton concept, hence how we can ensure the singleton works fine even with
DirContext.close()? Please clarify.

Thanks.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now the problem is if i close the DirContext.close(), when the next request comes the singleton wont work as the dir context is already closed, hence
it will create a new dir context for each requests.

I'm not sure what you mean by this as the code shown will return the same context object, it will just be a closed context after you've called close on it. Or does closing the context do something else as well like unloading the singleton class, loading a new classloader etc?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to close your DirContext then you can't use a singleton. From the javadoc for the close method
Invoking any other method on a closed context is not allowed, and results in undefined behaviour.
 
Rithanya Laxmi
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys, hence for singleton to work we cannot close the DirContext.close() right? Is there any alternative for this?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rithanya Laxmi wrote:Is there any alternative for this?

Create a new DirContext whenever you need one. Unless you can show that doing this causes a problem (performance, memory), then there is no reason to not do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!