Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions mssql_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@
apilevel = "2.0"
paramstyle = "qmark"
threadsafety = 1

from .pooling import PoolingManager
def pooling(max_size=100, idle_timeout=600):
# """
# Enable connection pooling with the specified parameters.

# Args:
# max_size (int): Maximum number of connections in the pool.
# idle_timeout (int): Time in seconds before idle connections are closed.

# Returns:
# None
# """
PoolingManager.enable(max_size, idle_timeout)

4 changes: 3 additions & 1 deletion mssql_python/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mssql_python.constants import ConstantsDDBC as ddbc_sql_const
from mssql_python.helpers import add_driver_to_connection_str, check_error
from mssql_python import ddbc_bindings
from mssql_python.pooling import PoolingManager

logger = get_logger()

Expand Down Expand Up @@ -57,7 +58,8 @@ def __init__(self, connection_str: str = "", autocommit: bool = False, attrs_bef
connection_str, **kwargs
)
self._attrs_before = attrs_before or {}
self._conn = ddbc_bindings.Connection(self.connection_str, autocommit)
self._pooling = PoolingManager.is_enabled()
self._conn = ddbc_bindings.Connection(self.connection_str, autocommit, self._pooling)
self._conn.connect(self._attrs_before)
self.setautocommit(autocommit)

Expand Down
29 changes: 29 additions & 0 deletions mssql_python/pooling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# mssql_python/pooling.py
from mssql_python import ddbc_bindings
import threading

class PoolingManager:
_enabled = False
_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

@classmethod
def is_enabled(cls):
return cls._enabled
4 changes: 2 additions & 2 deletions mssql_python/pybind/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ SqlHandlePtr Connection::_envHandle = nullptr;
// This class wraps low-level ODBC operations like connect/disconnect,
// transaction control, and autocommit configuration.
//-------------------------------------------------------------------------------------------------
Connection::Connection(const std::wstring& conn_str, bool autocommit)
: _connStr(conn_str) , _autocommit(autocommit) {
Connection::Connection(const std::wstring& conn_str, bool autocommit, bool use_pooling)
: _connStr(conn_str) , _autocommit(autocommit), _usePool(use_pooling) {
if (!_envHandle) {
LOG("Allocating environment handle");
SQLHANDLE env = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion mssql_python/pybind/connection/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class Connection {
public:
Connection(const std::wstring& conn_str, bool autocommit = false);
Connection(const std::wstring& conn_str, bool autocommit = false, bool use_pooling = false);
~Connection();

// Establish the connection using the stored connection string.
Expand Down
4 changes: 3 additions & 1 deletion mssql_python/pybind/ddbc_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,9 @@ PYBIND11_MODULE(ddbc_bindings, m) {
py::class_<SqlHandle, SqlHandlePtr>(m, "SqlHandle")
.def("free", &SqlHandle::free, "Free the handle");
py::class_<Connection>(m, "Connection")
.def(py::init<const std::wstring&, bool>(), py::arg("conn_str"), py::arg("autocommit") = false)
.def(py::init<const std::wstring&, bool, bool>(), py::arg("conn_str"), py::arg("autocommit") = false, py::arg("use_pooling") = false,
"Create a new connection with the given connection string, "
"autocommit mode, and pooling option")
.def("connect", &Connection::connect)
.def("close", &Connection::disconnect, "Close the connection")
.def("commit", &Connection::commit, "Commit the current transaction")
Expand Down