-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpooling.py
More file actions
61 lines (52 loc) · 1.74 KB
/
pooling.py
File metadata and controls
61 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# mssql_python/pooling.py
import atexit
from mssql_python import ddbc_bindings
import threading
class PoolingManager:
_enabled = False
_initialized = False
_pools_closed = False # Track if pools have been closed
_lock = threading.Lock()
_config = {
"max_size": 100,
"idle_timeout": 600
}
@classmethod
def enable(cls, max_size=100, idle_timeout=600):
with cls._lock:
if cls._enabled:
return
if max_size <= 0 or idle_timeout < 0:
raise ValueError("Invalid pooling parameters")
ddbc_bindings.enable_pooling(max_size, idle_timeout)
cls._config["max_size"] = max_size
cls._config["idle_timeout"] = idle_timeout
cls._enabled = True
cls._initialized = True
@classmethod
def disable(cls):
with cls._lock:
if cls._enabled and not cls._pools_closed: # Only cleanup if enabled and not already closed
ddbc_bindings.close_pooling()
cls._pools_closed = True
cls._enabled = False
cls._initialized = True
@classmethod
def is_enabled(cls):
return cls._enabled
@classmethod
def is_initialized(cls):
return cls._initialized
@classmethod
def _reset_for_testing(cls):
"""Reset pooling state - for testing purposes only"""
with cls._lock:
cls._enabled = False
cls._initialized = False
cls._pools_closed = False
@atexit.register
def shutdown_pooling():
with PoolingManager._lock:
if PoolingManager._enabled and not PoolingManager._pools_closed:
ddbc_bindings.close_pooling()
PoolingManager._pools_closed = True