Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Singleton for LDAP DirContext

User_19BPUAug 9 2013 — edited Aug 10 2013

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


public class SlmApplicationContext {


/**
     * inner class to hold the instance.
     */
    private static class Holder {
        private static DirContext instance =
            new InitialDirContext();
    }

    /**
     * Method to get the singleton instance of this class.
     * @return SlmApplicationContext
     */
    public static SlmApplicationContext getInstance() {
        return Holder.instance;
    }
   
    ...
   
    }
   
   
    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.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 7 2013
Added on Aug 9 2013
3 comments
1,060 views