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