This article is a mirror article of machine translation, please click here to jump to the original article.

View: 63300|Reply: 7

[.NET Core] ADO.NET Create a SqlConnection connection pool through the ObjectPool

[Copy link]
Posted on 2020-4-19 18:51:31 | | | |
ObjectPool is a new feature from Microsoft, Microsoft helps us maintain some objects that can be reused.

To put it simply, similar to shared bicycles, when you use it, scan the code to unlock it, the vehicle means that it is occupied, when you use it, when you lock the car, the vehicle will release the occupancy. These occupations and releases are all maintained by the platform for us.

Some objects are not thread-safe, but each time we instantiate the object, we need to consume some resources.

Detailed explanation of Object Pool in .NET Core
https://www.itsvse.com/thread-9152-1-1.html
(Source: Architect_Programmer)
First, we create a new SqlConnectionPooledObjectPolicy class that inherits the IPooledObjectPolicy <T>interface, and needs to implement its two methods.

How to instantiate the object in the Create method,Return indicates whether the object can still be reused, and when false is returned, the object will be deleted from the object pool.

The test source code is as follows:

The test results are as follows:



It can be seen that the connection pool is a little slower at the first 10 queries, and after that, it is faster than each instantiation of SqlConnection.

Attached is the source code

Tourists, if you want to see the hidden content of this post, pleaseReply






Previous:C# programs run time-consuming test classes
Next:MSSQL Trusted_Connection configuration
Posted on 2020-4-19 21:58:46 |
This is something new
Posted on 2020-4-30 13:05:18 |
Come and see, support, sofa
 Landlord| Posted on 2021-3-22 15:31:43 |
DefaultObjectPoolProvider source code
MaximumRetained Default Environment.ProcessorCount * 2;





 Landlord| Posted on 2021-3-22 15:34:51 |
It can also be written directly


 Landlord| Posted on 2021-3-23 09:50:57 |
Test code:

maximumRetained is set to 5, and the test is instantiated only once when it is returned every time it is used, as shown in the figure below:



In the case of not returning, the Test is instantiated 10 times, although it exceeds the maximumRetained setting of 5, and it will not throw an exception, it may be that after the use is completed, it will slowly recycle it, and then only 5 instances will be kept:


 Landlord| Posted on 2021-3-24 18:23:44 |
SQL Server Connection Pool (ADO.NET)

Whenever a user calls Open on a connection, the pool process looks for the connections available in the pool. If a pool connection is available, it is returned to the caller instead of opening a new connection. When the application calls Close on that connection, the pool process returns the connection to the active connection pool set instead of closing the connection.

https://docs.microsoft.com/zh-cn ... -connection-pooling

Posted on 2021-8-17 10:13:07 |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com