Chuyển đến nội dung chính

System.DirectoryServices and connection pooling

From DirTeam



System.DirectoryServices and connection pooling

Connection pooling is something with most of .NET and SQL developers are pretty familiar with. It is mechanism which allows to re-usage of once established connection under some conditions. Establishing connections are considered as costly operation in networking world. So it might do good for performance of Your application if only one connection will be used and will stay open as long as You need it.
S.DS is also imlementing connection pooling under some conditions. I was aware that S.DS should use the same connection if:
  • connection is being made within the same security context
  • connection is using the same authentication type.
So I was a little surprised when working on some code cleanup and re-write for Extensible MA I've hit max user connection limit on Windows 2003 Server (5000 by default). Code was pretty simple - I have list of DNs and have to enumerate some attributes for each of them. I used DirectoryEntry for this in a way somewhat similar to this snippet:
using (DirectoryEntry member = new DirectoryEntry())
{
member.Username = userName;
member.Password = userPwd;
member.AuthenticationType = AuthenticationTypes.Secure;
foreach (string dn in dns)
{
member.Path = “LDAP://” + ldapHostName”/” + dn;
Guid guid = new Guid((byte[])member.Properties[”objectGUID”].Value);
Console.WriteLine(guid.ToString(”B”));
}
}

So ... credentials in the loop at the same, authentication type is the same but new connections were established for each DN being processed. After some investigation and discussion on DL it turned out that connection will be re-used as long as there will be one object which will be live and keeping this connection alive. For example DirectoryEntry which will point to rootDSE:
using(DirectoryEntry rootEntry = new DirectoryEntry(
“LDAP://” + ldapHostName/rootDSE”,
userName,
userPwd,
AuthenticationTypes.Secure))
{
rootEntry.RefreshCache();
using (DirectoryEntry member = new DirectoryEntry())
{
member.Username = userName;
member.Password = userPwd;
member.AuthenticationType = AuthenticationTypes.Secure;
foreach (string dn in dns)
{
member.Path = “LDAP://w2k.pl/” + dn;
Guid guid = new Guid((byte[])member.Properties[”objectGUID”].Value);
Console.WriteLine(guid.ToString(”B”));
}
}
}

This simple modification does the trick - right now all objects within the loop were using the same connection. You have to remember that DirectoryEntry is using lazy bind so to open a connection You have to actually read data from object, in this example I've used RefreshCache method.
To show You how this can impact application performance I will give You example from my lab. My code was processing about 80k of user objects (this was slow VM, only 384 of RAM on notebook) and with this bad connection handling this was executed for 2h48m. With this simple change time required to process these entries was 0h56m in the same environment (this is Extensible MA for MIIS so this is time to produce data and read them into CS).
Just putting it here for others who may seek solution to this problem. My lack of developer background something is painful ... but I'm doing my best :), at least I'm trying.
I have to say Thank You to Rayn and other people who were helping me on this case. Ryan joined MSFT some time ago so I was lucky to have opportunity to ping him on IM and bother with my problems :). Gladly sorted out.

Nhận xét

Bài đăng phổ biến từ blog này

Ứng dụng tách file Contacts (vCard, *.vcf)

Trong itunes, khi backup dữ liệu Contacts, itunes sẽ lưu các Contacts dưới dạng 1 file vCard (*.vcf) duy nhất. Nếu cần import những Contacts này vào Windows Address Book (Wab) thì bạn cần phải tách file  vCard trên thành nhiều file vCard. Mỗi file vCard dành cho một contact. Phần mềm nhỏ dưới đây sẽ giúp bạn làm việc này: Download phần mềm tại đây:  Tool Tách file vCard Download source code tại đây:  Source Code Hope this help!

Phát Triển Bất Động Sản Khu Công Nghiệp Gắn Liền Với Nhà Ở Xã Hội – Giải Pháp Hút Vốn FDI Và Ổn Định Lao Động

 Sự kết hợp giữa phát triển bất động sản khu công nghiệp và phát triển nhà ở xã hội đang trở thành một trong những chiến lược quan trọng để thu hút dòng vốn đầu tư trực tiếp nước ngoài (FDI) vào Việt Nam. 1. Hạ tầng khu công nghiệp – Yếu tố quyết định thu hút FDI Việt Nam đang là một trong những điểm đến hấp dẫn của các nhà đầu tư nước ngoài, đặc biệt trong lĩnh vực công nghiệp và sản xuất. Tuy nhiên, một trong những điều kiện quan trọng để các doanh nghiệp FDI quyết định rót vốn chính là hạ tầng khu công nghiệp và chất lượng nguồn lao động . Trong đó, nhà ở cho công nhân là yếu tố thiết yếu, đảm bảo đời sống ổn định cho người lao động và giúp doanh nghiệp duy trì lực lượng sản xuất lâu dài . Nhiều tập đoàn quốc tế khi đầu tư vào Việt Nam không chỉ quan tâm đến hạ tầng sản xuất, giao thông , mà còn đặt yêu cầu về các tiện ích xã hội, bao gồm nhà ở cho công nhân, trường học, bệnh viện . Nếu vấn đề nhà ở không được giải quyết, lao động có thể chuyển dịch sang các khu vực khác,...

Riviera Point - Căn hộ hoàn hảo ven sông

Vừa qua, tập đoàn Keppel Land tiếp tục mở bạn các căn hộ thuộc dự án căn hộ cao cấp Riviera Point quận 7, TP.HCM, tiêu chuẩn Singapore. Chi tiết căn hộ giai đoạn 1: LOẠI DIỆN TÍCH SỐ CẴN GIÁ 2 phòng ngủ 91.7 – 105.2 m2 339 30 – 35 triệu /m2 3 phòng ngủ 120.9 – 148.8 m2 169 4 phòng ngủ 188.7 339 Penthouse – Bán 263.5 – 348.8 m2 6 10 Tỷ LH Kinh doanh 090.394.6116 Dự án căn hộ cao cấp Riviera Point với tổng vốn đầu tư 206 triệu USD. Dự án sẽ cung cấp 2.400 căn hộ và tiện nghi giải trí với tổng diện tích sàn xây dựng 408.500 m2. Các căn hộ từ 2, 3, 4 phòng ngủ và biệt thự trên không. Thiết kế căn hộ rộng rãi, dễ dàng trang trí nội thất. Giai đoạn một bao gồm 700 căn hộ. Dự kiến giai đoạn 1 của  Riviera Point  sẽ hoàn thành vào đầu năm 2015, và năm 2020 toàn bộ dự án sẽ đi vào hoạt động. Xem nhà mẫu liên hệ: 090.394.6116