Clear Connection pool

本文探讨了连接池与连接字符串之间的关系,解释了每个连接池都与特定的应用程序和连接字符串相关联。此外,还介绍了如何通过不同方法清除连接池,包括使用ADO.NET 2.0中的ClearAllPools和ClearPool方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The relationship between Connection pool and connection string


Connection pool and connection string go hand in hand. Every connection pool is associated with a distinct connection string and that too, it is specific to the application. In turn, what it means is – a separate connection pool is maintained for every distinct process, app domain and connection string.

 

A connection pool is also per process. So if you have two instances (or two web servers) of your application running and each have 50 connections in the pool, you will have a total of 100.


Connection Pool Deletion / Clearing Connection Pool (refer to http://www.codeproject.com/KB/dotnet/ADONET_ConnectionPooling.aspx)

Connection pool is removed as soon as the associated app domain is unloaded. Once the app domain is unloaded, all the connections from the connection pool become invalid and are thus removed. Say for example, if you have an ASP.NET application, the connection pool gets created as soon as you hit the database for the very first time, and the connection pool is destroyed as soon as we do iisreset . We'll see it later with example. Note that connection pooling has to do with IIS Web Server and not with the Dev Environment, s o do not expect the connection pool to be cleared automatically by closing your Visual Studio .NET dev environment.

ADO.NET 2.0 introduces two new methods to clear the pool: ClearAllPools and ClearPool . ClearAllPools clears the connection pools for a given provider, and ClearPool clears the connection pool that is associated with a specific connection. If there are connections in use at the time of the call, they are marked appropriately. When they are closed, they are discarded instead of being returned to the pool.

Refer to the section "Simple Ways to View Connections in the Pool Created by ADO.NET" for details on how to determine the status of the pool.

 

 

Clearing Connection Pool  (refer to http://www.codeproject.com/KB/dotnet/ADONET_ConnectionPooling.aspx)

Solution(s):

   1. If you are working with .NET and Oracle using ODP.NET v 9.2.0.4 or above, you can probably try adding "Validate Connection=true" in the connection string. Well, in couple of places, I noticed people saying use "validcon=true" works for them for prior versions on ODP.NET. See which works for you. With ODP.NET v 9.2.0.4, "validcon=true" errors out and "Validate Connection=true" works just fine.
   2. If you are working with .NET 2.0 and Microsoft SQL Server , You can clear a specific connection pool by using the static (shared in Visual Basic .NET) method SqlConnection.ClearPool or clear all of the connection pools in an appdomain by using the SqlConnection.ClearPools method. Both SqlClient and OracleClient implement this functionality.
   3. If you are working with .NET 1.1 and Microsoft SQL Server :
         1. In the connection string, at run time, append a blank space and try establishing the connection again . What in turn it would do is, a new connection pool would be created and will be used by your application, In the meantime the prior pool will get removed if it's not getting used.
         2. Do exception handling, and as soon as you get this error try connection afresh repeatedly in the loop. With time, ADO.NET and database server will automatically get in sync.
            Well, I am not totally convinced with either approach, but frankly speaking, I could not get any better workable solution for this so far.

 

 

ClearAllPools resets (or empties) the connection pool. (refer to http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.clearallpools(VS.80).aspx)

If there are connections in use at the time of the call,

they are marked appropriately and will be discarded (instead of being returned to the pool) when Close is called on them.

报错误如下C:\Users\70272\PyCharmMiscProject\.venv\Scripts\python.exe C:\Users\70272\PyCharmMiscProject\test.py Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 488, in _make_request raise new_e File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 464, in _make_request self._validate_conn(conn) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 1093, in _validate_conn conn.connect() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 790, in connect sock_and_verified = _ssl_wrap_socket_and_match_hostname( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 969, in _ssl_wrap_socket_and_match_hostname ssl_sock = ssl_wrap_socket( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 480, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 524, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock, server_hostname=server_hostname) File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 513, in wrap_socket return self.sslsocket_class._create( File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1071, in _create self.do_handshake() File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1342, in do_handshake self._sslobj.do_handshake() ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\adapters.py", line 667, in send resp = conn.urlopen( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 841, in urlopen retries = retries.increment( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\retry.py", line 474, in increment raise reraise(type(error), error, _stacktrace) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\util.py", line 38, in reraise raise value.with_traceback(tb) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 488, in _make_request raise new_e File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 464, in _make_request self._validate_conn(conn) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connectionpool.py", line 1093, in _validate_conn conn.connect() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 790, in connect sock_and_verified = _ssl_wrap_socket_and_match_hostname( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\connection.py", line 969, in _ssl_wrap_socket_and_match_hostname ssl_sock = ssl_wrap_socket( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 480, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\urllib3\util\ssl_.py", line 524, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock, server_hostname=server_hostname) File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 513, in wrap_socket return self.sslsocket_class._create( File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1071, in _create self.do_handshake() File "C:\Users\70272\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1342, in do_handshake self._sslobj.do_handshake() urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\http.py", line 32, in get resp = requests.get( File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\api.py", line 73, in get return request("get", url, params=params, **kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\sessions.py", line 703, in send r = adapter.send(request, **kwargs) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\requests\adapters.py", line 682, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\70272\PyCharmMiscProject\test.py", line 11, in <module> driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\chrome.py", line 40, in install driver_path = self._get_driver_binary_path(self.driver) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\manager.py", line 40, in _get_driver_binary_path file = self._download_manager.download_file(driver.get_driver_download_url(os_type)) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\drivers\chrome.py", line 32, in get_driver_download_url driver_version_to_download = self.get_driver_version_to_download() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\driver.py", line 48, in get_driver_version_to_download return self.get_latest_release_version() File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\drivers\chrome.py", line 70, in get_latest_release_version resp = self._http_client.get(url=latest_release_url) File "C:\Users\70272\PyCharmMiscProject\.venv\lib\site-packages\webdriver_manager\core\http.py", line 35, in get raise exceptions.ConnectionError(f"Could not reach host. Are you offline?") requests.exceptions.ConnectionError: Could not reach host. Are you offline? 进程已结束,退出代码为 1
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值