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!

Tập đoàn VinGroup khởi công dự án đô thị Vinhomes Tân Cảng

Sáng nay 26/7/2014, tập đoàn VinGroup đã tổ chức lễ khởi công dự án Vinhomes Tân Cảng. Dự án nằm trong khu phức hợp với diện tích gần 43 héc ta toạ lạc tại cảng Tân Cảng, quận Bình Thạnh, Tp.HCM. Đây là khu đô thị phức hợp đồng bộ đầu tiên của tập đoàn VinGroup tại phía Nam sau những thành công của các dự án phía Bắc. Tới tham dự buổi lễ có đại diện lãnh đạo Thành phố HCM, quân chủng Hải Quân, Tổng Công ty Tân Cảng, tập đoàn VinGroup, chức sắc tôn giáo Thành phố, đại diện UBND quận Bình Thạnh cùng nhiều đơn vị phân phối tiếp thị cho dự án. Phát biểu tại buổi lễ, ông Tất Thành Cang, phó chủ tịch UBND TP.HCM cho biết, Tân Cảng Sài Gòn chiếm một vị trí hết sức quan trọng về an ninh quốc phòng. Sau khi di dời qua cảng Cát Lái, vị trí hiện tại tổng công ty Tân Cảng Sài Gòn đã tìm được đối tác “xứng tầm” là tập đoàn VinGroup triển khai xây dựng khu đô thị tầm cỡ, tạo vị thế cho đầu tàu phát triển kinh tế của cả nước. Ông Tất Thành Cang cũng cho biết, UBND Thành phố sẽ tạo mọi điều kiện tố...

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,...