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!

Khắc phục tình trạng ổ đĩa không hiển thị file và folder vì bị nhiễm virus

Có một số loại virus máy tính (mình chưa xác định loại nào) làm các file và folder bị set là hidden. Mặc dù các trình quét virus đã remove virus này tuy nhiên, trạng thái của các file, folder bị nhiễm vẫn là hidden. Thậm chí, trong folder option, ta đã set cho hiển thị hidden và system files. Sau 30 phút tìm hiểu, sau đây là cách khắc phục. Cũng khá đơn giản. Chia sẻ cùng mọi người. đây là tình trạng ổ đĩa USB của mình. Windows explorer thông báo ổ đĩa vẫn có data trong đó. Tuy nhiên, khi open ổ đĩa này thì không có file hay folder gì: Kiểm tra bằng command line cũng có kết quả tương tự, empty! Tuy nhiên, khi mình zip ổ đĩa này lại và mở bằng windows explorer thì có tồn tại files và folders ở trạng thái "hidden" và "system": Nhưng khi cấu hình folder option để cho phép hiển thị các files hidden thì không hiệu quả: Chà chà, đau đầu rồi..... ... ... 30 phút googling.... ......

Đồng Nai Lên Kế Hoạch Đấu Giá Đất Khu Vực Núi Chứa Chan Phát Triển Du Lịch

Đồng Nai đã chính thức đưa khu đất hơn 107 ha tại núi Chứa Chan vào danh sách đấu giá trong năm 2025 , với mục tiêu thu hút đầu tư phát triển Khu du lịch núi Chứa Chan . Đây là một trong những dự án trọng điểm của tỉnh nhằm phát triển du lịch sinh thái và tâm linh tại khu vực này. Núi Chứa Chan Kế Hoạch Đấu Giá Và Phát Triển Hạ Tầng Theo kế hoạch của UBND tỉnh Đồng Nai , trong năm 2025, tỉnh dự kiến đấu giá 37 khu đất với tổng số tiền thu về gần 21.000 tỷ đồng . Trong đó, khu đất tại núi Chứa Chan (huyện Xuân Lộc) có diện tích 107 ha , được định giá 1.348 tỷ đồng . Lộ trình triển khai đấu giá khu đất này bao gồm: Tháng 7/2025: Hoàn thành các bước thu hồi đất, giải phóng mặt bằng, điều chỉnh quy hoạch. Tháng 9/2025: Xác định, thẩm định và phê duyệt giá khởi điểm. Tháng 10-12/2025: Ban hành quyết định đấu giá, lựa chọn đơn vị tổ chức và tổ chức phiên đấu giá. Bên cạnh đó, tỉnh Đồng Nai cũng đang thực hiện điều chỉnh Quy hoạch tổng thể bảo quản, tu bổ và phát triển di tích ...