Skip to content

Commit 6961abb

Browse files
authored
FEAT: Add Default Connection Pooling with User Opt-out Support (#118)
### ADO Work Item Reference <!-- Insert your ADO Work Item ID below (e.g. AB#37452) --> > [AB#37846](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/37846) ------------------------------------------------------------------- ### Summary <!-- Insert your Copilot Generated Summary below --> This pull request introduces enhancements to the connection pooling mechanism in the `mssql_python` package. Key changes include adding an option to explicitly disable pooling, auto-enabling pooling if not configured by the user, and introducing an initialization state to track whether pooling has been set up. ### Connection pooling enhancements: * [`mssql_python/__init__.py`](diffhunk://#diff-d95f3a67986de29f30453416b1b4c34e6a43207e9a33e2b1b80ef0c378b0a538L57-R61): Updated the `pooling` function to include an `enabled` parameter, allowing explicit disabling of pooling. Added logic to disable pooling when `enabled=False`. [[1]](diffhunk://#diff-d95f3a67986de29f30453416b1b4c34e6a43207e9a33e2b1b80ef0c378b0a538L57-R61) [[2]](diffhunk://#diff-d95f3a67986de29f30453416b1b4c34e6a43207e9a33e2b1b80ef0c378b0a538R70-R72) * [`mssql_python/connection.py`](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R61-R63): Added logic to auto-enable pooling during connection initialization if the user has not explicitly configured pooling. ### Pooling manager improvements: * [`mssql_python/pooling.py`](diffhunk://#diff-e51a48b4bd1555a5e32a6d20c9c6331f6a6d99e309185b418d0c2c6fe2e26541R7): Introduced `_initialized` state in `PoolingManager` to track whether pooling has been configured. Added `disable` method to explicitly disable pooling and updated `enable` method to set `_initialized` to `True`. Added `is_initialized` method to check initialization state. [[1]](diffhunk://#diff-e51a48b4bd1555a5e32a6d20c9c6331f6a6d99e309185b418d0c2c6fe2e26541R7) [[2]](diffhunk://#diff-e51a48b4bd1555a5e32a6d20c9c6331f6a6d99e309185b418d0c2c6fe2e26541R27-R41) <!-- ### PR Title Guide > For feature requests FEAT: (short-description) > For non-feature requests like test case updates, config updates , dependency updates etc CHORE: (short-description) > For Fix requests FIX: (short-description) > For doc update requests DOC: (short-description) > For Formatting, indentation, or styling update STYLE: (short-description) > For Refactor, without any feature changes REFACTOR: (short-description) > For release related changes, without any feature changes RELEASE: #<RELEASE_VERSION> (short-description) -->
1 parent fb0e7c1 commit 6961abb

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

mssql_python/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,21 @@
5454
threadsafety = 1
5555

5656
from .pooling import PoolingManager
57-
def pooling(max_size=100, idle_timeout=600):
57+
def pooling(max_size=100, idle_timeout=600, enabled=True):
5858
# """
5959
# Enable connection pooling with the specified parameters.
60-
60+
# By default:
61+
# - If not explicitly called, pooling will be auto-enabled with default values.
62+
6163
# Args:
6264
# max_size (int): Maximum number of connections in the pool.
6365
# idle_timeout (int): Time in seconds before idle connections are closed.
6466

6567
# Returns:
6668
# None
6769
# """
68-
PoolingManager.enable(max_size, idle_timeout)
70+
if not enabled:
71+
PoolingManager.disable()
72+
else:
73+
PoolingManager.enable(max_size, idle_timeout)
6974

mssql_python/connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def __init__(self, connection_str: str = "", autocommit: bool = False, attrs_bef
5858
connection_str, **kwargs
5959
)
6060
self._attrs_before = attrs_before or {}
61+
# Auto-enable pooling if user never called
62+
if not PoolingManager.is_initialized():
63+
PoolingManager.enable()
6164
self._pooling = PoolingManager.is_enabled()
6265
self._conn = ddbc_bindings.Connection(self.connection_str, self._pooling, self._attrs_before)
6366
self.setautocommit(autocommit)

mssql_python/pooling.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class PoolingManager:
77
_enabled = False
8+
_initialized = False
89
_lock = threading.Lock()
910
_config = {
1011
"max_size": 100,
@@ -24,12 +25,23 @@ def enable(cls, max_size=100, idle_timeout=600):
2425
cls._config["max_size"] = max_size
2526
cls._config["idle_timeout"] = idle_timeout
2627
cls._enabled = True
28+
cls._initialized = True
29+
30+
@classmethod
31+
def disable(cls):
32+
with cls._lock:
33+
cls._enabled = False
34+
cls._initialized = True
2735

2836
@classmethod
2937
def is_enabled(cls):
3038
return cls._enabled
3139

32-
@atexit.register
33-
def shutdown_pooling():
34-
if PoolingManager.is_enabled():
35-
ddbc_bindings.close_pooling()
40+
@classmethod
41+
def is_initialized(cls):
42+
return cls._initialized
43+
44+
@atexit.register
45+
def shutdown_pooling():
46+
if PoolingManager.is_enabled():
47+
ddbc_bindings.close_pooling()

0 commit comments

Comments
 (0)