From: Luke Daley Date: 2006-11-01T11:21:06+09:00 Subject: Net::LDAP connection semantics problem and nested blocks. Hi, I am working with the Net::LDAP library to communicate with active directory. The connection semantics of that library are causing me problems which I am trying to solve. The library was designed to connect and then disconnect from the LDAP server before and after each operation. I am running into a problem where the server is refusing connections because of too many successive connections. Some background.... I have a Net::LDAP subclass that among other things, supplies credential and connection information to the parent on construction based on a single key string that is given. class MyLDAP < Net::LDAP @cache = {} def self.get(key) if @cache.has_key? key @cache[key] else @cache[key] = CortecsLDAP.new(key) end end def initialize(key) super self.connect_details_for_key(key) end def self.connect_details_for_key(key) # Connect to DB and get details here relating to key end The caching business is because this it is common for 1000+ ldap operations to happen in one execution so I thought I was being more efficient in holding the connections open like that. The semantics of Net::LDAP however open and close underling tcp connection before and after each operation. Basically making my caching obsolete. However, Net::LDAP provides the method open() at the class and object level which takes a block and passes a Net::LDAP object to the block, keeping its connection open for the duration of the block. What I would like to do is be able to hold open all of the objects in the cache for the duration of a block. This is probably what I would want the interface to look like.... LDAPSession.open do | ldap_connection_cache | my_ldap_instance = ldap_connection_cache.get('some_key') my_other_ldap_instance = ldap_connection_cache.get('some_other_key') end The important thing here is that my_ldap_instance and my_other_ldap_instance remain open for the whole time they are in the block they are in. The only way to keep a Net::LDAP connection open is to use the open() method described above and pass it a block. I hope that makes sense to someone who can help. Cheers. ----------------------------- Luke Daley http://www.ldaley.com -----------------------------