Skip to content

Commit 2cc10bf

Browse files
committed
Merge branch 'main' into saumya/pool-c++
2 parents 079bf57 + 047953c commit 2cc10bf

5 files changed

Lines changed: 48 additions & 3 deletions

File tree

mssql_python/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,18 @@
5353
apilevel = "2.0"
5454
paramstyle = "qmark"
5555
threadsafety = 1
56+
57+
from .pooling import PoolingManager
58+
def pooling(max_size=100, idle_timeout=600):
59+
# """
60+
# Enable connection pooling with the specified parameters.
61+
62+
# Args:
63+
# max_size (int): Maximum number of connections in the pool.
64+
# idle_timeout (int): Time in seconds before idle connections are closed.
65+
66+
# Returns:
67+
# None
68+
# """
69+
PoolingManager.enable(max_size, idle_timeout)
70+

mssql_python/connection.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mssql_python.constants import ConstantsDDBC as ddbc_sql_const
1212
from mssql_python.helpers import add_driver_to_connection_str, check_error
1313
from mssql_python import ddbc_bindings
14-
# from mssql_python.pooling import PoolingManager
14+
from mssql_python.pooling import PoolingManager
1515

1616
logger = get_logger()
1717

@@ -58,8 +58,7 @@ def __init__(self, connection_str: str = "", autocommit: bool = False, attrs_bef
5858
connection_str, **kwargs
5959
)
6060
self._attrs_before = attrs_before or {}
61-
# self._pooling = PoolingManager.is_enabled()
62-
self._pooling = False
61+
self._pooling = PoolingManager.is_enabled()
6362
self._conn = ddbc_bindings.Connection(self.connection_str, self._pooling, self._attrs_before)
6463
self.setautocommit(autocommit)
6564

mssql_python/pooling.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# mssql_python/pooling.py
2+
from mssql_python import ddbc_bindings
3+
import threading
4+
5+
class PoolingManager:
6+
_enabled = False
7+
_lock = threading.Lock()
8+
_config = {
9+
"max_size": 100,
10+
"idle_timeout": 600
11+
}
12+
13+
@classmethod
14+
def enable(cls, max_size=100, idle_timeout=600):
15+
with cls._lock:
16+
if cls._enabled:
17+
return
18+
19+
if max_size <= 0 or idle_timeout < 0:
20+
raise ValueError("Invalid pooling parameters")
21+
22+
ddbc_bindings.enable_pooling(max_size, idle_timeout)
23+
cls._config["max_size"] = max_size
24+
cls._config["idle_timeout"] = idle_timeout
25+
cls._enabled = True
26+
27+
@classmethod
28+
def is_enabled(cls):
29+
return cls._enabled

mssql_python/pybind/connection/connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class Connection {
1515
public:
1616
Connection(const std::wstring& connStr, bool fromPool);
17+
1718
~Connection();
1819

1920
// Establish the connection using the stored connection string.

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,7 @@ PYBIND11_MODULE(ddbc_bindings, m) {
19271927

19281928
py::class_<SqlHandle, SqlHandlePtr>(m, "SqlHandle")
19291929
.def("free", &SqlHandle::free, "Free the handle");
1930+
19301931
py::class_<ConnectionHandle>(m, "Connection")
19311932
.def(py::init<const std::wstring&, bool, const py::dict&>(), py::arg("conn_str"), py::arg("use_pool"), py::arg("attrs_before") = py::dict())
19321933
.def("close", &ConnectionHandle::close, "Close the connection")

0 commit comments

Comments
 (0)